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>

これは、'、および'、たとえば'A、およびB'を持っていることとは別にうまく機能しているようです。

理想的には、AとBが欲しいのですが、他のものはすべてEG A、B、C、Fを持つことができます

20個の値のドロップダウンがある場合でも同じように適用したいと思います。

ありがとう

JLRish&TimCの回答に関するコメントを編集します。

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

個別のxslt画面がいくつかあるので、上記の3つ以上を選択して、A、A、B、C、A、Bなどの倍数にすることができますが、理想的には、すでに選択されているのは一度だけです。

4

2 に答える 2

2

あなたができることは、テンプレートマッチングを作成することです。最初に選択されたオプション要素を選択します

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

次に、オプション要素に一致するテンプレートがあり、最初に位置に基づいてコンマまたは「and」を出力します

  <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を使用して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 に答える