3

I am using this input xml file .

                 <Content>
                <body><text>xxx</text></body>
                    <body><text>yy</text></body>
               <body><text>zz</text></body>
               <body><text>kk</text></body>
                   <body><text>mmm</text></body>
                        </Content>

after Xslt transformation the output should be

                        <Content>
                 <body><text>xxx</text>
                       <text>yy</text>
                           <text>zz</text>
                     <text>kk</text>
                   <text>mmm</text></body>
                     </Content>

Can anyone please provide its relavant Xsl file.

4

2 に答える 2

2

この完全な変換:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="body"/>
 <xsl:template match="body[1]">
  <body>
   <xsl:apply-templates select="../body/node()"/>
  </body>
 </xsl:template>
</xsl:stylesheet>

提供された XML ドキュメントに適用した場合:

<Content>
    <body>
        <text>xxx</text>
    </body>
    <body>
        <text>yy</text>
    </body>
    <body>
        <text>zz</text>
    </body>
    <body>
        <text>kk</text>
    </body>
    <body>
        <text>mmm</text>
    </body>
</Content>

必要な正しい結果が生成されます

<Content>
   <body>
      <text>xxx</text>
      <text>yy</text>
      <text>zz</text>
      <text>kk</text>
      <text>mmm</text>
   </body>
</Content>

説明:

  1. アイデンティティ ルールは、すべてのノードを「そのまま」コピーします。

  2. これは、2 つのテンプレートによってオーバーライドされます。最初のものはすべての要素を無視/削除しますbody`。

  3. ID テンプレートをオーバーライドする 2 番目のテンプレートは、その親の最初の子である要素の最初のテンプレート (すべてbodyの要素を削除する)もオーバーライドします。この最初の子に対してのみ、要素が生成され、その本体で、その親の任意の子の子ノードであるすべてのノード(現在の要素とそのすべての兄弟) が処理されます。bodybodybodybodybodybodybody

于 2012-05-15T12:24:07.720 に答える
1
    <xsl:template match="Content">
      <body>
            <xsl:apply-templates select="body/text"/>
      </body>
    </xsl:template>

  <xsl:template match="body/text">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
于 2012-05-15T10:42:38.983 に答える