0

I have multiple data items, each with a value V that has multiple tags associated with it--t1,t2...tn. I have an XML file where I choose to store each item and the tags associated with it, e.g.

<root>
<item>
<value>V1</value>
<tag>t11</tag>
<tag>t12</tag>
<tag>t13</tag>
</item>
<item>
<value>V2</value>
<tag>t21</tag>
<tag>t22</tag>
</item>
...
</root>

I want to be able to easily query for values V when I search for tags. How can I do this efficiently without writing entire libraries of code?

Freakishly
  • 1,533
  • 5
  • 32
  • 61

1 Answers1

1

If you have control over your XML, I would restructure it like this:

<root>
    <item value="V1">
        <tag>t11</tag>
        <tag>t12</tag>
        <tag>t13</tag>
    </item>
    <item value="V2">
        <tag>t21</tag>
        <tag>t22</tag>
    </item>
</root>

This way you could simply write an XQuery like this to get all the tags for a given value:

//item[@value="V2"]/tag
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • awesome, can't wait to try it :) – Freakishly Mar 20 '12 at 23:48
  • Justin, is there a way, if I restructure the XML to the way you've suggested, that people can search for a tag, and find all the values associated with it? – Freakishly Aug 24 '12 at 21:10
  • http://stackoverflow.com/questions/3712452/how-to-retrieve-parent-node-using-xquery The top, selected answer shows that you can do an xquery like this: `/root/item/tag[text()='TagLookingFor']/..`, then just grab all the distinct @value's. Also, please remember to upvote and add a checkmark if you feel this answer is correct. – Justin Pihony Aug 26 '12 at 20:52