How to select distinct values from XML document using XPATH?

前端 未结 3 699
广开言路
广开言路 2021-01-01 17:00

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..

Th

相关标签:
3条回答
  • 2021-01-01 17:08

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

    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.

    0 讨论(0)
  • 2021-01-01 17:27

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

    distinct-values($catalog/product/*/name(.))
    
    0 讨论(0)
  • 2021-01-01 17:32

    In XPath 2.0:

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

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

    Using 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:

    size price rating year
    

    A more efficient XSLT 1.0 transformation, using 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>
    
    0 讨论(0)
提交回复
热议问题