Reputation: 127
I want to read the value “enquiry” from the attribute “name” in the xml snippet provided below using xslt. Can anyone help me with it? I am totally new to xslt.
Thanks in advance. :)
<?table name="enquiry"?>
<thead>
<row rowsep="1">
<entry colname="col1">
Upvotes: 0
Views: 820
Reputation: 101758
A more general approach would be to define a template like this:
<xsl:template name="GetPIAttribute">
<xsl:param name="pi" />
<xsl:param name="attrName" />
<xsl:variable name="toFind" select="concat(' ', $attrName, '=')"/>
<xsl:variable name="piAdjusted" select="concat(' ', normalize-space($pi))"/>
<xsl:variable name="foundMatch" select="substring-after($piAdjusted, $toFind)" />
<xsl:if test="$foundMatch">
<xsl:variable name="delimiter" select="substring($foundMatch, 1, 1)" />
<xsl:value-of select="substring-before(substring-after($foundMatch, $delimiter), $delimiter)"/>
</xsl:if>
</xsl:template>
Then you could call it to retrieve any pseudo-attribute you wanted, like this:
<xsl:template match="/">
<xsl:call-template name="GetPIAttribute">
<xsl:with-param name="pi" select="/processing-instruction()[name() = 'table']" />
<xsl:with-param name="attrName" select="'name'" />
</xsl:call-template>
</xsl:template>
The benefit with this approach is that it accounts for the value being enclosed in either single or double quotes, and that you can reuse it if you need to extract multiple values.
Upvotes: 2
Reputation: 52888
That's not actually an attribute. That's just the value of a processing instruction.
I think the only way to get the value is through some string manipulation...
<xsl:template match="processing-instruction()[name()='table']">
<xsl:value-of select="substring-before(substring-after(.,'name="'),'"')"/>
</xsl:template>
Upvotes: 0