开发者

xslt comma separate list of values

I am creating a summary view of Microsoft InfoPath form(s) using a custom XSLT Stylesheet.

I have a selection of the radio buttons on the form which are clicked "Yes", "No"

e.g.

 What is the primary construction of the building?

 Steel  Yes
        No

 Timber Yes
        NO

 etc....

In the stylesheet I have

          <tr>
            <td>
              The primary contruction of the building is
              <xsl:choose>
                <xsl:when test="/my:myFields/my:BrickPrimaryConstruction= 'true'">
                  Brick
                </xsl:when>
              </xsl:choose>
              <xsl:choose>
                <xsl:when test="/my:myFields/my:TimberPrimaryConstruction  = 'true'">
                  Timber Framed
                </xsl:when>
              </xsl:choose>
              <xsl:choose>
                <xsl:when test="/my:myFields/my:ConcretePrimaryConstruction  = 'true'">
                  Concrete Framed
                </xsl:when>
              </xsl:choose>
              et开发者_如何转开发c....

What I want to achieve in the final HTML output is something like:

The primary construction of the building is Brick, Concrete Framed, Prefabricated

I have not got much experience of XSLT, but what is the best way of achieving this?

Avoiding this:

, Concrete Framed, Prefabricated,

or

  Brick, , Prefabricated

Normally in C# I would assign to a string and check if it is empty before appending a comma and then trim commas on the ends however I know that I cannot assign variables in xslt.

EDIT

I also mentioned that I wanted to be able to reuse the function in other situations such as

              <xsl:value-of select="/my:myFields/my:Road"/>,
              <xsl:value-of select="/my:myFields/my:District"/>,
              <xsl:value-of select="/my:myFields/my:City"/>,
              <xsl:value-of select="/my:myFields/my:County"/>,
              <xsl:value-of select="/my:myFields/my:Postcode"/>

where these would be separated by comma or a new line character, but there is the possibility that the "District" for example might be blank resulting in "Road, , City" etc.


Try this:

<tr>
    <td>
        The primary contruction of the building is
        <xsl:for-each select="/my:myFields/my:*[ends-with(local-name(), 'PrimaryConstruction') and (.='true')]">
            <xsl:if test="position()!=1" xml:space="preserve">, </xsl:if>
            <xsl:choose>
                <xsl:when test="self::my:BrickPrimaryConstruction">Brick</xsl:when>
                <xsl:when test="self::my:TimberPrimaryConstruction">Timber Framed</xsl:when>
                <xsl:when test="self::my:ConcretePrimaryConstruction">Concrete Framed</xsl:when>
                etc...
            </xsl:choose>
        </xsl:for-each>

Basically, the for-each loops over all the relevant fields, so that you can check their position and only emit the comma if you're not on the first item (XSLT has a 1-based index). Since the for-each already filters only the relevant entries, we then only have to check the type, not whether the value is true or not.

Note that you can extend the same principle to emit an "and" for the last item instead of a comma if you like to do so.


Maybe look at Implode.

Then, you could do

<xsl:variable name="theItems">
    <xsl:if test="/my:myFields/my:BrickPrimaryConstruction= 'true'">
        <foo>Brick</foo>
    </xsl:if>
</xsl:variable>
<xsl:call-template name="implode">
    <xsl:with-param name="items" select="exsl:node-set($theItems)" />
</xsl:call-template>

You have to convert $theItems, which is a xml fragment, to a node-set. This can only be archived using non-standart methods. XML.com shows some methods to archive it using various XML processors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜