为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

千巷猫影 2022-05-20 18:41:54
我想在 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" 是一个原子值为什么没有任何解决方案可以实现它?
查看完整描述

2 回答

?
qq_遁去的一_1

TA贡献1725条经验 获得超8个赞

当您使用 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="$testPath/qwe:Test"/>

应该可以很好地处理qwe:Test先前选择的一个def:Description或多个元素的子def:Names元素。

当然也可以使用例如

<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"/>


查看完整回答
反对 回复 2022-05-20
?
拉丁的传说

TA贡献1789条经验 获得超8个赞

我正在处理类似的问题。在我看来,变量可见性仅在<xsl:choose>块内,我可以建议您制作模板块并以这种方式调用它们:


<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>


查看完整回答
反对 回复 2022-05-20
  • 2 回答
  • 0 关注
  • 137 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号