0

我将一个 xml 文件传递​​给我的 fo 文件,如下所示:

<?xml version="1.0"?>
    <activityExport>
    <resourceKey>
    <key>monthName</key>
    <value>January</value>
    </resourceKey>

所以如果我直接使用:

<xsl:value-of select="activityExport/resourceKey[key='monthName']/value"/>

我可以在我的 PDF 文件中看到“一月”。

但是,如果我在模板中这样使用它,我有:

<xsl:template name="format-month">
    <xsl:param name="date"/>
    <xsl:param name="month" select="format-number(substring($date,6,2), '##')"/>
    <xsl:param name="format" select="'m'"/>
    <xsl:param name="month-word">
        <xsl:choose>
            <xsl:when test="$month = 1"><xsl:value-of select="activityExport/resourceKey[key='monthName']/value"/>
</xsl:when>

然后我打电话时看不到“一月”:

<xsl:variable name="monthName">
    <xsl:call-template name="format-month">
    <xsl:with-param name="format" select="'M'"/>
    <xsl:with-param name="month" select="@monthValue"/>
    </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="concat($monthName,' ',@yearValue)"/>

我知道我的模板有效,因为如果我有一个静态字符串:

        <xsl:choose>
            <xsl:when test="$month = 1">Januaryyy</xsl:when>

然后我可以看到Januaryyy 很好。

所以模板有效,资源存在,但选择值在 call-template 或 xsl:choose 或 xsl:when test 内不起作用

有什么帮助吗?问候!

4

1 回答 1

2

您的模板可能没问题,只是您是从 XML 中不合适的位置调用它。因此,您用来设置的 XPathmonth-word找不到任何东西——它是一条通向任何东西的路径。

例如,以下 XSLT 样式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="format-month">
    <xsl:param name="date"/>
    <xsl:param name="month" select="format-number(substring($date,6,2), '##')"/>
    <xsl:param name="format" select="'m'"/>
    <xsl:param name="month-word">
      <xsl:choose>
        <xsl:when test="$month = 1">
          <xsl:value-of select="activityExport/resourceKey[key='monthName']/value"/>
        </xsl:when>
      </xsl:choose>
    </xsl:param>

    <xsl:value-of select="$month-word"/>
  </xsl:template>

  <xsl:template match="/">
    <xsl:variable name="monthName">
      <xsl:call-template name="format-month">
        <xsl:with-param name="month" select=" '1' "/>
        <xsl:with-param name="format" select="'M'"/>
      </xsl:call-template>
    </xsl:variable>

    <xsl:value-of select="concat($monthName,' ',@yearValue)"/>
  </xsl:template>

</xsl:stylesheet>

应用于此 XML:

<activityExport>
  <resourceKey>
    <key>monthName</key>
    <value>January</value>
  </resourceKey>
</activityExport>

产生这个输出:

January 

请注意,我已将month模板的参数替换为 value 1。此输入 XML 中的任何元素都没有@monthValue属性(这让我相信您是从不合适的位置调用模板),因此month-word也不会因为xsl:choose.

为了使您的真实输入 XML 工作正常,您可以尝试将 XPath 替换"//activityExport/resourceKey[key='monthName']/value"为双斜杠定义XML 文档中任何位置的路径的位置。activityExport如果只有一个节点,这应该没问题。否则,您将需要制定合适的 XPath。

于 2013-09-09T12:47:24.727 回答