如何重命名root和children元素XSLT

时间:2015-10-03 03:19:05

标签: xslt rename root

如何重命名root' channel'和孩子的项目'记录'和'记录'分别?

输入

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<item>
<title>A</title>
<link>/news/view/25857/A.html</link>
<guid isPermaLink="true">/news/view/25857/A.html</guid>
<comments>/news/25857/A.html</comments>
<pubDate>Sat, 03 Oct 2015 00:42:42 GMT</pubDate>
<description><![CDATA[]]></description>
<category>headline,hacker,bank,cybercrime,data loss,fraud</category>
</item>
</channel>
</rss>

XSLT

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
      <xsl:template match="item/comments">
        <taglink>
            <xsl:text></xsl:text>
            <xsl:value-of select="text()" />
        </taglink>
    </xsl:template>

    <!--delimits values if separated by comma-->
    <xsl:template match="item/category[contains(.,',')]">
        <category>
            <xsl:variable name="elementName" select="name(..)"/>

            <xsl:call-template name="splitIntoElements">
                <xsl:with-param name="baseName" select="name(..)" />
                <xsl:with-param name="txt" select="." />    
            </xsl:call-template>
        </category>
    </xsl:template>

    <xsl:template name="splitIntoElements">

        <xsl:param name="baseName" />
        <xsl:param name="txt" />
        <xsl:param name="delimiter" select="','" />
        <xsl:param name="index" select="1" />

        <xsl:variable name="first" select="substring-before($txt, $delimiter)" />
        <xsl:variable name="remaining" select="substring-after($txt, $delimiter)" />

        <xsl:element name="value">
            <xsl:choose>
                <xsl:when test="$first">
                    <xsl:value-of select="$first" />
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$txt" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:element>     
        <xsl:if test="$remaining">
            <xsl:call-template name="splitIntoElements">
                <xsl:with-param name="baseName" select="$baseName" />
                <xsl:with-param name="txt" select="$remaining" />
                <xsl:with-param name="index" select="$index" />
                <xsl:with-param name="delimiter" select="$delimiter" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

输出

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
   <channel>
      <item>
         <title>A</title>
         <link>/news/view/25857/A.html</link>
         <guid isPermaLink="true">/news/view/25857/A.html</guid>
         <taglink>/news/25857/A.html</taglink>
         <pubDate>Sat, 03 Oct 2015 00:42:42 GMT</pubDate>
         <description/>
         <category>
            <value>headline</value>
            <value>hacker</value>
            <value>bank</value>
            <value>cybercrime</value>
            <value>data loss</value>
            <value>fraud</value>
         </category>
      </item>
   </channel>
</rss>

预期输出 - 有许多&#39;项目&#39;在&#39;频道内&#39;所以预计会有很多记录和#39;在&#39;记录&#39;

<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
   <Records>
      <Record>
         <title>A</title>
         <link>/news/view/25857/A.html</link>
         <guid isPermaLink="true">/news/view/25857/A.html</guid>
         <taglink>/news/25857/A.html</taglink>
         <pubDate>Sat, 03 Oct 2015 00:42:42 GMT</pubDate>
         <description/>
         <category>
            <value>headline</value>
            <value>hacker</value>
            <value>bank</value>
            <value>cybercrime</value>
            <value>data loss</value>
            <value>fraud</value>
         </category>
      </Record>
   </Records>
</rss>

1 个答案:

答案 0 :(得分:1)

您只需添加一些与要重命名的元素相匹配的模板,然后吐出新的元素名称,例如:

<xsl:template match="channel">
    <Records>
        <xsl:apply-templates/>
    </Records>
</xsl:template>

<xsl:template match="item">
    <Record>
        <xsl:apply-templates/>
    </Record>
</xsl:template>