I'd like to select the value of a certain attribute from all tags where another attribute matches a condition.
For example, if my XML document is:
<?xml version="1.0"?>
<doc>
<mytag txt="alpha" foo="a"></mytag>
<mytag txt="beta" foo="b"></mytag>
</doc>
I'd like to use a XSLT file like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="doc/mytag">
|<xsl:value-of select="@txt[@foo='a']"/>|
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
To get output like this:
|alpha|
Instead, nothing is selected.
If I wrap the [xsl:value-of] tag with a [xsl:if test="@foo='a'"] tag, it will work:
...
<xsl:for-each select="doc/mytag">
<xsl:if test="@foo='a'">
|<xsl:value-of select="@txt"/>|
</xsl:if>
</xsl:for-each>
But I'd like to avoid that if possible, simply to save on space since I have ~20 separate tags I'd like to extract.
I'm guessing this is an xpath problem, but after fairly lengthy searching, I haven't found a solution. Any help is appreciated.
@txt[@foo='a']
to[@foo='a']/@txt
– SomeDude