0

我将重新表述整个问题,因为我几乎有了解决方案,而且我的问题构建得很糟糕:

我有代码:

<xsl:for-each select="documentarycredits/documentarycreditdata">
    <xsl:if test="tradetypes/option[@id='tradefinguar']='selected'">Trade Finance Guarantee</xsl:if>
    <xsl:if test="tradetypes/option[@id='tradefinstand']='selected'">Trade Finance Standby Letters of Credit serving as Financial Guarantees</xsl:if>
    <xsl:if test="tradetypes/option[@id='tradefindoc']='selected'">Trade Finance Documentary Credits</xsl:if>
    <xsl:if test="position()!=last()">
    <xsl:text>, </xsl:text>
    </xsl:if>
    <xsl:if test="position()=last()-1">
        <xsl:text> and </xsl:text>
    </xsl:if>
    <xsl:if test="position()=last()">
        <xsl:text></xsl:text>
    </xsl:if>
 </xsl:for-each>

除了具有 ', and' 例如 'A, and B' 之外,它似乎运作良好。

理想情况下,我想要 A 和 B,其他任何东西都可以有 EG A、B、C 和 F

即使我有 20 个值的下拉列表,我也想应用相同的值。

谢谢

编辑评论 JLRish & Tim C 答案:

            <tradetypes>
            <option id="tradefinguar">selected</option>
            <option id="tradefinstand"/>
            <option id="tradefindoc"/>
        </tradetypes>

有许多单独的 xslt 屏幕,因此可以选择上述三个以上的屏幕,这将导致倍数,例如 A、A、B、C、A 和 B,但理想情况下我希望它知道是否有被选中已经只生产一次。

4

2 回答 2

2

你可以做的是让我们进行模板匹配。首先选择已选择的选项元素

<xsl:apply-templates select="tradetypes/option[. = 'selected']"/>

然后你有一个匹配选项元素的模板,你首先根据位置输出逗号或“和”

  <xsl:choose>
     <xsl:when test="position() &gt; 1 and position() = last()"> and </xsl:when>
     <xsl:when test="position() &gt; 1">,</xsl:when>
  </xsl:choose>

什么时候,你有一个简单的xsl:选择输出 A、B 或 C

  <xsl:choose>
     <xsl:when test="@id='tradefinguar'">A</xsl:when>
     <xsl:when test="@id='tradefinstand'">B</xsl:when>
     <xsl:when test="@id='tradefindoc'">C</xsl:when>
  </xsl:choose>

这是完整的 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="/">
      <xsl:apply-templates select="tradetypes/option[. = 'selected']"/>
   </xsl:template>

   <xsl:template match="option">
      <xsl:choose>
         <xsl:when test="position() &gt; 1 and position() = last()"> and </xsl:when>
         <xsl:when test="position() &gt; 1">,</xsl:when>
      </xsl:choose>
      <xsl:choose>
         <xsl:when test="@id='tradefinguar'">A</xsl:when>
         <xsl:when test="@id='tradefinstand'">B</xsl:when>
         <xsl:when test="@id='tradefindoc'">C</xsl:when>
      </xsl:choose>
   </xsl:template>
</xsl:stylesheet>

在以下 XML 上使用时,输出为A

<tradetypes>
   <option id="tradefinguar">selected</option>
   <option id="tradefinstand"/>
   <option id="tradefindoc"/>
</tradetypes>

在以下 XML 上使用时,输出为A and B

<tradetypes>
   <option id="tradefinguar">selected</option>
   <option id="tradefinstand">selected</option>
   <option id="tradefindoc"/>
</tradetypes>

在以下 XML 上使用时,输出为A, B and C

<tradetypes>
   <option id="tradefinguar">selected</option>
   <option id="tradefinstand">selected</option>
   <option id="tradefindoc">selected</option>
</tradetypes>
于 2013-01-21T17:23:40.183 回答
1

离开您更新的 XSLT,我认为以下更改会做到这一点:

  <xsl:for-each select="documentarycredits/documentarycreditdata">
    <xsl:for-each select="tradetypes/option[. = 'selected']">
      <xsl:if test="@id='tradefinguar'">Trade Finance Guarantee</xsl:if>
      <xsl:if test="@id='tradefinstand'">Trade Finance Standby Letters of Credit serving as Financial Guarantees</xsl:if>
      <xsl:if test="@id='tradefindoc'">Trade Finance Documentary Credits</xsl:if>
      <xsl:choose>
        <xsl:when test="position() + 1 = last()">
          <xsl:text> and </xsl:text>
        </xsl:when>
        <xsl:when test="last() > 1 and position() != last()">
          <xsl:text>, </xsl:text>
        </xsl:when>
      </xsl:choose>
    </xsl:for-each>
  </xsl:for-each>
于 2013-01-21T17:52:11.507 回答