Java 有条件地分配变量以匹配xslt模板中的路径
我想在xslt模板中动态构建匹配。我使用xlsfo、apachefop和saxon9he。我希望以最好的方式从java传递param,但首先我尝试在xslt中设置它 当我创建如下的变量示例时:Java 有条件地分配变量以匹配xslt模板中的路径,java,pdf,xslt,conditional,variable-assignment,Java,Pdf,Xslt,Conditional,Variable Assignment,我想在xslt模板中动态构建匹配。我使用xlsfo、apachefop和saxon9he。我希望以最好的方式从java传递param,但首先我尝试在xslt中设置它 当我创建如下的变量示例时: <xsl:variable name="testPath" select="/abc:Files/def:Description" /> 并在xslt中使用 <xsl:param name="testPath "/> 并在模板中应用: <xsl:apply-temp
<xsl:variable name="testPath" select="/abc:Files/def:Description" />
并在xslt中使用
<xsl:param name="testPath "/>
并在模板中应用:
<xsl:apply-templates select="$testPath/qwe:Test"/>
<xsl:apply-templates select="$testPath/qwe:Test"/>
但我得到以下错误:
在中计算($testPath)时键入错误
xsl:apply templates/@select在第60列第74行
pdf_gen.xsl:XPTY0019:所需的
“/”的第一个操作数是node();提供的值u“/abc:Files/def:Description”是一个原子值
为什么任何解决方案都不起作用?如何实现它?我正在处理一个类似的问题。 在我看来,变量可见性仅在
块中,我可以建议您制作模板块并以这种方式调用它们:
<xsl:choose>
<xsl:when test="$versionNo = '2'" >
<xsl:call-template name="TemplateOne">
<xsl:with-param name="testPath" select="'/abc:Files/def:Description'" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="TemplateTwo">
<xsl:with-param name="testPath" select="'/abc:Files/def:Names'" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
模板定义为:
<xsl:template name="TemplateOne">
<xsl:param name="testPath" />
...
bla bla bla
remember to use variable in this template as "{$testPath}"
...
</xsl:template>
...
呜呜呜呜
记住在这个模板中使用变量“{$testPath}”
...
在使用Saxon 9时,至少要将XSLT 2与XPath 2结合使用,我建议在XPath中使用例如
<xsl:variable name="testPath" select="if ($versionNo = '2') then /abc:Files/def:Description else /abc:Files/def:Names"/>
或者做,例如
<xsl:apply-templates select="(/abc:Files/def:Description[$versionNo = '2'], /abc:Files/def:Names[$versionNo != '2'])/qwe:Test"/>
您使用的是哪个XSLT处理器?我使用xsl fo、apache fop和saxon。第一个错误是由fop处理器引发的,它只是告诉您在XSLT代码中构造的xsl-fo无效,表体
没有所需的子级。因此,要获得这方面的帮助,您需要显示最小但完整的相关代码片段,以便让我们了解您拥有哪些XML、要创建哪些XSL-FO以及当前获得的是哪一个。第二个错误XPTY0019
建议您使用XSLT 2或3处理器,找出它是Saxon 9.8还是9.9,您可以选择使用静态参数和阴影属性,但可能只有s9api。第一种方法在我使用内联声明时效果良好:
,但在choose标记中,即使我在when和other中输入了相同的值,这也不起作用。我想问题是
,但我能用什么来代替它呢?你用的是哪个版本的Saxon 9?是9.8还是9.9?它们实现了XSLT3,您可以在其中执行,例如
,如果确实需要这种方法,还可以使用影子属性
。另一方面,由于在编译之前需要设置静态参数,我不确定您是否可以使用JAXP API设置它们,因此使用Saxon 9的s9api可能更好,甚至更必要。
<xsl:template name="TemplateOne">
<xsl:param name="testPath" />
...
bla bla bla
remember to use variable in this template as "{$testPath}"
...
</xsl:template>
<xsl:variable name="testPath" select="if ($versionNo = '2') then /abc:Files/def:Description else /abc:Files/def:Names"/>
<xsl:apply-templates select="$testPath/qwe:Test"/>
<xsl:apply-templates select="(if ($versionNo = '2') then /abc:Files/def:Description else /abc:Files/def:Names)/qwe:Test"/>
<xsl:apply-templates select="(/abc:Files/def:Description[$versionNo = '2'], /abc:Files/def:Names[$versionNo != '2'])/qwe:Test"/>