How to create Xpath query that retrieve value pairs.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #1

    How to create Xpath query that retrieve value pairs.

    Give this XML:
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <linear xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    	<linedata>
    		<line>
    			<rise>253</rise>
    			<run>446</run>
    		</line>
    ...
    </close tags...>
    How do I define an XPath query that will obtain all the "rise"/"run" value pairs under "linedata" in the document?

    Xpath "//rise" or Xpath "//run" will give me the rise and run points in the entire document but if I understand properly the order of the rise run pairs is not guaranteed. I need each rise to have it's matching run or my lines will all be screwed up.
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    What are you using to process the xpath queries? Can you change context and aggregate results, or are you always running the query at the document level?

    I'm not sure if you're confusing this with parallel processing, like that in xslt. I think the order should remain the same, but the machine might find the 3rd match before the 2nd match, before concatenating the results.

    Also: Can you require that your line element always contains both rise and run elements?

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      My line element should always contain both a rise and a run, but I do not know how to require that in the xsd document. I am using some C/C++ COM code to process this and I believe it does have to be done at the document level. I am running the query against the XMLDOMDocument interface.

      I don't know if I can change the context and aggregate the results, my requirement is that for every rise element I have the corresponding run element. And I must have them in the order they are listed in the document.

      Do you have a better idea of how to help me?

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        If you can, do something like:
        Code:
        XMLDocument Doc  = new XMLDocument(load document somehow)
        XMLNodeList lineNodes = Doc.SelectNodes("//Line")
        foreach XMLNode lineNode in lineNodes {
          rise = String2Int(lineNode.SelectSingleNode("rise").value)
          run  = String2Int(lineNode.SelectSingleNode("run").value)
        
          process line with these values.
        }
        If the node doesn't match the schema, then you'll get null errors, which you might have to catch.

        Schema: minOccurs and maxOccurs are by default 1. Just by declaring the elements, they should appear (assuming you're validating the input).

        Code:
        <xs:element name="line" minOccurs="25" maxOccurs="25">
          <xs:complexType>
            <xs:sequence>					
               <xs:element name="rise" type="xs:integer"/>
               <xs:element name="run" type="xs:integer"/>
        ...

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Thanks, you've saved me quite a bit of guess and check.

          Comment

          Working...