user2307236
user2307236

Reputation: 755

XML to XML with XSLT using Visual Studio

I am trying to modify the structure of an XML file using an XSLT. The below is the input XML:-

<?xml version="1.0" encoding="utf-8"?>
<Report xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <table1>
        <Detail_Collection>
            <Details ID="1   " Type="4"  />
            <Details ID="2   " Type="5"  />
        </Detail_Collection>
    </table1>
</Report>

This is the XSLT file which I created however it is not picking up the values from the input XML.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    exclude-result-prefixes="xsi"
    >

    <xsl:output method="xml" indent="yes" encoding="utf-8" />

    <xsl:template match="/">
        <html>
            <body>
                <h2>TEST</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>ID</th>
                        <th>Type</th>
                    </tr>
                    <xsl:for-each select="Report/table1/Detail_Collection/Details">
                        <tr>
                            <td>
                                <xsl:value-of select="ID"/>
                            </td>
                            <td>
                                <xsl:value-of select="Type"/>
                            </td>
                         </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>   

The problem is that no vaues are being picked up and I cannot understand what is the problem. It seems like the input XML is not being linked with the XSLT. After this is solved I should be able to do my task since I think I grasped the basics of XSLT elements (e.g. for-each, value-of, sort etc.) However since I'm new to XSLT I am finding a hard time to connect/input the XML into the XSLT.

Upvotes: 0

Views: 760

Answers (1)

Tim C
Tim C

Reputation: 70618

Firstly, your XML has an table1 element which have missed out in your xsl:for-each select. It should look like this...

<xsl:for-each select="Report/table1/Detail_Collection/Details">

Secondly you are doing <xsl:value-of select="ID"/> to get the values of the Details, but ID is an attribute in your XML, so you really need to use the @ symbol to indicate this

<xsl:value-of select="@ID"/>

Try this XSLT

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    exclude-result-prefixes="xsi"    >

    <xsl:output method="html" indent="yes" encoding="utf-8" />

    <xsl:template match="/">
        <html>
            <body>
                <h2>TEST</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>ID</th>
                        <th>Type</th>
                    </tr>
                    <xsl:for-each select="Report/table1/Detail_Collection/Details">
                        <tr>
                            <td>
                                <xsl:value-of select="@ID"/>
                            </td>
                            <td>
                                <xsl:value-of select="@Type"/>
                            </td>
                         </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>  

Upvotes: 3

Related Questions