XSLT 如何 select 节点属性值与 xsl:for-each 和 xsl:if
XSLT How to select node attribute value with xsl:for-each and xsl:if
我有一个 XML 文件要用 XSLT 1.0 呈现。
我的cas如下:
If value of longName
is "Oui_Combi"
$Publication
= prodDate
value (21.11.2018)
其他
$Publication
= productionDates
value (17.11.2018, 21.11.2018)
我不确定我的语法在 xsl-if
上的 for-each and/or 是否正确
XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<Manuscript dateAndTime="2018-11-16T10:20:59.177+01:00" renderEPS="" forMediation="false">
<Order>
<OrderHeader productionDates="17.11.2018, 21.11.2018">
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<Criterion name="TypePublicity" ordering="1">
TypePubOccasionnel <ValueParameter longName="Occasionnel"/>
</Criterion>
<Criterion name="JuraCombi" ordering="2">
Oui_Combi <ValueParameter longName="Oui_Combi"/>
^^^^^^^^^^^^^^^^^^^^
</Criterion>
</OrderHeader>
<FirstIssueData prodDate="21.11.2018"></FirstIssueData>
</Order> ^^^^^^^^^^^^^^^^^^^^^
</Manuscript>
我创建变量的 XSL 部分:
<xsl:for-each select="Order/OrderHeader/Criterion">
<xsl:if test="@name = 'JuraCombi'">
<xsl:if test="ValueParameter/@longName = 'Oui_Combi'">
<xsl:variable name="JuraCombi">
OUI
</xsl:variable>
<xsl:variable name="Publication">
<xsl:value-of select="/Order/FirstIssueData/@prodDate" />
</xsl:variable>
</xsl:if>
<xsl:if test="ValueParameter/@longName != 'Oui_Combi'">
<xsl:variable name="JuraCombi">
NON
</xsl:variable>
<xsl:variable name="Publication">
<xsl:value-of select="/Order/OrderHeader/@productionDates" />
</xsl:variable>
</xsl:if>
</xsl:if>
</xsl:for-each>
...
我要打印变量的 XSL 部分 $Publication
:
<xsl:template match="OrderHeader">
<fo:table
table-layout="fixed"
width="18cm"
font-size="10pt"
padding="1.0pt">
<fo:table-column column-width="30mm"/>
<fo:table-column column-width="60mm"/>
<fo:table-column column-width="30mm"/>
<fo:table-column column-width="60mm"/>
<fo:table-body>
...
<fo:table-row>
<fo:table-cell>
<fo:block padding="0.5cm">
Parutions
</fo:block>
</fo:table-cell>
<fo:table-cell number-columns-spanned="3">
<fo:block padding="0.5cm">
<xsl:copy-of select="$Publication" />
</fo:block> ^^^^^^^^^^^^
</fo:table-cell>
</fo:table-row>
...
</fo:table-body>
</fo:table>
</xsl:template>
也许我们可以不用 for-each 实现它?
没有任何在线 XSLT tool/checker 可以让我得到详细的错误,但渲染停止了!
感谢帮助
XSLT 变量的范围仅限于包含 xsl:variable
的元素,并且仅适用于出现在 xsl:variable
之后的表达式。即,如 XSLT 1.0 规范中所说的 "the binding is visible for all following siblings and their descendants"(参见 https://www.w3.org/TR/1999/REC-xslt-19991116#local-variables)。
因此您的变量超出了声明它们的 xsl:if
的范围。
要做的事情是将你的逻辑从里到外翻转,并将条件逻辑放入每个 xsl:variable
元素中。像(未经测试):
<xsl:variable name="JuraCombi">
<xsl:choose>
<xsl:when test="ValueParameter/@longName = 'Oui_Combi'">OUI</xsl:when>
<xsl:otherwise>NON</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="Publication">
<xsl:choose>
<xsl:when test="ValueParameter/@longName = 'Oui_Combi'">
<xsl:value-of select="/Manuscript/Order/FirstIssueData/@prodDate" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="../@productionDates" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
请注意,您 select @prodDate
的 XPath 不会 select 任何东西。它需要 /Manuscript
步骤,因为 Manuscript
是文档元素。
不清楚您声明变量的代码如何与创建 fo:table
的模板相关联。对于能够应用的变量值,您需要让 xsl:apply-templates
that selects OrderHeader
在变量声明的范围内,并且您需要使用 xsl:with-param
和 xsl:param
将变量的值传递给模板。参见 https://www.w3.org/TR/1999/REC-xslt-19991116#section-Passing-Parameters-to-Templates。
我不确定我是否明白你在评论中的意思,所以这是我凭直觉得出的最佳解决方案:
<xsl:template match="Order">
<xsl:variable name="JuraCombi">
<xsl:choose>
<xsl:when test="OrderHeader/Criterion[@name = 'JuraCombi']/ValueParameter/@longName = 'Oui_Combi'">OUI</xsl:when>
<xsl:otherwise>NON</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="Publication">
<xsl:choose>
<xsl:when test="$JuraCombi = 'OUI'">
<xsl:value-of select="FirstIssueData/@prodDate" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="OrderHeader/@productionDates" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<fo:table
table-layout="fixed"
width="18cm"
font-size="10pt"
padding="1.0pt">
<fo:table-column column-width="30mm"/>
<fo:table-column column-width="60mm"/>
<fo:table-column column-width="30mm"/>
<fo:table-column column-width="60mm"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell>
<fo:block padding="0.5cm">
Parutions
</fo:block>
</fo:table-cell>
<fo:table-cell number-columns-spanned="3">
<fo:block padding="0.5cm">
<xsl:copy-of select="$Publication" />
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</xsl:template>
我有一个 XML 文件要用 XSLT 1.0 呈现。
我的cas如下:
If value of
longName
is "Oui_Combi"
$Publication
=prodDate
value (21.11.2018)其他
$Publication
=productionDates
value (17.11.2018, 21.11.2018)
我不确定我的语法在 xsl-if
上的 for-each and/or 是否正确XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<Manuscript dateAndTime="2018-11-16T10:20:59.177+01:00" renderEPS="" forMediation="false">
<Order>
<OrderHeader productionDates="17.11.2018, 21.11.2018">
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
<Criterion name="TypePublicity" ordering="1">
TypePubOccasionnel <ValueParameter longName="Occasionnel"/>
</Criterion>
<Criterion name="JuraCombi" ordering="2">
Oui_Combi <ValueParameter longName="Oui_Combi"/>
^^^^^^^^^^^^^^^^^^^^
</Criterion>
</OrderHeader>
<FirstIssueData prodDate="21.11.2018"></FirstIssueData>
</Order> ^^^^^^^^^^^^^^^^^^^^^
</Manuscript>
我创建变量的 XSL 部分:
<xsl:for-each select="Order/OrderHeader/Criterion">
<xsl:if test="@name = 'JuraCombi'">
<xsl:if test="ValueParameter/@longName = 'Oui_Combi'">
<xsl:variable name="JuraCombi">
OUI
</xsl:variable>
<xsl:variable name="Publication">
<xsl:value-of select="/Order/FirstIssueData/@prodDate" />
</xsl:variable>
</xsl:if>
<xsl:if test="ValueParameter/@longName != 'Oui_Combi'">
<xsl:variable name="JuraCombi">
NON
</xsl:variable>
<xsl:variable name="Publication">
<xsl:value-of select="/Order/OrderHeader/@productionDates" />
</xsl:variable>
</xsl:if>
</xsl:if>
</xsl:for-each>
...
我要打印变量的 XSL 部分 $Publication
:
<xsl:template match="OrderHeader">
<fo:table
table-layout="fixed"
width="18cm"
font-size="10pt"
padding="1.0pt">
<fo:table-column column-width="30mm"/>
<fo:table-column column-width="60mm"/>
<fo:table-column column-width="30mm"/>
<fo:table-column column-width="60mm"/>
<fo:table-body>
...
<fo:table-row>
<fo:table-cell>
<fo:block padding="0.5cm">
Parutions
</fo:block>
</fo:table-cell>
<fo:table-cell number-columns-spanned="3">
<fo:block padding="0.5cm">
<xsl:copy-of select="$Publication" />
</fo:block> ^^^^^^^^^^^^
</fo:table-cell>
</fo:table-row>
...
</fo:table-body>
</fo:table>
</xsl:template>
也许我们可以不用 for-each 实现它?
没有任何在线 XSLT tool/checker 可以让我得到详细的错误,但渲染停止了!
感谢帮助
XSLT 变量的范围仅限于包含 xsl:variable
的元素,并且仅适用于出现在 xsl:variable
之后的表达式。即,如 XSLT 1.0 规范中所说的 "the binding is visible for all following siblings and their descendants"(参见 https://www.w3.org/TR/1999/REC-xslt-19991116#local-variables)。
因此您的变量超出了声明它们的 xsl:if
的范围。
要做的事情是将你的逻辑从里到外翻转,并将条件逻辑放入每个 xsl:variable
元素中。像(未经测试):
<xsl:variable name="JuraCombi">
<xsl:choose>
<xsl:when test="ValueParameter/@longName = 'Oui_Combi'">OUI</xsl:when>
<xsl:otherwise>NON</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="Publication">
<xsl:choose>
<xsl:when test="ValueParameter/@longName = 'Oui_Combi'">
<xsl:value-of select="/Manuscript/Order/FirstIssueData/@prodDate" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="../@productionDates" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
请注意,您 select @prodDate
的 XPath 不会 select 任何东西。它需要 /Manuscript
步骤,因为 Manuscript
是文档元素。
不清楚您声明变量的代码如何与创建 fo:table
的模板相关联。对于能够应用的变量值,您需要让 xsl:apply-templates
that selects OrderHeader
在变量声明的范围内,并且您需要使用 xsl:with-param
和 xsl:param
将变量的值传递给模板。参见 https://www.w3.org/TR/1999/REC-xslt-19991116#section-Passing-Parameters-to-Templates。
我不确定我是否明白你在评论中的意思,所以这是我凭直觉得出的最佳解决方案:
<xsl:template match="Order">
<xsl:variable name="JuraCombi">
<xsl:choose>
<xsl:when test="OrderHeader/Criterion[@name = 'JuraCombi']/ValueParameter/@longName = 'Oui_Combi'">OUI</xsl:when>
<xsl:otherwise>NON</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="Publication">
<xsl:choose>
<xsl:when test="$JuraCombi = 'OUI'">
<xsl:value-of select="FirstIssueData/@prodDate" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="OrderHeader/@productionDates" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<fo:table
table-layout="fixed"
width="18cm"
font-size="10pt"
padding="1.0pt">
<fo:table-column column-width="30mm"/>
<fo:table-column column-width="60mm"/>
<fo:table-column column-width="30mm"/>
<fo:table-column column-width="60mm"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell>
<fo:block padding="0.5cm">
Parutions
</fo:block>
</fo:table-cell>
<fo:table-cell number-columns-spanned="3">
<fo:block padding="0.5cm">
<xsl:copy-of select="$Publication" />
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</xsl:template>