创建示例输入XML文件:
<root>
<header>This header text</header>
<body>This is body text.</body>
</root>
Run Code Online (Sandbox Code Playgroud)运行身份转换,
<?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:stylesheet>
Run Code Online (Sandbox Code Playgroud)并确保生成与输入XML相同的XML:
<root>
<header>This header text</header>
<body>This is body text.</body>
</root>
Run Code Online (Sandbox Code Playgroud)然后添加另一个模板来body区别对待
<?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>
<!-- New template for body element -->
<xsl:template match="body">
<!-- Copy body as-is like in the default identity template -->
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<!-- Do something new here: Add a footer element -->
<footer>This is new footer text</footer>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)并运行新的XSLT以生成包含页脚的新XML输出:
<root>
<header>This header text</header>
<body>This is body text.</body>
<footer>This is new footer text</footer>
</root>
Run Code Online (Sandbox Code Playgroud)推荐的XSLT资源