2013-01-21 18 views
0

我会改写整个问题,因为我几乎有解决方案,并构建不好我的问题:XSLT置换加逗号,“与”

我的代码:

<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,其他任何东西都可以有E.G. A,B,C和F

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

感谢

编辑对JLRish &添Ç回答评论:

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

有这将导致许多单独的XSLT屏幕所以它可能有不止上述三个可以选择以倍数表示,例如A,A,B,C,A和B,但理想情况下,我希望知道是否已经选择了一个已经生产过的产品。

+0

也许XSL-如果是更好地使用 – topcat3

+1

你能提供你的输入XML的相关部分?你在用什么XSLT处理器? – JLRishe

+0

我已经添加了这个问题。我接近得到它,但需要调整我的想法。 – topcat3

回答

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> 
+1

+1巧妙的解决方案 – JLRishe

+0

绝对是最好的解决方案。这在我的情况 \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t的 \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t的 \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t – topcat3

+0

@TimÇ对不起蒂姆,我可以问一个问题。有人可能会选择上述选项中的5个,因此它可能是A,A,B,B和C.我希望它是这样的,如果它们被选中一次就不会重复它们,并且对于一个特殊的“其他”自由文本框会重复这一点。例如A,B,其他1,其他2和C或其他1,A,其他2和C或类似物。不确定如果可能。 – topcat3

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> 
+0

谢谢。我做了 and哪个工作正常,但你的是整齐的:) – topcat3

+0

对不起JL,我可以再问一个问题。有人可能会选择上述选项中的5个,因此它可能是A,A,B,B和C.我希望它是如此,如果他们被选中,一旦它不会重复他们和一个特殊的“其他”自由文本框,它会重复这一点。例如A,B,其他1,其他2和C或其他1,A,其他2和C或类似物。不确定如果可能。 – topcat3

+1

因此,他们可以多次选择相同的选项?您能否在上面的问题中显示在输入XML中看起来像什么?同时显示如何在输入XML中表示自由文本项目。 – JLRishe