millka_15
millka_15

Reputation: 369

How to transform xml via Xslt transform

i have the xml output

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="/topic/get-all.xslt"?>
<List>
    <item>
        <id>8541915a-9098-4d10-bf80-5fbc9a5800af</id>
        <name>TestTopfdic4</name>
        <modifiedDate>2019-12-18T12:37:42.718</modifiedDate>
    </item>
    <item>
        <id>55bc34e6-5cd2-436a-9d37-ceb1052187b0</id>
        <name>TestTopfdic4</name>
        <modifiedDate>2019-12-18T12:40:12.948</modifiedDate>
    </item>
    <item>
        <id>2fee9ce3-1595-4c56-9833-cda03642ad05</id>
        <name>TestTopfdic4</name>
        <modifiedDate>2019-12-18T12:42:15.385</modifiedDate>
    </item>
</List>

And try to tarnsform to html via xslt, I try to select each item and handle it separatly

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="/*">
        <html>
            <body>
                <table align="center">
                    <thead>
                        <th>Id</th>
                        <th>Name</th>
                        <th>modified date</th>
                    </thead>
                    <tbody>
                        <xsl:for-each select="List">
                            <tr>
                                <td><xsl:value-of select="item.id"/></td>
                                <td><xsl:value-of select="item.name"/></td>
                                <td><xsl:value-of select="item.modifiedDate"/></td>
                            </tr>
                        </xsl:for-each>
                    </tbody>

                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

But get empty output, what am I doing wrong? Coult you help me please to solve this problem ?

Upvotes: 0

Views: 36

Answers (2)

Michael Kay
Michael Kay

Reputation: 163635

There's a couple of mistakes here. Firstly, <xsl:for-each select="List"/> iterates over multiple List elements. You only have one List element and you are trying to iterate over its children: that would be <xsl:for-each select="List/*"/>. But then the context item would have to be the root (document) node, not the List element, so you would want match="/" rather than match="/*".

Secondly, you've used "." instead of "/" as your path separator: item.id should be item/id.

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 117165

Your syntax is not XPath syntax. Try:

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="/List">
        <html>
            <body>
                <table align="center">
                    <thead>
                        <th>Id</th>
                        <th>Name</th>
                        <th>modified date</th>
                    </thead>
                    <tbody>
                        <xsl:for-each select="item">
                            <tr>
                                <td><xsl:value-of select="id"/></td>
                                <td><xsl:value-of select="name"/></td>
                                <td><xsl:value-of select="modifiedDate"/></td>
                            </tr>
                        </xsl:for-each>
                    </tbody>

                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Related Questions