使用 XSLT 重命名 XML 元素和逗号分隔 [英] Rename a XML element and comma seperate with XSLT

查看:23
本文介绍了使用 XSLT 重命名 XML 元素和逗号分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是将 iavmNotice/title 重命名为 iavmNotice/iavmTitle:
这是我的 XML:

What I want to do is rename iavmNotice/title to iavmNotice/iavmTitle:
Here is my XML:

<iavmNotice xmlns="http://stuff.com" noticeId="138643">
   <title>Cisco Vulnerability</title>
   <techOverview>
      <entry>
         <title>2012-2490</title>
         <description>Cisco ID 71.</description>
      </entry>
      <entry>
         <title>2012-2525</title>
         <description>Cisco ID 69.</description>
      </entry>
   </techOverview>
</iavmNotice>

这是我的尝试:

<xsl:template match="@* | node()">
  <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>
<xsl:template match="iavmNotice/title">
   <iavmTitle>
     <xsl:apply-templates select="@* | node()"/>
   </iavmTitle>
</xsl:template>
<xsl:template match="@iavmNotice/title">
  <xsl:attribute name="iavmTitle">
    <xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>

另外作为奖励,有没有办法以逗号分隔的形式导入 techOverview/entry/title?将其导入为 2012-2490、2012-2525 并删除描述.

Also as a bonus is there a way to import techOverview/entry/title as comma separated? Import it as 2012-2490, 2012-2525 and remove description.

推荐答案

xmlns="http://stuff.com" 声明在 iavmNotice 根元素上XML 文档将整个文档放入 http://stuff.com 命名空间.

The xmlns="http://stuff.com" declaration on the iavmNotice root element of the XML document puts the entire document into the http://stuff.com namespace.

所以你要么需要让你的样式表知道该命名空间,要么在你的匹配项中使用 local-name():

So you either need to make your stylesheet aware of that namespace, or else on your matches just use local-name():

<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="*[local-name() = 'iavmNotice']/*[local-name() = 'title']">
     <iavmTitle>
       <xsl:apply-templates select="@* | node()"/>
     </iavmTitle>
  </xsl:template>
</xsl:stylesheet>

这篇关于使用 XSLT 重命名 XML 元素和逗号分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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