Schwimms
Schwimms

Reputation: 85

Generate XSLT FROM XML

I am in the process of creating an XLST file from an XML. I have reviewed W3schools and many other posts but everything I find seems to point to basic XLST syntax. I assume what I am doing is simple though. The following XML is an example of the items I need help creating an XLST file for is:

<?xml version="1.0" encoding="UTF-8"?>
<Order>
    <Item Version="2">
        <GeneralInfo>
            <Name>TW</Name>
            <DateTime format="YYYY-MM-DD-HH:MM:SS">2017-04-17-17:06:36</DateTime>       
        </GeneralInfo>
        <AllInfo>
            <Quantity>166800</Quantity>
            <Cost>1668</Cost>
            <ID type="width">20</ID>
            <ID type="length">30</ID>               
            <Info>
                <Id type="ClientCode">Ac</Id>
                <Name>Arthur</Name>
            </Info>
        </AllInfo>
    </Item>
    <Item Version="2">
        <GeneralInfo>
            <Name>TW</Name>
            <DateTime format="YYYY-MM-DD-HH:MM:SS">2017-04-17-17:07:36</DateTime>       
        </GeneralInfo>
        <AllInfo>
            <Quantity>166</Quantity>
            <Cost>15</Cost>
            <ID type="width">20</ID>            
            <Info>
                <Id type="ClientCode">Ad</Id>
                <Name>Lynx</Name>
            </Info>
        </AllInfo>
        <SupplementalData>
            <Items>
                <Color>Red</Color>
            </Items>
        </SupplementalData>
    </Item> 
</Order>    

I want the output of the XLST to look like the following table: enter image description here

Upvotes: 0

Views: 16761

Answers (1)

Ajeet Singh
Ajeet Singh

Reputation: 1076

For XML

<Order>
<Item Version="2">
<GeneralInfo>
<Name>TW</Name>
<DateTime format="YYYY-MM-DD-HH:MM:SS">2017-04-17-17:06:36</DateTime>
</GeneralInfo>
<AllInfo>
<Quantity>166800</Quantity>
<Cost>1668</Cost>
<ID type="width">20</ID>
<ID type="length">30</ID>
<Info>
<Id type="ClientCode">Ac</Id>
<Name>Arthur</Name>
</Info>
</AllInfo>
</Item>
<Item Version="2">
<GeneralInfo>
<Name>TW</Name>
<DateTime format="YYYY-MM-DD-HH:MM:SS">2017-04-17-17:07:36</DateTime>
</GeneralInfo>
<AllInfo>
<Quantity>166</Quantity>
<Cost>15</Cost>
<ID type="width">20</ID>
<Info>
<Id type="ClientCode">Ad</Id>
<Name>Lynx</Name>
</Info>
</AllInfo>
<SupplementalData>
<Items>
<Color>Red</Color>
</Items>
</SupplementalData>
</Item> 
</Order>

and XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head>
</head>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th>Name</th>
<th>Date Time</th>
<th>Quantity</th>
<th>Cost</th>
<th>width</th>
<th>length</th>
<th>Client code</th>
<th>Name</th>
<th>Color</th>
</tr>
<xsl:for-each select="Order/Item">
<tr>
<td><xsl:value-of select="GeneralInfo/Name"/></td>
<td><xsl:value-of select="GeneralInfo/DateTime"/></td>
<td align="right"><xsl:value-of select="AllInfo/Quantity"/></td>
<td align="right"><xsl:value-of select="AllInfo/Cost"/></td>
<td align="right"><xsl:value-of select="AllInfo/ID[@type='width']"/></td>
<td align="right"><xsl:value-of select="AllInfo/ID[@type='length']"/></td>
<td><xsl:value-of select="AllInfo/Info/Id[@type='ClientCode']"/></td>
<td><xsl:value-of select="AllInfo/Info/Name"/></td>
<td><xsl:value-of select="SupplementalData/Items/Color"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template> 
</xsl:stylesheet>

Upvotes: 2

Related Questions