首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XPath -如果属性值存在,则要求它的值

XPath -如果属性值存在,则要求它的值
EN

Stack Overflow用户
提问于 2016-09-08 16:20:19
回答 2查看 1.4K关注 0票数 0

我的目标是遍历XML文件(内存中的DOM对象),删除包含给定属性但不包含特定值的所有元素。因此,我希望返回一个xpath,该xpath将标识所有这些元素,以便在本例中通过php删除。

具有代表性的XML布局:

代码语言:javascript
复制
<root>
    <pages>
        <page required_distribution="customers, internal, vendors">
            <id>ID of page</id>
            <name>Name of page with limited scope</name>
            <more>more stuff</more>
        </page>
        <page>
            <id>ID of next page</id>
            <name>Name of next page which has unlimited scope</name>
            <more>More stuff, other elements, etc.</more>
        </page>
    </pages>
    <buttons>
        <button>
            <id>button ID</id>
            <text>button text</text>
        </button>
        <button required_distribution="customers, vendors">
            <id>button ID with limited distribution</id>
            <text>button text</text>
        </button>
    </buttons>
    <innerhtmlblocks>
        <!-- Represents elements that are inner html and pulled in directly 
            without additional XSLT parsing, except to remove the control attribute -->
        <innerhtmlblock id="blockid">
            This is a content page, wherein there is innerhtml such as
            <img src="./image.png" /> images and other elements can be
            included in free form. Theoretically, though, I want to be
            able to show certain
            <div required_distribution="internal">
                content only to certain versions.
            </div>
            <div required_distribution="vendor, customers">
                content that varies by version.
            </div>
        </innerhtmlblock>
    </innerhtmlblocks>
</root>

XSLT将提供从XML到HTML的转换;我希望在XSLT发生之前对元素进行过滤,以便通过选择并删除所有不满足我的需求的节点,得到结果XML,并具有虚构的“内部”分布。

代码语言:javascript
复制
<root>
    <pages>
        <page required_distribution="customers, internal, vendors">
            <id>ID of page</name>
            <name>Name of page with limited scope</name>
            <more>more stuff</more>
        </page>
        <page>
            <id>ID of next page</id>
            <name>Name of next page which has unlimited scope</name>
            <more>More stuff, other elements, etc.</more>
        <page>
    </pages>
    <buttons>
        <button>
            <id>button ID</id>
            <text>button text</text>
        </button>
    </buttons>
    <innerhtmlblocks>
        <!-- Represents elements that are inner html and pulled in directly 
            without additional XSLT parsing, except to remove the control attribute -->
        <innerhtmlblock id="blockid">
            This is a content page, wherein there is innerhtml such as
            <img src="./image.png" /> images and other elements can be
            included in free form. Theoretically, though, I want to be
            able to show certain
            <div required_distribution="internal">
                content only to certain versions.
            </div>
    </innerhtmlblocks>
</root>

在这个实例中,应该检查所有具有@required_distribution的元素,如果没有出现$requiredval (“内部”),则应该删除该节点。

我最接近的想法(堆栈交换的过程)是:

代码语言:javascript
复制
//*[@required_distribution and not(contains(@required_distribution,$requiredval))]

我也试过

代码语言:javascript
复制
//*[@required_distribution]/[contains(@required_distribution,$requiredval)]

代码语言:javascript
复制
//*[@required_distribution]/@required_distribution[contains(string(),$requiredval]

但没有结果。我也尝试过节点()、self::等的变体,但是这些都是同样没有成果的(而且可能编写得很糟糕,以致于发布它们毫无帮助)。

一旦我这样做了,我将使用XPath删除控件属性,这是我所知道的唯一工作:

代码语言:javascript
复制
//*[@required_distribution]

总之,我的问题是如何选择所有存在给定属性但不包含给定字符串的元素?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-08 17:04:59

在内部后面包括逗号

代码语言:javascript
复制
//*[@required_distribution and not(contains(@required_distribution, 'internal,'))]

属性值由空格分隔,而不是逗号分隔。这就是为什么contains找不到“内部”的原因。

票数 0
EN

Stack Overflow用户

发布于 2016-09-08 17:03:33

下列措施应能发挥作用:

//*[@required_distribution and not(contains(@required_distribution, 'internal'))]

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39395934

复制
相关文章

相似问题

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