Click to See Complete Forum and Search --> : XPATH help


nap0leon
06-10-2008, 11:17 AM
I've inherited an XML file that resembles this:


<ROOT>
<ROW state="AK" product="AAA" range="0TO100" customervalue="HIGH" optin="Y" />
<ROW state="AK" product="AAA" range="0TO100" customertype="MEDIUM" optin="N" />
<ROW state="AK" product="AAA" range="0TO100" customertype="LOW" optin="N" />
<ROW state="AK" product="AAA" range="125TO500" customervalue="HIGH" optin="Y" />
<ROW state="AK" product="AAA" range="125TO500" customertype="MEDIUM" optin="Y" />
<ROW state="AK" product="AAA" range="125TO500" customertype="LOW" optin="N" />
</ROOT>


In classic ASP, I need to be able to grab the value for the attribute "optin" wher the other 4 attributes are satisified. That is:
Give me the value for "value" where state="AK", product="AAA" , range="0TO100" and customervalue="HIGH"

If this were broken out into a better tree-structure with only 1 attribute per node, I could do it something like this:

strXPATH= "//State[@abbr='NC']//Product[@name='AAA']//Range[@range='0TO100']//Customertype[@type='HIGH']/optin"


But I've never messed with one this flat before. How do I search for a single node where multiple attibiutes satisfy my criteria?

Many thanks!

rpgfan3233
06-10-2008, 12:42 PM
//ROW[@state='AK' and @product='AAA' and @range='0TO100']

That will target any ROW elements that match those conditions. From there, you should have a node set available that will allow you to target the @optin node of each node. Change the conditions as necessary. Here is an example XSLT document that can help you to play with it, along with the full XML file:

test.xsl -
<?xml version="1.0" encoding="utf-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:xhtml="http://www.w3.org/1999/xhtml">

<xsl:template match="ROW[@state='AK' and @product='AAA' and @range='0TO100']">
<xhtml:p><xsl:value-of select="@optin"/></xhtml:p>
</xsl:template>

<xsl:template match="ROOT">
<document>
<xsl:apply-templates/>
</document>
</xsl:template>

</xsl:transform>

test.xml -
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<ROOT>
<ROW state="AK" product="AAA" range="0TO100" customervalue="HIGH" optin="Y" />
<ROW state="AK" product="AAA" range="0TO100" customertype="MEDIUM" optin="N" />
<ROW state="AK" product="AAA" range="0TO100" customertype="LOW" optin="N" />
<ROW state="AK" product="AAA" range="125TO500" customervalue="HIGH" optin="Y" />
<ROW state="AK" product="AAA" range="125TO500" customertype="MEDIUM" optin="Y" />
<ROW state="AK" product="AAA" range="125TO500" customertype="LOW" optin="N" />
</ROOT>