Home > OS >  How to include all the sub-elements of a sub-element in xslt?
How to include all the sub-elements of a sub-element in xslt?

Time:11-24

I have such xml code:

<Car no="1" CarID="id_111-111">
            <ProductionD>2001-07-12</ProductionD>
            <MarketValue currency="$">1999.5</MarketValue>
            <VisitedCountries>
                <Country no="1">
                   Poland
                </Country>
                <Country no="2">
                   England
                </Country>
            </VisitedCountries>
            <InspectionDates>
                2011-09-13 2013-08-13
            </InspectionDates>
</Car>

And i want to display all of Car contents in a table. I've came up with this xsl code:

<?xml version="1.0" encoding="UTF-8"?>

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                                  xmlns:xhtml="http://www.w3.org/1999/xhtml">
        <xsl:output method="xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>
<xsl:template match="/">
<html>
<head></head>
<body>
        <table>
                             <xsl:for-each select="CarOwnerships/Cars/Car">
                                <tr>
                                    <td><xsl:value-of select="ProductionD"/></td>
                                    <td><xsl:value-of select="MarketValue"/></td>
                                    <td><xsl:for-each select="VisitedCountries/Country">
                                        <xsl:value-of select="Country"/>
                                    </xsl:for-each></td>
                                    <td><xsl:value-of select="InsuranceID"/></td>
                                    <td><xsl:value-of select="Phone"/></td>
                                </tr>
                            </xsl:for-each>
        </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

But it only shows the first Country, not both. How could this be done?

CodePudding user response:

<xsl:for-each select="VisitedCountries/Country">
   <xsl:value-of select="Country"/>
</xsl:for-each>

should be

<xsl:for-each select="VisitedCountries/Country">
  <xsl:value-of select="."/>
</xsl:for-each>

but with XSLT 2 doing simply <xsl:value-of select="VisitedCountries/Country"/> should suffice, no need for a for-each.

  • Related