Xpath for unique value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • milecimm
    New Member
    • Oct 2007
    • 24

    Xpath for unique value

    Hi

    I have the following xml structure

    CONTRACTS
    --CONTRACT
    ---SUPPLIERS
    ----SUPPLIER
    -----SUPPLIERID
    -----SUPPLIERNAME

    I need to get all unique values for suppliers so I dont get the same supplier more than once. I'm using the following xpath but it's not working:

    Xpath = CONTRACTS/CONTRACT/SUPPLIERS/SUPPLIER[not(SUPPLIERID= preceding-sibling::SUPPLI ER/SUPPLIERID)]")

    And how can I sort by SUPPLIERNAME?

    Many thanks!
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Just use preceding instead. Xpath = CONTRACTS/CONTRACT/SUPPLIERS/SUPPLIER[not(SUPPLIERID= preceding::SUPP LIER/SUPPLIERID)]")

    Are you using xsl? Perhaps:
    <xsl:sort select="SUPPLIE RNAME"/>

    This assumes you don't have any other types of SUPPLIER nodes, and if you did you should be using namespaces anyways.

    Comment

    • milecimm
      New Member
      • Oct 2007
      • 24

      #3
      This works, thank you very much.

      I'm not using xsl. Is there another way?

      Thanks again.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        What are you using? ......

        Comment

        • milecimm
          New Member
          • Oct 2007
          • 24

          #5
          Hi, I managed to solve the problem with the sorting. This is my code:

          My xml is -----> REVISED

          Code:
          CONTRACTS
          --CONTRACT
          ---SUPPLIER
          ---COMMODITIES
          ----COMMODITY
          -----COMODDITYNAME
          
          
                  Dim xdoc As New XPathDocument("testxml.xml")
                  Dim nav As XPathNavigator = xdoc.CreateNavigator()
                  Dim expr As XPathExpression
          
                 
                  expr = nav.Compile("/pf:CONTRACTS/pf:CONTRACT[not(pf:SUPPLIER=preceding::pf:SUPPLIER)]/pf:SUPPLIER")
          
                  Dim namespaceManager As XmlNamespaceManager = New XmlNamespaceManager(nav.NameTable)
                  namespaceManager.AddNamespace("pf", "http://namespace.ac.uk")
                  expr.AddSort(".", XmlSortOrder.Ascending, XmlCaseOrder.None, String.Empty, XmlDataType.Text)
                  expr.SetContext(namespaceManager)
                  Dim nodes As XPathNodeIterator = nav.Select(expr)
          
                  If nodes.Count > 0 Then
          
                      Dim tr As String = Nothing
                     
                      myString.AppendLine("<table width='300px' border='0' cellpadding='0' cellspacing='0' border='0' class='datatable1'>")
                      myString.AppendLine("<th>Supplier name</th>")
          
                      While nodes.MoveNext()
          
                          Dim supplier As XPathNavigator = nodes.Current.SelectSingleNode(".", namespaceManager)
          
                          myString.AppendLine("<tr><td>" & supplier.ToString())
                          myString.AppendLine("</td></tr>")
          
                      End While
          
                      myString.AppendLine("</table>")
          
                      Dim strOutput As String = myString.ToString()
                      lblOutput.Text = strOutput
          
          
                  Else
          
                      lblOutput.Text = "No results for your search"
          
                  End If
          Last edited by jkmyoung; Mar 24 '10, 06:19 PM. Reason: Code tags

          Comment

          Working...