猿问

有条件地分配变量以匹配 xslt 模板中的路径

我想在 xslt 模板中动态构建匹配。我使用 xls fo 和 apache fop 和 saxon9he。最好的方式是我想从 java 传递参数,但首先我尝试在 xslt 中设置它。


当我创建这样的变量示例时:


<xsl:variable name="testPath" select="/abc:Files/def:Description" /> 

如果我尝试在应用模板中使用它也可以正常工作:


<xsl:apply-templates select="$testPath/qwe:Test"/>

但我想动态设置 testPath 变量。我尝试使用选择标签:


<xsl:variable name="testPath"> 

    <xsl:choose>

        <xsl:when test="$versionNo = '2'">

             <xsl:value-of select="/abc:Files/def:Description" />   

        </xsl:when>

        <xsl:otherwise>

             <xsl:value-of select="/abc:Files/def:Names" /> 

        </xsl:otherwise>

    </xsl:choose>

</xsl:variable> 

但是这种方法不起作用,当我尝试使用这个变量时:


<xsl:apply-templates select="$testPath/qwe:Test"/>

我收到此错误:


在 pdf_gen.xsl 的第 82 行第 21 列评估 ((attr{table-layout=...}, ...)) 时出错:SXCH0003: org.apache.fop.fo.ValidationException: "fo:table-body" 是缺少子元素。所需内容模型:marker* (table-row+|table-cell+)(见位置 82:21):

file:/C:/Users/SuperUser/workspace/project/xls-editor/target/classes/pdf/xsl/pdf_gen .xsl:82:21:“fo:table-body”缺少子元素。所需内容模型:marker* (table-row+|table-cell+)(见位置 82:21)


在最佳选择中,我想将 Java 中的 $testPath 变量作为参数传递,例如:


transformer.setParameter("testPath ", "/abc:Files/def:Description");

并在 xslt 中使用


<xsl:param name="testPath "/>

并在模板中应用:


<xsl:apply-templates select="$testPath/qwe:Test"/>

但我收到以下错误:


pdf_gen.xsl 的第 74 行第 60 列 xsl:apply-templates/@select 中的类型错误评估 ($testPath): XPTY0019: '/' 的第一个操作数所需的项目类型是 node(); 提供的值

u"/abc:Files/def:Description" 是一个原子值


为什么没有任何解决方案可以实现它?


千巷猫影
浏览 138回答 2
2回答

qq_遁去的一_1

当您使用 Saxon 9 时,您确实至少使用 XSLT 2 和 XPath 2,我建议在 XPath 中实现变量选择,例如<xsl:variable&nbsp;name="testPath"&nbsp;select="if&nbsp;($versionNo&nbsp;=&nbsp;'2')&nbsp;then&nbsp;/abc:Files/def:Description&nbsp;else&nbsp;/abc:Files/def:Names"/>这样你的<xsl:apply-templates&nbsp;select="$testPath/qwe:Test"/>应该可以很好地处理qwe:Test先前选择的一个def:Description或多个元素的子def:Names元素。当然也可以使用例如<xsl:apply-templates&nbsp;select="(if&nbsp;($versionNo&nbsp;=&nbsp;'2')&nbsp;then&nbsp;/abc:Files/def:Description&nbsp;else&nbsp;/abc:Files/def:Names)/qwe:Test"/>或者做例如<xsl:apply-templates&nbsp;select="(/abc:Files/def:Description[$versionNo&nbsp;=&nbsp;'2'],&nbsp;/abc:Files/def:Names[$versionNo&nbsp;!=&nbsp;'2'])/qwe:Test"/>

拉丁的传说

我正在处理类似的问题。在我看来,变量可见性仅在<xsl:choose>块内,我可以建议您制作模板块并以这种方式调用它们:<xsl:choose>&nbsp; &nbsp; <xsl:when test="$versionNo = '2'" >&nbsp; &nbsp; &nbsp; &nbsp; <xsl:call-template name="TemplateOne">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xsl:with-param name="testPath" select="'/abc:Files/def:Description'" />&nbsp; &nbsp; &nbsp; &nbsp; </xsl:call-template>&nbsp; &nbsp; </xsl:when>&nbsp; &nbsp; <xsl:otherwise>&nbsp; &nbsp; &nbsp; &nbsp; <xsl:call-template name="TemplateTwo">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <xsl:with-param name="testPath" select="'/abc:Files/def:Names'" />&nbsp; &nbsp; &nbsp; &nbsp; </xsl:call-template>&nbsp; &nbsp; </xsl:otherwise></xsl:choose>模板定义为:<xsl:template name="TemplateOne">&nbsp; &nbsp; <xsl:param name="testPath" />&nbsp; &nbsp; ...&nbsp; &nbsp; bla bla bla&nbsp; &nbsp; remember to use variable in this template as "{$testPath}"&nbsp; &nbsp; ...</xsl:template>
随时随地看视频慕课网APP

相关分类

Java
我要回答