A6784
A6784

Reputation: 19

Using XSL IF to check if the element appears in XML

I have the following XML file:

    <club>
      <person>
        <name>name</name>
        <ranking>1</ranking>
        <nationality>spanish</nationality>
        <joined>2007</joined>
        <joined>2009</joined >
      </person>

      <person>
        <name>name</name>
        <ranking>2</ranking>
        <nationality>english</nationality>
      </person>
    </club>

I would like to create an XSLT file that can search through the XML and check whether the person has joined (<joined>). Then I would want to display the name and the year the person joined in a table. I am not sure if this is entirely possible, but any help would be greatly appreciated. (not all the people on the list will have joined).

Upvotes: 0

Views: 117

Answers (1)

Rupesh_Kr
Rupesh_Kr

Reputation: 3435

Use this:

XSLT 2.0

<xsl:template match="information">
    <table>
        <xsl:for-each select="person[joined]">
            <tr>
                <td><xsl:value-of select="name"/></td>
                <td><xsl:value-of select="joined" separator=", "/></td>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

See transformation for XSLT 2.0 at https://xsltfiddle.liberty-development.net/eiZQaEZ

For XSLT 1.0 you can change second td to

<td><xsl:for-each select="joined">
       <xsl:value-of select="."/>
       <xsl:if test="position() != last()">, </xsl:if>
    </xsl:for-each></td>

Transformation for XSLT 1.0 https://xsltfiddle.liberty-development.net/eiZQaEZ/1

Upvotes: 3

Related Questions