2015-10-03 76 views
1

如何分别将根'channel'和子项'重命名为'Records'和'Record'?如何重命名根元素和子元素XSLT

输入

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

期待ED输出 - 有这么有料可有“记录”中的许多“记录”

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

您可以简单地添加了几个模板匹配元素将改名为“通道”内的许多“项目”,并吐出新的元素名称,例如:

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

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