4

xsltでカットアンドペーストする方法は?私のxmlがこのようになっているとしましょう。

  <root>
    <header>
      some nodes
    </header>
    <body>
    <p>
      some text
    <link>1</link>
      <note xml:id="c5-note-0001" numbered="no">text</note>
    </p>
    <p>
      some text
      <link>2</link>
      <figure>
        <note xml:id="c5-note-0003">text</note>
      </figure>
      <note xml:id="c5-note-0002">text</note>
    </p>
    <tabular>
      <note xml:id="c5-note-0004" numbered="no">text</note>
    </tabular>
    <p>
      some text
      <link>3</link>
      <notegroup>
        <note xml:id="c5-note-0006">text</note>
      </notegroup>
      <note xml:id="c5-note-0005">text</note>
    </p>
    <p>
      some text
      <link>4</link>
      <note xml:id="c5-note-0007">text</note>
    </p>
    </body>
  </root>

私の期待する出力は、

  <root>
    <header>
      some nodes
    </header>
    <body>
      <p>
        some text
        <link>1</link>
        <note xml:id="c5-note-0001" numbered="no">text</note>
      </p>
      <p>
        some text
        <link>2</link>
        <figure>
            <note xml:id="c5-note-0003">text</note>
        </figure>
        <notenum>1</notenum>
      </p>
      <tabular>
        <note xml:id="c5-note-0004" numbered="no">text</note>
      </tabular>
      <p>
        some text
        <link>3</link>
        <notegroup>
          <note xml:id="c5-note-0006">text</note>
        </notegroup>
        <notenum>2</notenum>
      </p>
      <p>
        some text
        <link>4</link>
        <notenum>3</notenum>
      </p>
      <newnote>
        <note xml:id="c5-note-0002">text</note>
        <note xml:id="c5-note-0005">text</note>
        <note xml:id="c5-note-0007">text</note>
      </newnote>
    </body>
  </root>

newnotebodyタグの終わりの前に新しいノードを作成し、そのノードを切り取って貼り付ける必要があり、その代わりにノードnoteを生成する必要があります。notenumnote

pこれはノード内でのみ行う必要があります。に該当する場合は、notetabularもする必要はfigureありnotegroupません。

note次のような属性が含まれている場合はnumbered="no"、何もする必要はありません。

私は次のxsltを使用しています(使用しているテンプレートの一致を示すためだけに)、

  <?xml version="1.0" encoding="utf-8"?>
   <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
  >
      <xsl:output method="xml" indent="yes"/>

      <xsl:template match="@* | node()">
          <xsl:copy>
              <xsl:apply-templates select="@* | node()"/>
          </xsl:copy>
      </xsl:template>
    <xsl:template match ="figure">
      some operation
    </xsl:template>
    <xsl:template match ="tabular">
      some operation
    </xsl:template>
    <xsl:template match ="notegroup">
      some operation
    </xsl:template>
  </xsl:stylesheet>

前もって感謝します。

4

1 に答える 1

3

一般に、XSLTでは、「カットアンドペースト」を実際の状態、つまり「オリジナルをコピーアンドペーストして削除する」と想像するのが有益です。

bodyタグの終わりの前に新しいノードnewnoteを作成し、それにノートノードを切り取って貼り付ける必要があります

あなたがしたいのは、<body>要素を適応させて、(おそらく他の方法で変更された)元のコンテンツの後に新しい<newnote>要素が含まれるようにすることです。したがって、<body>要素の新しいテンプレートを追加します。

<xsl:template match="body">
</xsl:template>

前述のように、基本的には元の内容を引き継ぐ必要があります。その元のコンテンツは他のテンプレートによって引き続き処理される可能性があるため、<xsl:apply-tepmlates>ここで使用します。

<xsl:template match="body">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

元のコンテンツの後に、新しい要素を挿入します。

<xsl:template match="body">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    <newNote>
    </newNote>
</xsl:template>

最後に、この要素では、説明されているすべての要素のコピーが必要です。これは、ループ<note>で実現できます。<xsl:for-each>

<xsl:template match="body">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
    <newnote>
        <xsl:for-each select="p/note[not(@numbered = 'no')]">
            <xsl:copy-of select="."/>
        </xsl:for-each>
    </newnote>
</xsl:template>

そのノートの代わりにnotenumノードを生成する必要があります。

<note>これは、それぞれの要素を置き換えるテンプレートを使用して実行できます。

<xsl:template match="p/note[not(@numbered = 'no')]">
    <notenum><xsl:value-of select="count(preceding::note[parent::p][not(@numbered = 'no')]) + 1"/></notenum>
</xsl:template>

<notenum>代わりに新しい要素を挿入し、<xsl:value-of>コマンドを使用して計算値を出力します。値は、<note>制限に一致する先行要素の数に1を加えたものです。

于 2012-08-04T10:55:04.333 に答える