开发者

How do I rename XML tags using XSLT

This is my XML-

<CATALOG>
    <NAME>C1</NAME>
    <CD>
        <NAME>Empire Burlesque</NAME>
        <ARTIST>Bob Dylan</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>Columbia</COMPANY>
        <PRICE>10.90</PRICE>
        <YEAR>1985</YEAR>
    </CD>
    <CD>
        <NAME>Hide your heart</NAME>
        <ARTIST>Bonnie Tyler</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>CBS Records</COMPANY>
        <PRICE>9.90</PRICE>
        <YEAR>1988</YEAR>
    </CD>
</CATALOG>

I want to replace the NAME tag in catalog to CATALOG-NAME and the the NAME tag in CD's to CD-NAME which should make my xml look like this-

<CATALOG>
    <CATALOG-NAME>C1</CATALOG-NAME>
    <CD>
        <CD-NAME>Empire Burlesque</CD-NAME>
        <ARTIST>Bob Dylan</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>Columbia</COMPANY>
      开发者_JAVA百科  <PRICE>10.90</PRICE>
        <YEAR>1985</YEAR>
    </CD>
    <CD>
        <CD-NAME>Hide your heart</CD-NAME>
        <ARTIST>Bonnie Tyler</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>CBS Records</COMPANY>
        <PRICE>9.90</PRICE>
        <YEAR>1988</YEAR>
    </CD>
</CATALOG>


Use the identity transform with overrides for the elements you want to rename:

<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="CD/NAME">
        <CD-NAME><xsl:apply-templates select="@*|node()" /></CD-NAME>
    </xsl:template>
    <xsl:template match="CATALOG/NAME">
        <CATALOG-NAME><xsl:apply-templates select="@*|node()" /></CATALOG-NAME>
    </xsl:template>
</xsl:stylesheet>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜