XSLT Simple Question

S

Scott

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.
 
P

Patrick TJ McPhee

% 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:eek:utput 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:eek:utput 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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top