在XSLT转换期间省略未使用的XML元素

时间:2017-01-26 14:21:53

标签: xml xslt

我正在尝试使用XSLT转换XML文档。原始XML文档中的某些元素在我的XSL文档中未使用,但这些元素的值将添加到结果中。我怎么能省略这些元素?例如:

XML

<?xml version="1.0"?>
<data>
    <id>1</id>
    <name>Test</name>
    <description>Test description</description>
</data>

XSL

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="id">
        <id>
            <xsl:apply-templates/>
        </id>
    </xsl:template>
</xsl:stylesheet>

结果

<id>1</id>TestTest description

预期结果

<id>1</id>

1 个答案:

答案 0 :(得分:1)

您看到的是built-in template rules将文本节点复制为默认值的结果。

防止这种情况的最简单方法是更具体 - 例如,执行:

<xsl:template match="/data">
    <xsl:copy-of select="id"/>
</xsl:template>
相关问题