Hi I have an xml as follows:
I have 3 variables tablename, fieldname and codevalue
I would like to use XPath to find the value of Description
I am trying to build the expression as below:
strExpression = "/Table={tablename}/field={fieldname}/code={codevalue}/Description]";
<?xml version="1.0" encoding="UTF-8"?>
<root>
<row>
<Table>WaterLine</Table>
<Field>AssetGroup</Field>
<Code>0</Code>
<Description>Unknown</Description>
</row>
<row>
<Table>WaterLine</Table>
<Field>AssetGroup</Field>
<Code>150</Code>
<Description>Water Main</Description>
</row>
<row>
<Table>WaterLine</Table>
<Field>AssetGroup</Field>
<Code>155</Code>
<Description>Service Pipe</Description>
</row>
</root>
1
An XPath expression applying a predicate that would select the Description
element from a row
who’s child elements have certain values:
/root/row[Table='WaterLine' and Field='AssetGroup' and Code='0']/Description
Depending upon what host language or environment you are building and applying the XPath, you could either use placeholders and substitute variable values to construct the XPath or use variable references.
For instance, in XSLT you can use xsl:param
to pass in the parameter value and use it in the XPath statement:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:param name="table"/>
<xsl:param name="field"/>
<xsl:param name="code"/>
<xsl:template match="/">
<xsl:copy-of select="/root/row[Table=$table and Field=$field and Code=$code]/Description"/>
</xsl:template>
</xsl:stylesheet>
7