XSLT Simple Question

Discussion in 'XML' started by Scott, Jun 15, 2005.

  1. Scott

    Scott Guest

    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.
     
    Scott, Jun 15, 2005
    #1
    1. Advertising

  2. In article <>,
    Scott <> 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: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>

    --

    Patrick TJ McPhee
    North York Canada
     
    Patrick TJ McPhee, Jun 16, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    734
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,729
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    646
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    733
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    18
    Views:
    2,791
    Joseph Kesselman
    Oct 4, 2006
Loading...

Share This Page