0

一部のセクションを処理して再利用するためのテンプレートを作成したいと思います。

以下のXMLでは、メッセージが、、、ごとに繰り返されていることがわかります。必要に応じて処理して呼び出すテンプレートを作成したいと思います。

        <Data>

           <A>
              <text>this is text</text>
              <Message>
                <local>Local  link</local>
                <STD>External link</STD>
                </Message>
            </A>
            <B>
               <info>Information</info>
               <Message>
                <local>Local uri link</local>
                <STD>External link uri</STD>

                </Message>
            </B>
            <C>
               <longtext>Long Text</longtext>
               <Message>
                <local>Local uri link</local>
                <STD>External link uri</STD>

                </Message>
                </C>
        <Data>

必要な出力:

    <Information>     
          <AA>
                this is text
                <MSG local value="Local uri link" STD value="External link"/>
            </AA>

            <BB>
                Information
                <MSG local value  ="Local uri link"  STD value="External link"/>

            </BB>
            <CC>
                Long Text
                <MSG local value="Local uri link" STD value="External link"/>
            </CC>

    <Information>

すべてのノードでタグを処理している間、私はA、B、Cのすべてのタグのコードを書いています。

書かれたサンプルコード

    <Information>

        <xsl:template match="A">
        <AA>
            <xsl:value-of select="text"/>
            <xsl:element name="MSG">
                <xsl:attribute name="local value">
                    <xsl:value-of select="Message/local"/>
                </xsl:attribute>
                <xsl:attribute name="STD value">
                    <xsl:value-of select="Message/STD"/>
                </xsl:attribute>
        </AA>
    </Information>

同様に、すべてのテンプレートについて、ブロックMSGのコードを明示的に記述しています。

次に、を処理するための別のテンプレートを作成します。そして、私はすべてのテンプレートからこのテンプレートを呼び出したいと思います。

基本的に私は処理するために書かれたコードを再利用したい

誰かが私にそれをする方法を手伝ってくれますか?

ありがとうございました。

4

1 に答える 1

3

サンプルは次のとおりです。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

<xsl:template match="Data">
  <Information>
    <xsl:apply-templates/>
  </Information>
</xsl:template>

<xsl:template match="Data/*">
  <xsl:element name="{local-name()}{local-name()}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<xsl:template match="Data/A/text | Data/B/info | Data/C/longtext">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="Message">
  <MSG local-value="{local}" STD-value="{STD}"/>
</xsl:template>

</xsl:stylesheet>

入力に適用した場合

<Data>

   <A>
      <text>this is text</text>
      <Message>
        <local>Local  link</local>
        <STD>External link</STD>
        </Message>
    </A>
    <B>
       <info>Information</info>
       <Message>
        <local>Local uri link</local>
        <STD>External link uri</STD>

        </Message>
    </B>
    <C>
       <longtext>Long Text</longtext>
       <Message>
        <local>Local uri link</local>
        <STD>External link uri</STD>

        </Message>
        </C>
</Data>

Saxon6.5.5出力

<?xml version="1.0" encoding="utf-8"?><Information>

           <AA>
              this is text
              <MSG local-value="Local  link" STD-value="External link"/>
            </AA>
            <BB>
               Information
               <MSG local-value="Local uri link" STD-value="External link uri"/>
            </BB>
            <CC>
               Long Text
               <MSG local-value="Local uri link" STD-value="External link uri"/>
                </CC>
        </Information>

投稿された入力サンプルも必要な出力サンプルも整形式のXMLではないため、XMLを出力するだけでなく、処理するために要素名または属性名にいくつかの変更を加える必要がありました。

于 2012-05-29T11:21:36.967 に答える