0

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">
3
  • 1
    Now then....Is that really your XML sample? It is not actually well-formed (i.e no closing tags), and more imporantly the table node is a processing-instruction! Do you really mean to have the question marks there? Thanks! Commented Jan 17, 2013 at 9:50
  • @TimC - Good point about the pi most likely being a mistake. I should've noticed that before I posted an answer. Commented Jan 17, 2013 at 9:51
  • @TimC, Actually, I have taken only a small part of the code. yes, it has an end tag and the question marks are also required. Commented Jan 17, 2013 at 9:55

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This won't work when you need the id "attribute" of a PI like <?myPI grid="wide" id="a123"?>; 'wide' would be returned. The select attribute of the foundMatch variable should therefore be substring-after(concat(' ', normalize-space($pi)), concat(' ', $attrName, '=').
0

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=&quot;'),'&quot;')"/>
</xsl:template>

1 Comment

The processing instruction in the xml file is being skipped during the transformation. Is there any way to make this readable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.