gpt4 book ai didi

xslt - 如何根据变量或参数查找元素是否具有祖先(XPath 2.0)

转载 作者:行者123 更新时间:2023-12-03 16:56:35 25 4
gpt4 key购买 nike

我要解决的问题是获取仅包含唯一文本元素的所有元素,但同时也能够根据作为参数传递给XSLT工作表的元素名称排除节点树。

选择仅包含不为空的唯一文本元素的所有节点的部分相对容易:

$StartNode//element()/text()[normalize-space(.)]/parent::*


其中, $StartNode是XML文档中特定元素的Xpath表达式。

现在我遇到了很多问题,从结果中排除了特定的节点树:

所以我希望有这样的东西:

$StartNode//element()/text()[normalize-space(.)]/parent::*[not(ancestor::**$element_to_be_excluded**)]


其中$ element_to_be_excluded是要排除的元素名称。

但是不可能在表达式的那部分使用变量...

所以我想出了这个解决方案

<xsl:variable name="ancestors_of_current_node" as="xs:string*">
<xsl:sequence select="$current_parent_node/ancestor::*/name()"/>
</xsl:variable>
<xsl:variable name="is_value_in_sequence" as="xs:boolean" select="functx:is-value-in-sequence($exclude_node_local_name, $ancestors_of_current_node)"/>
<xsl:if test="not($is_value_in_sequence)">
<xsl:call-template name="dataElementMap">
<xsl:with-param name="node_item" select="$current_parent_node"/>
</xsl:call-template>
</xsl:if>


其中functx:is-value-in-sequence是:

<xsl:function name="functx:is-value-in-sequence" as="xs:boolean" xmlns:functx="http://www.functx.com">
<xsl:param name="value" as="xs:anyAtomicType?"/>
<xsl:param name="seq" as="xs:anyAtomicType*"/>
<xsl:sequence select="$value = $seq"/>
</xsl:function>


现在的问题是,有没有更优雅的方法来解决这个问题?

预先感谢您的任何提示。

问候
弗拉克斯

最佳答案

而不是这样做(正如您提到的那样,由于变量而无效)

$StartNode//element()/text()[normalize-space(.)]
/parent::*[not(ancestor::$element_to_be_excluded)]


您可以改用此方法检查祖先的姓名吗?

$StartNode//element()/text()[normalize-space(.)]
/parent::*[not(ancestor::*[local-name() = $element_to_be_excluded])]

关于xslt - 如何根据变量或参数查找元素是否具有祖先(XPath 2.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11775542/

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