473,508 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XSLT Simple Question

The following is the XML I have to work with. Below is the question
<Table0>
<CaseID>102114</CaseID>
<CaseNumber>1</CaseNumber>
<DateOpened>2005-06-14T07:26:00.0000000-05:00</DateOpened>
<OnCallPerson />
<CallType>Exposure</CallType>
<ExposureReason>General</ExposureReason>
<OtherExposureReason>Unintentional</OtherExposureReason>
<ClientName>Test Client</ClientName>
<Priority>Medium</Priority>
<Table1>
<CaseID>102114</CaseID>
<CaseProductID>1</CaseProductID>
<ProductName>Product B</ProductName>
</Table1>
<Table1>
<CaseID>102114</CaseID>
<CaseProductID>2</CaseProductID>
<ProductName>Product A</ProductName>
</Table1>
<Table2>
<ProductIssue>Not applicable</ProductIssue>
<CaseID>102114</CaseID>
</Table2>
<Table3>
<CaseID>102114</CaseID>
<CaseCallerID>23290</CaseCallerID>
<CallerName>John Doe</CallerName>
<Address>123 Main Street</Address>
<City>Brooklyn</City>
<State>NY</State>
<ZipCode>12345</ZipCode>
<Country>USA</Country>
<Phone>1234567890</Phone>
<Relation>Self</Relation>
</Table3>
<Table4>
<CaseID>102114</CaseID>
<CasePatientID>102114</CasePatientID>
<Gender>Male</Gender>
<ExposureTime />
<ManagementSite>Managed on site</ManagementSite>
<SymptomOnset>30 min or less</SymptomOnset>
<SymptomDuration>Unknown</SymptomDuration>
<Age>Unknown</Age>
<Severity>Minor</Severity>
<Table5>
<CaseID>102114</CaseID>
<CasePatientID>102114</CasePatientID>
<ExposureRoute>Dermal</ExposureRoute>
</Table5>
<Table6>
<CaseID>102114</CaseID>
<CasePatientID>102114</CasePatientID>
<Symptom>: Irritation/Pain</Symptom>
</Table6>
<Table7>
<CaseID>102114</CaseID>
<CasePatientID>102114</CasePatientID>
<Therapy>wash</Therapy>
</Table7>
<Table7>
<CaseID>102114</CaseID>
<CasePatientID>102114</CasePatientID>
<Therapy>Other</Therapy>
</Table7>
</Table4>
<Table8>
<Note>Caller used product this morning</Note>
<CreatedOn>2005-06-14T07:27:00.0000000-05:00</CreatedOn>
<CaseID>102114</CaseID>
</Table8>
<Table8>
<Note>No Followup number. Close case.</Note>
<CreatedOn>2001-06-21T12:12:00.0000000-05:00</CreatedOn>
<CaseID>102114</CaseID>
</Table8>
</Table0>

I need to convert all this data to a CSV format. How do I do it using
XSLT. I can do the simple for each loop but when it comes to the
nested Table1, Table2 I get lost how do I reference those nested values
notice that All the Tables 0 to 8 can have 0 to n number of entries
with Table 0 needing at least one.

Jul 20 '05 #1
1 1770
In article <11**********************@g43g2000cwa.googlegroups .com>,
Scott <sc***********@itfirst.com> wrote:

% The following is the XML I have to work with. Below is the question

[...]

% I need to convert all this data to a CSV format.

That's not exactly a lot to go on. Here's one way:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='text'/>

<xsl:variable name="nl"><xsl:text>
</xsl:text></xsl:variable>

<xsl:template match='text()'/>

<xsl:template match='Table0'>
<xsl:value-of select = "CaseID"/>
<xsl:text>,</xsl:text>
<xsl:value-of select='.'/>
</xsl:template>

</xsl:stylesheet>

This makes the case id the first field and everything else in the document
the second field. You should be more clear about the output you expect.

% XSLT. I can do the simple for each loop but when it comes to the
% nested Table1, Table2 I get lost how do I reference those nested values
% notice that All the Tables 0 to 8 can have 0 to n number of entries
% with Table 0 needing at least one.

I suggest using apply-templates instead of for-each, using parameters
to pass data down to the point where you want to emit it, and using modes
if necessary to distinguish between different nested uses of the same
element name.

Hopefully this will give you some ideas.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method='text'/>

<!-- this defines a variable whose value is a new-line -->
<xsl:variable name="nl"><xsl:text>
</xsl:text></xsl:variable>

<!-- we emit nothing from Table 0, merely collecting data -->
<xsl:template match='Table0'>
<xsl:apply-templates>
<xsl:with-param name="CaseID" select="string(CaseID)"/>
<xsl:with-param name="CaseNumber" select="string(CaseNumber)"/>
<xsl:with-param name="DateOpened" select="string(DateOpened)"/>
<!-- etc -->
</xsl:apply-templates>
</xsl:template>

<!-- override the default handling of text nodes -->
<xsl:template match='text()'/>

<!-- In table1, I'm emitting some data but not handling nested tables -->
<xsl:template match='Table1'>
<xsl:param name="CaseID"/>
<xsl:param name="CaseNumber"/>
<xsl:param name="DateOpened"/>
<!-- etc -->

<xsl:value-of select="$CaseID"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="$CaseNumber"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="$DateOpened"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="string(CaseProductID)"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="string(ProductName)"/>
<xsl:value-of select="$nl"/>
</xsl:template>

<!-- In table4, I'm emitting some data then going on to handle all the
nested tables -->
<xsl:template match='Table4'>
<xsl:param name="CaseID"/>
<xsl:param name="CaseNumber"/>
<xsl:param name="DateOpened"/>
<!-- etc -->

<xsl:value-of select="$CaseID"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="$CaseNumber"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="$DateOpened"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="string(Gender)"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="string(ManagementSite)"/>
<xsl:value-of select="$nl"/>

<!-- I set a mode so that the table4 handling of symptom et al
can differ from other tables -->
<xsl:apply-templates mode="table4">
<xsl:with-param name="CaseID" select="$CaseID"/>
<xsl:with-param name="CaseNumber" select="$CaseNumber"/>
<xsl:with-param name="DateOpened" select="$DateOpened"/>
<!-- etc -->
</xsl:apply-templates>

</xsl:template>

<!-- I'm picking the nested tables based on their content rather
than their element name. These could also call apply-templates
to nest further. -->
<xsl:template match='*[Symptom]' mode="table4">
<xsl:param name="CaseID"/>
<xsl:param name="CaseNumber"/>
<xsl:param name="DateOpened"/>
<!-- etc -->

<xsl:value-of select="$CaseID"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="$CaseNumber"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="$DateOpened"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="string(CasePatientID)"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="string(Symptom)"/>
<xsl:value-of select="$nl"/>
</xsl:template>

<xsl:template match='text()' mode="table4"/>
<xsl:template match='*[Therapy]' mode="table4">
<xsl:param name="CaseID"/>
<xsl:param name="CaseNumber"/>
<xsl:param name="DateOpened"/>
<!-- etc -->

<xsl:value-of select="$CaseID"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="$CaseNumber"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="$DateOpened"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="string(CasePatientID)"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="string(Therapy)"/>
<xsl:value-of select="$nl"/>
</xsl:template>
</xsl:stylesheet>

--

Patrick TJ McPhee
North York Canada
pt**@interlog.com
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2109
by: Phil | last post by:
Hi everybody, I am a XSLT beginner and the following problem really makes me crazy ! I have a main "contacts.xml" document which contains references to several contact data XML files. My aim...
3
1659
by: Just Curious | last post by:
Hello, I have a following situation.. XML Document contains <PurchaseOrder> <LineItems> <LineItem> <Description></Description> </LineItem> <LineItem>
4
2104
by: Ringo Langly | last post by:
Hi all, I'm a seasoned web programmer, but I've never touched XSLT. It's always been one of those acronyms I've never needed to educate myself on. Now... we're working with a web content...
9
12617
by: Eric Anderson | last post by:
Got a quick question trying to figure out the XSLT way of implementing the functionality of associative arrays. Basically want I want to do is the following: <xsl:template name="foo">...
5
1445
by: dennis | last post by:
Hi, First of all, hi to you all. I'm working on a Delphi project wich is becoming near it's deadline. I have a very simple XSLT question wich i hope one of you folks can help me with? The...
3
3071
by: Ian Roddis | last post by:
Hello, I want to embed SQL type queries within an XML data record. The XML looks something like this: <DISPLAYPAGE> <FIELD NAME="SERVER" TYPE="DROPDOWN"> <OPTION>1<OPTION> <OPTION>2<OPTION>...
0
3284
by: DAnne | last post by:
Hi, I'm very new to xslt and this is my first time posting to a Forum so please forgive me if I transgress any protocols. I have to do a tally report. This report is divided up into sections....
0
4514
by: DAnne | last post by:
I'm trying to do a simple count function in xslt and it's turning out to be extremely painful. I have a report that brings back a set of responses for each question per section in an Audit. I...
3
1665
by: RC | last post by:
Let's say: if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { // Now I got an XML object here var xmlDocument = XMLHttpRequestObject.responseXML; // next I have...
3
2309
by: Chrism2671 | last post by:
I'm new to XSLT/XML and I have a very simple, quick question. i've been trying to convert simple xml files into CSV files and have made a simple XSLT template using the w3 tutorials, but it doesn't...
0
7227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7127
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7331
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7501
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5633
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4713
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1564
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
424
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.