我必须根据条件选择值。如果author name= Bero,它应该显示第一作者的角色,如果Author name= Aurora,它应该显示所有我没有得到预期输出的topics.But。下面是我的XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:regexp="http://exslt.org/regular-expressions"
version="1.0">
<xsl:template match="/">
<xsl:variable name="Store">
<Bookstore>
<Author name="Bero">
<topic>Athletics</topic>
</Author>
<Author name="ABC">
<topic>Sports</topic>
</Author>
</Bookstore>
</xsl:variable>
<xsl:for-each select="$Store/Bookstore/Author">
<xsl:choose>
<xsl:when test="contains($Author ='Bero')">
<xsl:variable name="Store" select="string($Store/Bookstore/Author[@name='Aurora']/u/text())"/>
</xsl:when>
<xsl:when test="contains($Author ='Aurora')">
<xsl:variable name="Store" select="string($Store/Bookstore/Author[@name='Aurora'and @name='Bero']/u/text())"/>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>执行第一个测试用例时预期的部分Output1:
<topic>Athletics</topic>预期的部分Output2:
<topic>Athletics</topic>
<topic>Sports</topic>发布于 2020-12-08 21:20:31
您可以像在过程化语言中一样处理变量。例如,查看xslt variable scope and its usage可以更好地理解为什么这不起作用。
你犯了两个基本错误:
xsl:for-each是一个函数映射,而不是循环。可以将其视为并行处理所有选定的项。处理一个项目的方式不会对后续项目的处理方式产生任何影响xsl:variable是一个变量绑定,而不是赋值。每个xsl:variable指令创建一个新的变量,它不会修改其他现有变量的值,即使它们具有相同的名称。当xsl:when中只有xsl:variable指令时,它就不起作用了,因为新变量一创建就消失了。任何一本好的XSLT教科书都会为您解释这些概念。
此外,条件<xsl:when test="contains($Author ='Bero')">没有任何意义。没有名为$Author的变量,如果有,contains()函数需要两个字符串值参数,而您只提供了一个布尔参数。
https://stackoverflow.com/questions/65198633
复制相似问题