Xml XSLT 2.0在模板之间传输数据
我想知道有可能在模板中传输变量并获得值。例如,在模板A中设置变量,在模板B中获取值? 我尝试使用调用模板,但没有得到值Xml XSLT 2.0在模板之间传输数据,xml,templates,xslt,Xml,Templates,Xslt,我想知道有可能在模板中传输变量并获得值。例如,在模板A中设置变量,在模板B中获取值? 我尝试使用调用模板,但没有得到值 <xsl:template name="Transf"> <xsl:param name="T1"/> <xsl:value-of select="$T1"/> </xsl:template> <xsl:template match='director/filmDirectors'> <xsl:i
<xsl:template name="Transf">
<xsl:param name="T1"/>
<xsl:value-of select="$T1"/>
</xsl:template>
<xsl:template match='director/filmDirectors'>
<xsl:if test="filmDirector='Allen Woody'">
<xsl:call-template name="Transf">
<xsl:value-of select="@id"/><xsl:with-param name="T1" select="@id"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match='movie/titles'>
<xsl:call-template name="Transf">
<xsl:with-param name="T1"><xsl:value-of select="tile[@id=$T1]"/></xsl:with-param>
</xsl:call-template>
</xsl:template>
XML文件
<list>
<director>
<filmDirectors>
<filmDirector id="steve-s">
<lname>Spielberg</lname>
<lfirstname>Steven</lfirstname>
</filmDirector>
<filmDirector id="woody-a">
<lname>Allen<lname>
<lfirstname>Woody</lfirstname>
</filmDirector>
<filmDirectors>
</director>
<movie>
<titles>
<title id="steve-s">Jurassic Park</title>
</titles>
<titles>
<title id="woody-a">Small Time Crooks</title>
</titles>
</movie>
斯皮尔伯格
史蒂文
艾伦
伍迪
侏罗纪公园
小骗子
你能给我一些建议吗
提前感谢您的回复。在不了解XML结构的情况下,很难给出解决方案,但总体思路是使用应用模板而不是调用模板。解决方案可能如下所示
<xsl:template match='director/filmDirectors'>
<xsl:apply-templates>
<xsl:with-param name="filmDirectors" select="." tunnel="yes" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match='movie/titles'>
<xsl:param name="filmDirectors" tunnel="yes" />
<xsl:value-of select="concat('title: ',.,' director: ',$filmDirectors)" />
</xsl:template>
我想你的问题就在这里:
<xsl:template match='movie/titles'>
<xsl:call-template name="Transf">
<xsl:with-param name="T1"><xsl:value-of select="tile[@id=$T1]"/></xsl:with-param>
</xsl:call-template>
</xsl:template>
xsl:if
不能是`xsl:call-template'
我建议您先阅读一本关于XSLT的介绍性书籍,然后再提出充满错误的问题给定的模板无法编译。您不能在调用模板中使用if语句…提供的“xml”“严重畸形。请编辑问题并更正。隧道变量只能在xslt 2.0中使用,对于给定的解决方案不是必需的。谢谢您的回答。但是你能解释一下为什么我们用concat吗?这是不可能的?我只是使用concat函数来格式化包含参数值的输出结果。
<xsl:template match='director/filmDirectors'>
<xsl:call-template name="Transf">
<xsl:if test="filmDirector='Allen Woody'">
<xsl:value-of select="@id"/><xsl:with-param name="T1" select="@id"/>
</xsl:if>
</xsl:call-template>
</xsl:template>