复制所有xml节点,并使用XSLT 2重命名其中的几个节点

复制所有xml节点,并使用XSLT 2重命名其中的几个节点,xml,xslt,Xml,Xslt,我有一个XML,在这个XML中我有一个元素,它本身有几个元素,现在我想要的是复制每个in并更改标记名 XML结构: <mothertag> <atag> asdfasdfa </atag> <storybody> <p>sometext here<URI ref="http://google.com" /> some more text</p>

我有一个XML,在这个XML中我有一个元素,它本身有几个元素,现在我想要的是复制每个in并更改标记名

XML结构:

<mothertag>
     <atag>
        asdfasdfa
     </atag>

     <storybody>
        <p>sometext here<URI ref="http://google.com" /> some more text</p>
     </storybody>

</mothertag>

asdfasdfa
这里有一些文字,还有一些文字

  • 我想将名称
    更改为
  • 其他一切都应保持原样,并将
    更改为

  • 我在文档其余部分所做的是定义模板并逐一应用。现在我遇到了一个需要复制的元素,它只需要很少的更改和名称更改。

    要在XSLT中执行此操作,您只需要一个基本的标识转换,但
    元素除外

    大概是这样的:

    <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="storybody">
        <body>
          <xsl:apply-templates select="@*|node()"/>
        </body>
      </xsl:template>
    
    </xsl:stylesheet>
    

    如果您的意图与此不同,请澄清,我们将进一步帮助您。

    如果没有所需结果XML的示例,我们不清楚您想做什么,但是

    如果您需要扔掉大部分输入文档,只保留一个标记,那么此模式可能会有所帮助:

     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
       <xsl:template match="@*|node()">
           <xsl:apply-templates select="@*|node()"/>
       </xsl:template>
    
       <xsl:template match="storybody">
           <xsl:copy-of select="."/>
       </xsl:template>
    
     </xsl:stylesheet>
    
    
    
    当我打字时,我不是在一台有XSLT引擎的机器前面,所以我没有测试过这个。但是,我们的想法是将所有内容与第一个模板匹配,并忽略它,除了我们简单地复制到输出文档中的storybody元素


    如果这样做没有帮助,那么我建议您提供一个示例所需的结果XML文档和一个明确的规则声明,您希望应用这些规则

    实际上我不想使用标识转换。相反,我需要在文档中的一个元素上使用标识转换。请查看
    说明。
    属性可能是真正的
    ref
    还是实际的
    href
     <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
       <xsl:template match="@*|node()">
           <xsl:apply-templates select="@*|node()"/>
       </xsl:template>
    
       <xsl:template match="storybody">
           <xsl:copy-of select="."/>
       </xsl:template>
    
     </xsl:stylesheet>