首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT -只选择具有特定属性值的元素(但获取全部)

XSLT -只选择具有特定属性值的元素(但获取全部)
EN

Stack Overflow用户
提问于 2022-07-14 06:55:27
回答 1查看 49关注 0票数 0

当我选择具有特定属性值的元素时,仍然会选择所有元素。为什么?所以,我应用了一个标准的解决方案,但它不起作用。尝试了许多备选方案,但都没有成功。是的,XLST对我来说是很新的。

XLST:

代码语言:javascript
复制
<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文件:

代码语言:javascript
复制
<?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‘元素,即使它没有特定的属性值。

通缉结果:

代码语言:javascript
复制
<?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>

实际结果:

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-14 07:49:38

你可以简单地做:

XSLT1.0

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72976402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档