Formatting rows in XSLT

Here should go questions about transforming XML with XSLT and FOP.
AshKutt
Posts: 8
Joined: Tue Oct 02, 2012 4:53 am

Formatting rows in XSLT

Post by AshKutt »

I have the following XML and the intended out put is a pipe delimited file.

Code: Select all

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<Header Record>
<page>1</page>
<records>5</records>
</Header Record>
<Employee_Data>
<Employee>
<Empl_Reference Descriptor="John Smith">
<ID type="Employee_ID">00001</ID>
</Empl_Reference>
<Employee_Details>
<Empl_ID>00001</Empl_ID>
<First_Name>John</First_Name>
<Last_Name>Smith</Last_Name>
</Employee_Details>
</Employee>
<Employee>
<Empl_Reference Descriptor="John Carter">
<ID type="Employee_ID">00002</ID>
</Empl_Reference>
<Employee_Details>
<Empl_ID>00002</Empl_ID>
<First_Name>John</First_Name>
<Last_Name>Carter</Last_Name>
</Employee_Details>
</Employee>
<Employee>
<Empl_Reference Descriptor="Katie Smith">
<ID type="Employee_ID">00003</ID>
</Empl_Reference>
<Employee_Details>
<Empl_ID>00003</Empl_ID>
<First_Name>Katie</First_Name>
<Last_Name>Smith</Last_Name>
</Employee_Details>
</Employee>
</Employee Data>
I'm trying to create a pipe delimited file using xslt.

I'had created the xslt with the following code

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:ex="http://exslt.org/dates-and-times" extension-element-prefixes="ex"
xmlns:env=http://schemas.xmlsoap.org/soap/envelope/>

<xsl:template match="/">
<xsl:value-of select="/env:Envelope/env:Body/page"/> | <xsl:value-of
select="/env:Envelope/env:Body/records"/>
So far fine. I get the header records in pipe delimited format. Now when I put the reference for Employee_Data, all records gets displayed. I tried to create template reference and it still would not work. Either it displays all the records of all employees after the header is displayed and if I change it, I dont see any results but the header.

I'm new to writing xslt's. I know im making mistake on references. If anyone could help with sample code that would be great.

Here is the output I expect

1| 5
0001|John Smith
0002|John Carter
0003|Katie Smith


Thanks
Ash
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

Re: Formatting rows in XSLT

Post by adrian »

Hi,

The XML seems to have some mistakes that would make it not well formed and thus unusable. I'm guessing these are just spelling errors you've inserted here and the XML is actually fine otherwise.
e.g. <Header Record> can't have space in element name, so I'm guessing it's actually <Header_Record>.

Here's one way to do this:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:ex="http://exslt.org/dates-and-times" extension-element-prefixes="ex"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">

<xsl:output indent="no" method="text"/>
<xsl:template match="/">
<xsl:value-of select="/env:Envelope/env:Body/Header_Record/page"/> | <xsl:value-of
select="/env:Envelope/env:Body/Header_Record/records"/>
<xsl:for-each select="/env:Envelope/env:Body/Employee_Data//Employee">
<xsl:value-of select="' '"/>
<xsl:text>&#xa;</xsl:text>
<xsl:value-of select="Empl_Reference/ID"/> | <xsl:value-of select="Employee_Details/First_Name"/>
<xsl:text> </xsl:text>
<xsl:value-of select="Employee_Details/Last_Name"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
AshKutt
Posts: 8
Joined: Tue Oct 02, 2012 4:53 am

Re: Formatting rows in XSLT

Post by AshKutt »

That worked. Thannks Adrain.

What is the function to check for null values. I want to display space if last name does not exist.
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

Re: Formatting rows in XSLT

Post by adrian »

I've mentioned in the other thread: http://www.oxygenxml.com/forum/topic7275.html
To check for empty values:
<xsl:if test="preferredLastNameElement/text()">
To check for values that contain only space characters:
<xsl:if test="normalize-space(preferredLastNameElement/text())">
I guess the explanation is actually backwards.
The first one checks if there is a text node in that element and the second checks if there is a text node that contains something other than space characters.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply