VextoR
VextoR

Reputation: 5165

How to use xpath for variable in xslt?

for example, I have a variable $myVar which equal to:

<ResultSet>
    <Row>
        <Cell name="NEXTVAL" type="NUMBER">475535</Cell>
    </Row>
    <Row>
        <Cell name="NEXTVAL" type="NUMBER">475536</Cell>
    </Row>
    <Row>
        <Cell name="NEXTVAL" type="NUMBER">475537</Cell>
    </Row>
</ResultSet>

How I can use xpath for this variable in xslt?

Like somehow get value in /ResultSet/Row[1]/Cell

Upvotes: 3

Views: 11796

Answers (3)

Sten
Sten

Reputation: 162

If your variable is formed like this

<xsl:variable name="myVar" as="element()">
    <ResultSet>
        <Row>
            <Cell name="NEXTVAL" type="NUMBER">475535</Cell>
        </Row>
        <Row>
            <Cell name="NEXTVAL" type="NUMBER">475536</Cell>
        </Row>
        <Row>
            <Cell name="NEXTVAL" type="NUMBER">475537</Cell>
        </Row>
    </ResultSet>
</xsl:variable>

Then your xpath should look like this

<xsl:sequence select="$myVar/Row[1]/Cell"/>

Your variable is "standing" on ResultSet node so you dont want to use $myVar/ResultSet because on that level there are just Row nodes. Note that you need to set your variable to be element ("as" attribute). If your namespaces are relevant and some variable have different default namespaces from other variables, you can set default namespace just for this xpath expresion like this

<xsl:sequence select="$myVar/Row[1]/Cell" xpath-default-namespace="http://something.com"/>

You can also set default namespace for whole stylesheet. If you want to ignore namespaces you can use xpath like this

<xsl:sequence select="$myVar/*:Row[1]/*:Cell"/>

Upvotes: 2

Pavan
Pavan

Reputation: 1289

First line will match the ResultSet as template ... Next line will display only row1 value

'<xsl:template match="//ResultSet">
 <xsl:value-of select="/ResultSet/Row[1]/Cell"/>'

This single also returns 475535 as output.

 '<xsl:value-of select="/ResultSet/Row[1]/Cell"/> '

Please refer XPATH to learn Xpaths

Let me know this will resolve your problem.

Thanks,

Pavan

Upvotes: -1

VextoR
VextoR

Reputation: 5165

found solution here

<xsl:variable name="author">
  <firstname>Jirka</firstname>
  <surname>Kosek</surname>
  <email>[email protected]</email>
</xsl:variable>

Now let's say we want to extract the e-mail address from the $author variable. The most obvious way is to use an expression such as $author/email. But this will fail, as you can't apply XPath navigation to a variable of the type "result tree fragment."

If we want to get around this limitation, we can use an extension function which is able to convert a result tree fragment back to a node-set. This function is not a part of the XSLT or XPath standards; thus, stylesheets which use it will not be as portable as ones which don't. However, the advantages of node-set() usually outweigh portability issues.

Extension functions always reside in a separate namespace. In order to use them we must declare this namespace as an extension namespace in our stylesheet. The namespace in which the node-set() function is implemented is different for each processor, but fortunately many processors also support EXSLT, so we can use the following declarations at the start of our stylesheet.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:exsl="http://exslt.org/common"
                extension-element-prefixes="exsl"
                version="1.0">
  ...
  <!-- Now we can convert result tree fragment back to node-set -->
  <xsl:value-of select="exsl:node-set($author)/email"/>
  ...
</xsl:stylesheet>

Upvotes: 0

Related Questions