如何获得与父标签的每一次出现的所有子标签转化xml文件只能有一个父标签下(how to get a

2019-09-17 05:41发布

我使用该输入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>

XSLT转换后输出应该是

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

任何人都可以请提供其初步认识XSL文件。

Answer 1:

这完全转变

<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. 它由两个模板覆盖。 首先忽略/删除每一个body element`。

  3. 第二个模板覆盖的身份模板还覆盖了第一个这样的模板(即删除每一个body部件),用于任何body是第一要素body其父的孩子。 对于这第一个body子止, body生成元素,并在其主体是任何的子节点的所有节点body其父子(电流body元件和其所有的body兄弟姐妹)进行处理。



Answer 2:

    <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>


文章来源: how to get a transformed xml file with all child tags of every occurance of parent tag under only one parent tag
标签: xslt