使用XSLT进行转换时更改标记名称

时间:2014-04-09 13:49:39

标签: xslt-2.0

我正在使用XSLT进行XML转换,并在重命名标记时遇到问题。请找到相同的下面的详细信息。我转换后的XML应该使用BookName而不是Name和LibraryName而不是Name标签。

输入XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Catalog xmlns="http://example.com">
    <Books>
        <Book>
            <Name>Wise Otherwise</Name>
            <author>Great Expectations</author>
        </Book>
        <Book>
            <Name>Rich Dad Poor Dad</Name>
            <author>Orange</author>
        </Book>
    </Books>
    <libraries>
        <library>
            <Name> Forsyth </Name>
            <city> Cumming </city>
        </library>
        <library>
            <Name> COBB </Name>
            <city> Marietta </city>
        </library>
    </libraries>
</Catalog>

转换后的预期XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Catalog xmlns="http://example.com">
    <Books>
        <Book>
            <BookName>Wise Otherwise</BookName>
            <author>Great Expectations</author>
        </Book>
        <Book>
            <Name>Rich Dad Poor Dad</Name>
            <author>Orange</author>
        </Book>
    </Books>
    <libraries>
        <library>
            <LibraryName> Forsyth </LibraryName>
            <city> Cumming </city>
        </library>
        <library>
            <LibraryName> COBB </LibraryName>
            <city> Marietta </city>
        </library>
    </libraries>
</Catalog>

我的XSLT相同

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

<xsl:strip-space  elements="*"/>

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

 <xsl:template match="ns:Name">
    <xsl:for-each select="Catalog/Books/Book/Name">
        <BookName>
            <xsl:apply-templates />
        </BookName>
    </xsl:for-each>
    <xsl:for-each select="Catalog/libraries/library/Name">
        <LibraryName>
            <xsl:apply-templates />
        </LibraryName>
    </xsl:for-each>
</xsl:template>     
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:2)

您可以使用此XSLT作为参考:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://example.com" xmlns="http://example.com" exclude-result-prefixes="ns">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="ns:Book/ns:Name">
        <BookName>
            <xsl:apply-templates/>
        </BookName>
    </xsl:template>

    <xsl:template match="ns:library/ns:Name">
        <LibraryName>
            <xsl:apply-templates/>
        </LibraryName>
    </xsl:template>
</xsl:stylesheet>

我使用和不使用前缀声明了命名空间。因此,所有新创建的元素都将属于默认命名空间。也排除了前缀,因为它没有被使用。

您可以编写多个模板来匹配要更改的节点。例如,阅读本教程:http://www.xmlplease.com/xsltidentity

相关问题