Reputation: 170
I have an xml file:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<rows>
<column account="1" name="balibid" seq="1">1</column>
<column account="1" name="genre" seq="1">Kids</column>
<column account="1" name="ph" seq="1">3</column>
<column account="1" name="ph" seq="1">4</column>
<column account="1" name="ph" seq="1">5</column>
<column account="1" name="ph" seq="1">6</column>
<column account="1" name="pl6" seq="1">6</column>
</rows>
</response>
I need XPath of column name = genre. What should I use?
Upvotes: 2
Views: 1113
Reputation: 243579
Use (for the particular provided XML document):
/*/*/*[@name = 'genre']
XSLT - based verification:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select="/*/*/*[@name = 'genre']"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the provided XML document:
<response>
<rows>
<column account="1" name="balibid" seq="1">1</column>
<column account="1" name="genre" seq="1">Kids</column>
<column account="1" name="ph" seq="1">3</column>
<column account="1" name="ph" seq="1">4</column>
<column account="1" name="ph" seq="1">5</column>
<column account="1" name="ph" seq="1">6</column>
<column account="1" name="pl6" seq="1">6</column>
</rows>
</response>
the XPath expression is evaluated and the selected node is copied to the output:
<column account="1" name="genre" seq="1">Kids</column>
Upvotes: 0
Reputation: 38265
Using //
in XPATH queries leads to unnecessary (and potentially expensive) evaluation, it's how I started (because it's easy), but I'm now trying hard to kick the habit.
Given you've provided a nice, well formed sample. Here are two example XPATH queries:
/response/rows/column[@name="genre"]
And
rows/column[@name="genre"]
Upvotes: 3