如何将当前路径存储在xsl中?

发布于 2024-10-07 01:20:56 字数 141 浏览 0 评论 0原文

我想存储当前节点的路径,以便可以在 XSLT 的表达式中重用它。是否可以?

<!-- . into $path? -->    
<xsl:value-of select="$path" />

I would like to store the path of the current node so I can reused it in an expression in XSLT. Is it possible?

<!-- . into $path? -->    
<xsl:value-of select="$path" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

草莓酥 2024-10-14 01:20:56

嗨,我想存储路径
当前节点,以便我可以在中重用它
XSLT 中的表达式。可能吗?

任何给定节点都可以构造一个 XPath 表达式,在计算时,精确选择该节点。事实上,存在多个选择同一节点的 XPath 表达式。

请参阅此答案,了解构造此类的确切 XSLT 代码XPath 表达式

问题在于,在 XSLT 1.0 或 XSLT 2.0 中,无法在同一转换期间计算此 XPath 表达式,除非使用 EXSLT 扩展函数 dyn:evaluate (并且很少有 XSLT 1.0 处理器实现 dyn:evaluate( ))。

使用 指令,可以在 XSLT 中以更简单的方式实现您想要的效果

<xsl:variable name="theNode" select="."/>

此变量可以在其范围内的任何位置引用为 < code>$theNode, 并且可以在应用或调用模板时作为参数传递。

Hi, I would like to store the path of
the current node so I can reused it in
an expression in XSLT. Is it possible?

It is possible for any given node to construct an XPath expression that, when evaluated, selects exactly this node. In fact more than one XPath expression exists that selects the same node.

See this answer for the exact XSLT code that constructs such an XPath expression.

The problem is that this XPath expression cannot be evaluated during the same transformation in XSLT 1.0 or XSLT 2.0, unless the EXSLT extension function dyn:evaluate is used (and very few XSLT 1.0 processors implement dyn:evaluate() ).

What you want can be achieved in an easier way in XSLT using the <xsl:variable> instruction:

<xsl:variable name="theNode" select="."/>

This variable can be referenced anywhere in its scope as $theNode, and can be passed as parameter when applying or calling templates.

天冷不及心凉 2024-10-14 01:20:56

不,这对于普通 XSLT 1.0 是不可能的。没有简单的方法来检索给定节点的 XPath 表达式字符串,并且绝对没有办法评估看起来像 XPath 的字符串就像 XPath 一样。

有一些扩展支持 XPath 表达式的动态计算,但这些扩展并不与每个 XSLT 处理器兼容。

无论如何,如果您提供有关您实际尝试执行的操作的更多详细信息,则可能有另一种方法可以实现。

No, this is not possible with vanilla XSLT 1.0. There is no easy way to retrieve an XPath expression string for a given node, and there is definitely no way to evaluate a string that looks like XPath as if it was XPath.

There are extensions that support dynamic evaluation of XPath expressions, but these are not compatible with every XSLT processor.

In any case, if you provide more detail around what you are actually trying to do, there might be another way to do it.

卸妝后依然美 2024-10-14 01:20:56

正如 @Dimitre 和 @Tomalak 所指出的,我认为在同一转换中获取表示给定节点的 XPath 表达式的字符串,然后选择节点“解析”没有任何价值。 ”这样的字符串。我可以看到在不同的转换中执行这些操作的一些价值。

除此之外,这个样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <xsl:for-each select=".|//node()|//@*">
            <xsl:variable name="vPath">
                <xsl:apply-templates select="." mode="getPath"/>
            </xsl:variable>
            <xsl:value-of select="concat($vPath,'
')"/>
            <xsl:call-template name="select">
                <xsl:with-param name="pPath" select="$vPath"/>
            </xsl:call-template>
            <xsl:text>
</xsl:text>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="/|node()|@*" mode="getPath" name="getPath">
        <xsl:apply-templates select="parent::*" mode="getPath"/>
        <xsl:text>/</xsl:text>
        <xsl:choose>
            <xsl:when test="self::*">
                <xsl:value-of select="concat(name(),'[',
                                             count(preceding-sibling::*
                                                     [name() =
                                                      name(current())]) + 1,
                                             ']')"/>
            </xsl:when>
            <xsl:when test="count(.|../@*)=count(../@*)">
                <xsl:value-of select="concat('@',name())"/>
            </xsl:when>
            <xsl:when test="self::text()">
                <xsl:value-of
                     select="concat('text()[',
                                    count(preceding-sibling::text()) + 1,
                                    ']')"/>
            </xsl:when>
            <xsl:when test="self::comment()">
                <xsl:value-of
                     select="concat('comment()[',
                                    count(preceding-sibling::comment()) + 1,
                                    ']')"/>
            </xsl:when>
            <xsl:when test="self::processing-instruction()">
                <xsl:value-of
                     select="concat('processing-instruction()[',
                                    count(preceding-sibling::
                                             processing-instruction()) + 1,
                                    ']')"/>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="select">
        <xsl:param name="pPath"/>
        <xsl:param name="pContext" select="/"/>
        <xsl:param name="pInstruction" select="'value-of'"/>
        <xsl:variable name="vPosition"
                      select="number(
                                 substring-before(
                                    substring-after($pPath,
                                                    '['),
                                    ']'))"/>
        <xsl:variable name="vTest"
                      select="substring-before(
                                 substring-after($pPath,
                                                 '/'),
                                 '[')"/>
        <xsl:variable name="vPath" select="substring-after($pPath,']')"/>
        <xsl:choose>
            <xsl:when test="$vPath">
                <xsl:call-template name="select">
                    <xsl:with-param name="pPath" select="$vPath"/>
                    <xsl:with-param name="pContext"
                                    select="$pContext/*[name()=$vTest]
                                                          [$vPosition]"/>
                    <xsl:with-param name="pInstruction"
                                    select="$pInstruction"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="vContext"
                              select="$pContext/node()
                                          [self::*[name()=$vTest]|
                                           self::comment()[$vTest='comment()']|
                                           self::text()[$vTest='text()']|
                                           self::processing-instruction()
                                              [$vTest =
                                               'processing-instruction()']]
                                          [$vPosition]|
                                      $pContext[$pPath='/']|
                                      $pContext/@*[name() =
                                                   substring($pPath,3)]
                                                  [not($vTest)]"/>
                <xsl:choose>
                    <xsl:when test="$pInstruction='value-of'">
                        <xsl:value-of select="$vContext"/>
                    </xsl:when>
                    <xsl:when test="$pInstruction='copy-of'">
                        <xsl:copy-of select="$vContext"/>
                    </xsl:when>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

使用这个输入:

<?somePI pseudoAttributes?>
<root>
    <!-- This is a comment -->
    <node attribute="Value">text</node>
</root>

输出:

/
  text 
/processing-instruction()[1]
pseudoAttributes
/root[1]
  text 
/root[1]/comment()[1]
 This is a comment 
/root[1]/node[1]
text
/root[1]/node[1]/@attribute
Value
/root[1]/node[1]/text()[1]
text

As @Dimitre and @Tomalak have point out, I don't think it has some value in the same transformation to obtain a string representing an XPath expression for a given node, and then select the node "parsing" such string. I could see some value in performing those operations in different transformations.

Besides that, this stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes"/>
    <xsl:template match="/">
        <xsl:for-each select=".|//node()|//@*">
            <xsl:variable name="vPath">
                <xsl:apply-templates select="." mode="getPath"/>
            </xsl:variable>
            <xsl:value-of select="concat($vPath,'
')"/>
            <xsl:call-template name="select">
                <xsl:with-param name="pPath" select="$vPath"/>
            </xsl:call-template>
            <xsl:text>
</xsl:text>
        </xsl:for-each>
    </xsl:template>
    <xsl:template match="/|node()|@*" mode="getPath" name="getPath">
        <xsl:apply-templates select="parent::*" mode="getPath"/>
        <xsl:text>/</xsl:text>
        <xsl:choose>
            <xsl:when test="self::*">
                <xsl:value-of select="concat(name(),'[',
                                             count(preceding-sibling::*
                                                     [name() =
                                                      name(current())]) + 1,
                                             ']')"/>
            </xsl:when>
            <xsl:when test="count(.|../@*)=count(../@*)">
                <xsl:value-of select="concat('@',name())"/>
            </xsl:when>
            <xsl:when test="self::text()">
                <xsl:value-of
                     select="concat('text()[',
                                    count(preceding-sibling::text()) + 1,
                                    ']')"/>
            </xsl:when>
            <xsl:when test="self::comment()">
                <xsl:value-of
                     select="concat('comment()[',
                                    count(preceding-sibling::comment()) + 1,
                                    ']')"/>
            </xsl:when>
            <xsl:when test="self::processing-instruction()">
                <xsl:value-of
                     select="concat('processing-instruction()[',
                                    count(preceding-sibling::
                                             processing-instruction()) + 1,
                                    ']')"/>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="select">
        <xsl:param name="pPath"/>
        <xsl:param name="pContext" select="/"/>
        <xsl:param name="pInstruction" select="'value-of'"/>
        <xsl:variable name="vPosition"
                      select="number(
                                 substring-before(
                                    substring-after($pPath,
                                                    '['),
                                    ']'))"/>
        <xsl:variable name="vTest"
                      select="substring-before(
                                 substring-after($pPath,
                                                 '/'),
                                 '[')"/>
        <xsl:variable name="vPath" select="substring-after($pPath,']')"/>
        <xsl:choose>
            <xsl:when test="$vPath">
                <xsl:call-template name="select">
                    <xsl:with-param name="pPath" select="$vPath"/>
                    <xsl:with-param name="pContext"
                                    select="$pContext/*[name()=$vTest]
                                                          [$vPosition]"/>
                    <xsl:with-param name="pInstruction"
                                    select="$pInstruction"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="vContext"
                              select="$pContext/node()
                                          [self::*[name()=$vTest]|
                                           self::comment()[$vTest='comment()']|
                                           self::text()[$vTest='text()']|
                                           self::processing-instruction()
                                              [$vTest =
                                               'processing-instruction()']]
                                          [$vPosition]|
                                      $pContext[$pPath='/']|
                                      $pContext/@*[name() =
                                                   substring($pPath,3)]
                                                  [not($vTest)]"/>
                <xsl:choose>
                    <xsl:when test="$pInstruction='value-of'">
                        <xsl:value-of select="$vContext"/>
                    </xsl:when>
                    <xsl:when test="$pInstruction='copy-of'">
                        <xsl:copy-of select="$vContext"/>
                    </xsl:when>
                </xsl:choose>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

With this input:

<?somePI pseudoAttributes?>
<root>
    <!-- This is a comment -->
    <node attribute="Value">text</node>
</root>

Output:

/
  text 
/processing-instruction()[1]
pseudoAttributes
/root[1]
  text 
/root[1]/comment()[1]
 This is a comment 
/root[1]/node[1]
text
/root[1]/node[1]/@attribute
Value
/root[1]/node[1]/text()[1]
text
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文