复制所有 xml 节点并使用 XSLT 重命名其中的几个节点 [英] copy all xml nodes and rename few in them using XSLT

查看:27
本文介绍了复制所有 xml 节点并使用 XSLT 重命名其中的几个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML,在那个 XML 我有一个元素 <storybody>它本身有几个元素,现在我想要的是复制 <storybody> 中的每个元素.并更改<URI><storybody>中的标签用 .

I have an XML, in that XML I have an element <storybody> which itself have several elements, now what I want is copy every in <storybody> and change <URI> tag in <storybody> with <a>.

请记住,我只需要为 <storybody> 执行此操作.在文档中.

Remember I need to do this for only <storybody> in document.

XML 结构:

<mothertag>
     <atag>
        asdfasdfa
     </atag>

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

现在我想将此 URI 更改为标签.

Now I want to change this URI to a tag.

推荐答案

您需要一个 身份转换 增加了额外的模板以匹配您想要重命名的节点.像这样:

You would need an identity transformation augmented with additional templates to match nodes you want renamed. Like this:

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

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

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

</xsl:stylesheet>

应用于(更正结束 storybody 标签中的错字)时:

When applied to (corrected the typo in the closing storybody tag):

<mothertag>
     <atag>
        asdfasdfa
     </atag>

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

</mothertag>

产生:

<mothertag>
   <atag>
      asdfasdfa

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

这篇关于复制所有 xml 节点并使用 XSLT 重命名其中的几个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆