当我选择具有特定属性值的元素时,仍然会选择所有元素。为什么?所以,我应用了一个标准的解决方案,但它不起作用。尝试了许多备选方案,但都没有成功。是的,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>在结果中,我看到了id="1002“的'staff‘元素,即使它没有特定的属性值。
通缉结果:
<?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>发布于 2022-07-14 07:49:38
你可以简单地做:
XSLT1.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>https://stackoverflow.com/questions/72976402
复制相似问题