如何使用 XPATH 从 XML 文档中选择不同的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2871707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:06:39  来源:igfitidea点击:

How to select distinct values from XML document using XPATH?

xmlxsltxpath

提问by Alex

How can I select only distinct elements for the XML document using XPATH?I've tried to use the 'distinct-values' function but it didn't work for some reason..

如何使用 XPATH 只为 XML 文档选择不同的元素?我尝试使用“distinct-values”函数,但由于某种原因它不起作用。

The XML is similar to the following:

XML 类似于以下内容:

<catalog>

<product>
<size>12</size>
<price>1000</price>
<rating>1</rating>
</product>

<product>
<size>10</size>
<price>1000</price>
<rating>1</rating>
<year>2010</year>
</product>

</catalog>

So what I want to get is the list of distinct children of all the product elements.In the given example it would be - size,price,rating,year My xpath was something like : distinct-values(catalog/product/*)

所以我想要的是所有产品元素的不同子元素的列表。在给定的示例中,它是 - 大小、价格、评级、年份我的 xpath 类似于:distinct-values(catalog/product/*)

回答by Dimitre Novatchev

In XPath 2.0:

在 XPath 2.0 中

distinct-values(/*/*/*/name(.))

In XPath 1.0 this cannot be produced with a single XPath expression.

在 XPath 1.0 中,这不能用单个 XPath 表达式生成

Using XSLT 1.0:

使用 XSLT 1.0

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

 <xsl:template match="/">
   <xsl:for-each select=
   "/*/*/*[not(../following::*/*
                       [not(name() = name(current()))]
               )
           ]">
     <xsl:value-of select="concat(name(), ' ')"/>
   </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document, the wanted result is produced:

当此转换应用于提供的 XML 文档时,会产生所需的结果

size price rating year

A more efficient XSLT 1.0 transformation, using keys:

更高效的 XSLT 1.0 转换,使用 keys

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

 <xsl:key name="kpchildByName"
  match="product/*" use="name()"/>

 <xsl:template match="/">
   <xsl:for-each select=
   "/*/*/*
         [generate-id()
         =
          generate-id(key('kpchildByName', name())[1])
          ]">
     <xsl:value-of select="concat(name(), ' ')"/>
   </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

回答by 341008

distinct-values()is available in XPath 2.0. Are you using that?

distinct-values()在 XPath 2.0 中可用。你用那个?

If distinct-values()is not available, the standard way of getting distinct values is to use not(@result = preceding:: @result)to get unique @result. It will give you the first occurrence only.

如果distinct-values()不可用,则获取不同值的标准方法是使用not(@result = preceding:: @result)获取唯一的 @result。它只会给你第一次出现。

回答by Chris Wallace

You need the distinct values of the element names - something like:

您需要元素名称的不同值 - 例如:

distinct-values($catalog/product/*/name(.))