Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XSLT Simple Question

Reply
Thread Tools

XSLT Simple Question

 
 
Scott
Guest
Posts: n/a
 
      06-15-2005
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.

 
Reply With Quote
 
 
 
 
Patrick TJ McPhee
Guest
Posts: n/a
 
      06-16-2005
In article <(E-Mail Removed) .com>,
Scott <(E-Mail Removed)> 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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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'>
<xslaram name="CaseID"/>
<xslaram name="CaseNumber"/>
<xslaram 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'>
<xslaram name="CaseID"/>
<xslaram name="CaseNumber"/>
<xslaram 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">
<xslaram name="CaseID"/>
<xslaram name="CaseNumber"/>
<xslaram 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">
<xslaram name="CaseID"/>
<xslaram name="CaseNumber"/>
<xslaram 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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT question: How to lookup another tag's children in XSLT yinglcs@gmail.com XML 18 10-03-2006 11:22 PM
Multiple XSLT Transforms using a Controller XSLT sneill@mxlogic.com XML 2 10-19-2005 11:00 AM
ANN: New low-cost XML Editor, XSLT Editor, XSLT Debugger, DTD/Schema Editor Stylus Studio Java 0 08-03-2004 03:53 PM
xslt alone or xslt/java for static site? ted XML 1 01-26-2004 10:41 AM
[XSLT]Passing values from Javascript to a XSLT variable Benjamin Hillsley XML 3 09-25-2003 04:50 AM



Advertisments