How to use xpath for variable in xslt?

不羁岁月 提交于 2019-12-12 07:22:22

问题


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


回答1:


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"/>



回答2:


found solution here

<xsl:variable name="author">
  <firstname>Jirka</firstname>
  <surname>Kosek</surname>
  <email>jirka@kosek.cz</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>



回答3:


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



来源:https://stackoverflow.com/questions/14036897/how-to-use-xpath-for-variable-in-xslt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!