XSLT样式表,用于复制XML文档

时间:2012-08-01 17:03:59

标签: xml xslt

编写一个XSLT样式表,用于复制XML文档。源文档的元素和属性名称为大写。输出文档应该是精确副本,但元素和属性名称是小写的。例如,它应该转换:

<p>
<BODY ATTRIBUTE="TheValue">
<H1>Hello world</H1>
</BODY>

into

<body attribute=”TheValue”>
<h1>Hello world</h1>
</body>

1 个答案:

答案 0 :(得分:2)

试试这个:

<?xml version="1.0"?>
<!-- Transform a document to itself, lowercasing all tag names -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Import the identity transformation -->
    <!-- Whenever you match any node or any attribute -->
    <xsl:template match="node()|@*">
        <!-- Copy the current node -->
        <xsl:copy>
            <!-- Including any attributes it has and any child nodes -->
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <!-- Whenever you match any node or any attribute -->
    <!-- When you match any element -->
    <xsl:template match="*">
        <!-- Create the same element with a lowercase name -->
        <xsl:element name="{translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ',  'abcdefghijklmnopqrstuvwxyz')}">
            <!-- Including any attributes it has and any child nodes -->
            <xsl:apply-templates select="@*|node()"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>