简体   繁体   English

XPath:如何选择具有一个或另一个属性的所有元素

[英]XPath: How to select all elements which have one attribute or another

I've been searching everywhere for this and can't find a solution to my problem. 我一直在到处寻找这个问题,但是找不到我的问题的解决方案。 I'm trying to find an XPath expression that will select all elements with an attribute named "user" or "product" (or both). 我试图找到一个XPath表达式,该表达式将选择所有具有名为“用户”或“产品”(或两者)的属性的元素。 I know the XPath expression for one attribute would be : 我知道一个属性的XPath表达式是:

//*[@user]

or 要么

//*[@product]

And these both work fine, they grab all elements with the proper attribute anywhere inside the document. 这些都可以正常工作,它们可以在文档中的任何位置获取具有适当属性的所有元素。 But whenever I try to combine them : 但是每当我尝试结合它们时:

//*[@user|@product]

or 要么

//*[@user]|//*[@product]

I only get the elements found in the first level where these attributes are found. 我只在找到这些属性的第一级中找到元素。 Here's an example of my XML document : 这是我的XML文档的示例:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet href="xslt.xml" type="application/xml"?>
<catalog>
<item user="me" product="coffee" />
<price product="expensive" quality="good">$19.50</price>
<item user="still me"><note product="poison">Do not eat.</note></item>
<price product="mystery"><exchange user="still me" product="euro" />$99.95</price>
</catalog>

Now with this XSLT transformation : 现在通过此XSLT转换:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="//*[@user|@product]">
<xsl:copy />
</xsl:template>
</xsl:stylesheet>

I only get these elements : 我只有这些元素:

<item/>
<price/>
<item/>
<price/>

But what I really want is this : 但是我真正想要的是:

<item/>
<price/>
<item/>
<note/>
<price/>
<exchange/>

Of course, as you might have guessed already, when I put the "user" attribute in my catalog element, all that's selected is the catalog element, no children. 当然,正如您可能已经猜到的那样,当我在目录元素中放置“ user”属性时,选择的只是目录元素,没有子级。

I've been trying for hours and can't find the solution. 我已经尝试了几个小时,却找不到解决方案。 If anyone knows how to fix this, please let me know. 如果有人知道如何解决此问题,请告诉我。

Use or instead of | 使用or代替| . | is a different operator which I frankly do not know the meaning of :-) 是一个不同的运算符,坦率地说,我不知道:-)的含义

Though the answer provided by Jiří Kantor is right, you would have to use the following XSLT to get the desired result: 尽管JiříKantor提供的答案是正确的,但您必须使用以下XSLT才能获得所需的结果:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
    <xsl:apply-templates select="//*[@user or @product]"/>
</xsl:template>

<xsl:template match="*">
    <xsl:copy/>
</xsl:template>

</xsl:stylesheet>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 简单XML-如何选择所有具有特定属性值的元素 - Simple XML - How to select all elements which have a specific attribute value XPath选择具有Xpath 2.0属性的元素的唯一列表 - XPath to select unique list of elements that have a certain attribute with Xpath 2.0 Xpath获取至少具有一个属性的所有元素 - Xpath to get all elements with at least one attribute 使用XPath如何选择缺少属性的元素 - using XPath how to select elements with absent attribute XPath-选择一个名称相同的所有子节点本身都具有特定子节点的节点 - XPath - select nodes for which all child nodes of one name have a particular child node themselves 如何在XPath中选择具有一个属性的特定值和另一个属性的最大值的节点? - How can I select a node with specific value of one attribute and maximum value of another attribute in XPath? 通过XPath选择具有特定属性的子节点的父节点? - Select parent nodes which have a subnode with specific attribute via XPath? XPath / XSLT:如何选择满足涉及另一组元素的条件的所有元素 - XPath/XSLT: how to select all elements that satisfy a condition involving another set of elements 如何通过另一个 XPATH 的所有值过滤属性 - How to filter an attribute by all value of another XPATH XPath-选择具有给定属性的除element(及其子元素)以外的所有元素 - XPath - select all elements except element (and it's subelements) with given attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM