0

我有一个要使用 XSLT 1.0 呈现的 XML 文件。

我的案例如下:

longName如果值为“Oui_Combi”

$Publication=prodDate价值 ( 21.11.2018 )

别的

$Publication=productionDates价值 ( 17.11.2018, 21.11.2018 )

我不确定我的语法在 for-each 和/或 xsl-if 上是否正确

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 工具/检查器可以让我得到详细的错误,但渲染停止!

感谢帮助

4

1 回答 1

1

XSLT 变量的范围仅限于包含 的元素,xsl:variable并且仅适用于出现在xsl:variable. 即,正如 XSLT 1.0 规范中所说,“绑定对所有后续兄弟姐妹及其后代可见”(请参阅​​ 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>

请注意,您要选择的 XPath@prodDate不会选择任何内容。它需要这/Manuscript一步,因为Manuscript它是文档元素。

目前尚不清楚您用于声明变量的代码与创建fo:table. 要使变量值能够应用,您需要将xsl:apply-templates选择OrderHeader在变量声明的范围内,并且您需要使用xsl:with-paramand将变量的值传递给模板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>
于 2018-11-16T13:13:01.263 回答