我的目标是遍历XML文件(内存中的DOM对象),删除包含给定属性但不包含特定值的所有元素。因此,我希望返回一个xpath,该xpath将标识所有这些元素,以便在本例中通过php删除。
具有代表性的XML布局:
<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,并具有虚构的“内部”分布。
<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 (“内部”),则应该删除该节点。
我最接近的想法(堆栈交换的过程)是:
//*[@required_distribution and not(contains(@required_distribution,$requiredval))]我也试过
//*[@required_distribution]/[contains(@required_distribution,$requiredval)]和
//*[@required_distribution]/@required_distribution[contains(string(),$requiredval]但没有结果。我也尝试过节点()、self::等的变体,但是这些都是同样没有成果的(而且可能编写得很糟糕,以致于发布它们毫无帮助)。
一旦我这样做了,我将使用XPath删除控件属性,这是我所知道的唯一工作:
//*[@required_distribution]总之,我的问题是如何选择所有存在给定属性但不包含给定字符串的元素?
发布于 2016-09-08 17:04:59
在内部后面包括逗号
//*[@required_distribution and not(contains(@required_distribution, 'internal,'))]属性值由空格分隔,而不是逗号分隔。这就是为什么contains找不到“内部”的原因。
发布于 2016-09-08 17:03:33
下列措施应能发挥作用:
//*[@required_distribution and not(contains(@required_distribution, 'internal'))]
https://stackoverflow.com/questions/39395934
复制相似问题