gpt4 book ai didi

XSLT 无法从函数内查询输入 XML

转载 作者:行者123 更新时间:2023-12-01 15:32:00 25 4
gpt4 key购买 nike

我正在尝试创建一个函数,它接受一辆婴儿车,然后根据输入以多种方式查询输入 XML。我的问题是,当我在函数中尝试查询输入 xml 并将值存储在变量中时,出现错误:

'/'无法选择包含上下文项的树的根节点:上下文项不存在

如何从函数中查询 XML?下面是 XSLT

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:lang="info:lc/xmlns/codelist-v1"
xmlns:foo="http://whatever">

<xsl:output indent="yes" />

<xsl:function name="foo:get-prefered">
<xsl:param name="field-name"/>
<xsl:variable name="var1" select="sources/source[@type='A']/name" />
</xsl:function>

<xsl:template match="/">
<xsl:value-of select="foo:get-prefered(10)"></xsl:value-of>
</xsl:template>
</xsl:stylesheet>

最佳答案

这是根据 W3C specification它指出...

Within the body of a stylesheet function, the focus is initially undefined; this means that any attempt to reference the context item, context position, or context size is a non-recoverable dynamic error.

解决方法是传入一个节点(比如根节​​点)作为参数

<xsl:function name="foo:get-prefered">
<xsl:param name="root"/>
<xsl:param name="field-name"/>
<xsl:variable name="var1" select="$root/sources/source[@type='A']/name"></xsl:variable>
<xsl:value-of select="$var1" />
</xsl:function>

<xsl:template match="/">
<xsl:value-of select="foo:get-prefered(/, 10)"></xsl:value-of>
</xsl:template>

或者,您可以考虑使用命名模板而不是函数,也许:

<xsl:template name="get-prefered">
<xsl:param name="field-name"/>
<xsl:variable name="var1" select="sources/source[@type='A']/name"></xsl:variable>
<xsl:value-of select="$var1" />
</xsl:template>

<xsl:template match="/">
<xsl:call-template name="get-prefered">
<xsl:with-param name="field-name">10</xsl:with-param>
</xsl:call-template>
</xsl:template>

关于XSLT 无法从函数内查询输入 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25337034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com