当我选择具有特定属性值的元素时,仍然选择所有元素。为什么?所以,我应用了一个标准的解决方案,但它不起作用。尝试了许多替代方案都没有成功。是的,XLST对我来说是很新的。
的XLST:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/company/staff[@attrib = 'select']">
<parent>
<header>Some inserted text</header>
<xsl:copy-of select="staff"/>
</parent>
</xsl:template>
</xsl:stylesheet>
示例XML文件:
<?xml version="1.0" encoding="utf-8"?>
<company>
<staff attrib="select" id="1001">
<name>should-1</name>
<role>copy-1</role>
</staff>
<staff id="1002">
<name>should-3</name>
<role>not-copy-3</role>
</staff>
<staff attrib="select" id="1003">
<name>should-2</name>
<role>copy-2</role>
</staff>
</company>
在结果中,我看到'staff'元素id="1002"即使它没有特定的属性值。
想要的结果:
<?xml version="1.0" encoding="UTF-8"?><parent><header>Some inserted text</header><company>
<staff attrib="select" id="1001">
<name>should-1</name>
<role>copy-1</role>
</staff>
<staff attrib="select" id="1003">
<name>should-2</name>
<role>copy-2</role>
</staff>
</company></parent>
实际结果:<?xml version="1.0" encoding="UTF-8"?><parent><header>Some inserted text</header><company>
<staff attrib="select" id="1001">
<name>should-1</name>
<role>copy-1</role>
</staff>
<staff id="1002">
<name>should-3</name>
<role>not-copy-3</role>
</staff>
<staff attrib="select" id="1003">
<name>should-2</name>
<role>copy-2</role>
</staff>
</company></parent>
你可以这样做:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/company">
<parent>
<header>Some inserted text</header>
<xsl:copy-of select="staff[@attrib = 'select']"/>
</parent>
</xsl:template>
</xsl:stylesheet>