gpt4 book ai didi

xslt - 在 xslt 中分配变量

转载 作者:行者123 更新时间:2023-12-01 06:51:36 27 4
gpt4 key购买 nike

我有以下xsl

<Root>
<child>
<Book name="Title" value="hailey" />
<Book name="Title" value="After death" />
<Book name="Price" value="100" />
</child>
<child>
<Book name="Title" value="After death" />
<Book name="genre" value="fiction" />
</child>
</Root>

我想遍历“子”节点,如果“标题”出现(至少一次),我想设置一个变量。我在 xslt 中使用以下代码

<xsl:variable name="flag">
<xsl:for-each select="/Root/Child" >
<xsl:for-each select="./Book" >
<xsl:if test="./@name = 'Title'">
<xsl:value-of select="'true'"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>


</xsl:variable>

问题是变量“flag”设置为“truetruetrue”时的值,而我希望它只是“true”。感谢任何帮助

最佳答案

根本不需要迭代或条件指令。只需使用这个单行:

<xsl:variable name="vYourName" select="boolean(/Root/Child/Book[@name='Title'])"/>

对于这个特定的 XML 文档,这可以表示得更短:

<xsl:variable name="vYourName" select="boolean(/*/*/*[@name='Title'])"/>

解释:

这两个定义都将名为 "vYourName" 的变量定义为 true() 恰好当至少一个 Root/Child/Book元素具有 Title 属性。

请注意:

  1. 根据函数的定义 boolean ($ns) 当且仅当节点集 $ns 非空时返回 true。

  2. bool 值 true() 的字符串表示是字符串 "true"

更新:

在评论中,OP 问道:

if there is atleast one occurence, is there a way to assign the "value" of that to the variable?

答案:是的,如果“值”是指第一个 value 属性,请使用:

 <xsl:variable name="vYourName" select="(/*/*/*[@name='Title'])[1]/@value"/>

关于xslt - 在 xslt 中分配变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11082134/

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