如何在xslt中使用xpath作为变量? [英] How to use xpath for variable in xslt?

查看:24
本文介绍了如何在xslt中使用xpath作为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个变量 $myVar 等于:

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>

如何在 xslt 中为这个变量使用 xpath?

How I can use xpath for this variable in xslt?

就像以某种方式在 /ResultSet/Row[1]/Cell 中获取值

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

推荐答案

如果你的变量是这样形成的

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>

那么你的 xpath 应该是这样的

Then your xpath should look like this

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

您的变量站立"在 ResultSet 节点上,因此您不想使用 $myVar/ResultSet,因为在该级别上只有 Row 节点.请注意,您需要将变量设置为元素(as"属性).如果您的命名空间是相关的,并且某些变量具有与其他变量不同的默认命名空间,您可以像这样为这个 xpath 表达式设置默认命名空间

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

您还可以为整个样式表设置默认命名空间.如果你想忽略命名空间,你可以像这样使用 xpath

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

这篇关于如何在xslt中使用xpath作为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆