Hi,

I have an xml file that has the same tag name for different categories of data. Below is an example. I would like to create an xpath to extract each data category. For example Age is the 4th tag. So I would need the xpath script to navigate to the 4th <item name> tag so I can extract the value of '21' that is located in the 4th <value> tag.

Any help appreciated.

Thanks

<metadata>
<item name="Incident Number" type="xs:string" length="102"/>
<item name="Chief Complaint" type="xs:string" length="202"/>
<item name="PCR Date/Time" type="xs:dateTime"/>
<item name="Age" type="xs:int" precision="1"/>
<item name="Gender" type="xs:string" length="512"/>
<item name="Race" type="xs:string" length="512"/>
<item name="Onset Time" type="xs:dateTime"/>
<item name="Unit Notified" type="xs:dateTime"/>
<item name="Arrive Scene" type="xs:dateTime"/>
<item name="Event Description" type="xs:string" length="512"/>
<item name="Start Time" type="xs:dateTime"/>
<item name="Arrive Destination" type="xs:dateTime"/>
<item name="Drug Name" type="xs:string" length="202"/>
</metadata>
<data>
<row>
<value>10136254</value>
<value>Chest Pain (Non-Cardiac)</value>
<value>2010-09-18T00:54:12</value>
<value>21</value>
<value>Female</value>
<value>Black or African American</value>
<value>2010-09-11T00:00:00</value>
<value>2010-09-18T00:54:12</value>
<value>2010-09-18T01:00:30</value>
<value>12-Lead 1</value>
<value>2010-09-18T01:08:57</value>
<value>2010-09-18T01:40:56</value>
<value xs:nil="true" />
</row>

I believe the expression would a predicate and look something like this.

/metadata/itemname[4] to get the Age

/data/row/value[4] to get the actual age

Your sample document wasn't a well-formed XML document so I had to change it to test my XPath. The sample document that I used is below.

<doc xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <metadata>
        <item name="Incident Number" type="xs:string" length="102"/>
        <item name="Chief Complaint" type="xs:string" length="202"/>
        <item name="PCR Date/Time" type="xs:dateTime"/>
        <item name="Age" type="xs:int" precision="1"/>
        <item name="Gender" type="xs:string" length="512"/>
        <item name="Race" type="xs:string" length="512"/>
        <item name="Onset Time" type="xs:dateTime"/>
        <item name="Unit Notified" type="xs:dateTime"/>
        <item name="Arrive Scene" type="xs:dateTime"/>
        <item name="Event Description" type="xs:string" length="512"/>
        <item name="Start Time" type="xs:dateTime"/>
        <item name="Arrive Destination" type="xs:dateTime"/>
        <item name="Drug Name" type="xs:string" length="202"/>
    </metadata>
    <data>
        <row>
            <value>10136254</value>
            <value>Chest Pain (Non-Cardiac)</value>
            <value>2010-09-18T00:54:12</value>
            <value>21</value>
            <value>Female</value>
            <value>Black or African American</value>
            <value>2010-09-11T00:00:00</value>
            <value>2010-09-18T00:54:12</value>
            <value>2010-09-18T01:00:30</value>
            <value>12-Lead 1</value>
            <value>2010-09-18T01:08:57</value>
            <value>2010-09-18T01:40:56</value>
            <value xs:nil="true"/>
        </row>
    </data>
</doc>

The following Xpath works this way. Looking inside the predicate, "/doc/metadata/item[@name='Age']/(count(preceding-sibling::*) + 1)" This portion of the Xpath counts the number of nodes that come BEFORE the node that you're looking for then adds 1. So for 'Age', there are (3+1) item nodes that are previous siblings in the item tree. Now, I want the value node whose position() is equal to the nuber that I return from the first part.

/doc/data/row/value[position() = /doc/metadata/item[@name='Age']/(count(preceding-sibling::*) + 1)]

All you have to do is change the name of the item/@name attribute to get the corresponding value that is paired with it. If you're looking for something else let me know.