469,360 Members | 1,600 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

How to create Xpath query that retrieve value pairs.

RedSon
5,000 Expert 4TB
Give this XML:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <linear xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  3.     <linedata>
  4.         <line>
  5.             <rise>253</rise>
  6.             <run>446</run>
  7.         </line>
  8. ...
  9. </close tags...>
  10.  
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.
Apr 28 '10 #1
4 4283
jkmyoung
2,057 Expert 2GB
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?
Apr 28 '10 #2
RedSon
5,000 Expert 4TB
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?
Apr 28 '10 #3
jkmyoung
2,057 Expert 2GB
If you can, do something like:
Expand|Select|Wrap|Line Numbers
  1. XMLDocument Doc  = new XMLDocument(load document somehow)
  2. XMLNodeList lineNodes = Doc.SelectNodes("//Line")
  3. foreach XMLNode lineNode in lineNodes {
  4.   rise = String2Int(lineNode.SelectSingleNode("rise").value)
  5.   run  = String2Int(lineNode.SelectSingleNode("run").value)
  6.  
  7.   process line with these values.
  8. }
  9.  
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).

Expand|Select|Wrap|Line Numbers
  1. <xs:element name="line" minOccurs="25" maxOccurs="25">
  2.   <xs:complexType>
  3.     <xs:sequence>                    
  4.        <xs:element name="rise" type="xs:integer"/>
  5.        <xs:element name="run" type="xs:integer"/>
  6. ...
  7.  
Apr 28 '10 #4
RedSon
5,000 Expert 4TB
Thanks, you've saved me quite a bit of guess and check.
Apr 28 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Jesper Denmark | last post: by
2 posts views Thread by ree32 | last post: by
4 posts views Thread by johnb41 | last post: by
2 posts views Thread by Jesper Denmark | last post: by
10 posts views Thread by Michael C# | last post: by
reply views Thread by Michael C# | last post: by
4 posts views Thread by cgaden | last post: by
1 post views Thread by Gene Kelley | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.