XML data to csv using XSLT

Post Reply
abonsey
Member
Posts: 98
Joined: Fri May 24, 2013 5:10 pm

XML data to csv using XSLT

Post by abonsey »

Hi All,
Ive some xml (see extract below) that I'm trying to convert to csv for use with HP Smartstream Designer.

<?xml version="1.0" encoding="Windows-1252" ?>
<!--//
Document to describe a number of personalised books
//-->
<CustomBooks>
<BatchID>5263</BatchID>
<CreationDateTime>2016-11-12T11:45:07.3723293+00:00</CreationDateTime>
<ProjectID>New Books</ProjectID>
<JobCode>2345637</JobCode>
<CustomBook>
<CustomerReference>72663GFG</CustomerReference>
<ShipmentMethod name="DHL"></ShipmentMethod>
<ShipmentAddress>
<AddressLine1>Andrew Bonsey</AddressLine1>
<AddressLine2>Street1</AddressLine2>
<AddressLine3>Street2</AddressLine3>
<AddressLine4>Street3</AddressLine4>
<AddressTownCity>Northampton</AddressTownCity>
<AddressPostCode>NN1 6TT</AddressPostCode>
<AddressCountry>UK</AddressCountry>
<AddressSortCode>PS7</AddressSortCode>
</ShipmentAddress>
<CustomFields>
<CustomField name="CudosID" value="5423312"></CustomField>
</CustomFields>
<Pages>
<Page index="1" name="FrontCover.pdf">
<CustomPageContent name="FirstName" value="Andrew"></CustomPageContent>
<CustomPageContent name="Course1" value="Art"></CustomPageContent>

using this xslt I get the fields converted fine apart from the CustomPageContent/FirstName/value. This comes out empty

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/"><xsl:text>"JobCode","FirstName","AddressLine1","AddressLine2","AddressLine3","AddressLine4","AddressTownCity","AddressPostCode","AddressCountry","AddressSortCode","CudosID","Barcode"</xsl:text>
<xsl:text>
</xsl:text>
<xsl:for-each select="/CustomBooks">
<xsl:text>"</xsl:text><xsl:value-of select="JobCode"/><xsl:text>","</xsl:text><xsl:value-of select="/CustomBooks/CustomBook/Pages/Page[@index='1']/CustomPageContent[@Name='FirstName']/@value"/><xsl:text>","</xsl:text><xsl:value-of select="CustomBook/ShipmentAddress/AddressLine1"/><xsl:text>","</xsl:text><xsl:value-of select="CustomBook/ShipmentAddress/AddressLine2"/><xsl:text>","</xsl:text><xsl:value-of select="CustomBook/ShipmentAddress/AddressLine3"/><xsl:text>","</xsl:text><xsl:value-of select="CustomBook/ShipmentAddress/AddressLine4"/><xsl:text>","</xsl:text><xsl:value-of select="CustomBook/ShipmentAddress/AddressTownCity"/><xsl:text>","</xsl:text><xsl:value-of select="CustomBook/ShipmentAddress/AddressPostCode"/><xsl:text>"</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Here's my output:
"JobCode","FirstName","AddressLine1","AddressLine2","AddressLine3","AddressLine4","AddressTownCity","AddressPostCode","AddressCountry","AddressSortCode","CudosID","Barcode"
"2345637","","Andrew Bonsey","Street1","Street2","Street3","Northampton","NN1 6TT"

Any ideas why it's not seeing the @/value properly??

Thanks for any help
freddyp
Advanced member
Posts: 413
Joined: Thu Feb 09, 2012 3:53 pm

Re: XML data to csv using XSLT

Post by freddyp »

XML and XPath are case-sensitive also for tags and attributes: CustomPageContent[@Name='FirstName'] does not exist, but CustomPageContent[@name='FirstName'] does.
abonsey
Member
Posts: 98
Joined: Fri May 24, 2013 5:10 pm

Re: XML data to csv using XSLT

Post by abonsey »

Thanks for that. All working fine now.

You spend so much time looking at something you can't see the wood for the trees :?
Post Reply