XSLT Simple Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott

    XSLT Simple Question

    The following is the XML I have to work with. Below is the question
    <Table0>
    <CaseID>10211 4</CaseID>
    <CaseNumber>1 </CaseNumber>
    <DateOpened>200 5-06-14T07:26:00.000 0000-05:00</DateOpened>
    <OnCallPerson />
    <CallType>Expos ure</CallType>
    <ExposureReason >General</ExposureReason>
    <OtherExposureR eason>Unintenti onal</OtherExposureRe ason>
    <ClientName>Tes t Client</ClientName>
    <Priority>Mediu m</Priority>
    <Table1>
    <CaseID>10211 4</CaseID>
    <CaseProductID> 1</CaseProductID>
    <ProductName>Pr oduct B</ProductName>
    </Table1>
    <Table1>
    <CaseID>10211 4</CaseID>
    <CaseProductID> 2</CaseProductID>
    <ProductName>Pr oduct A</ProductName>
    </Table1>
    <Table2>
    <ProductIssue>N ot applicable</ProductIssue>
    <CaseID>10211 4</CaseID>
    </Table2>
    <Table3>
    <CaseID>10211 4</CaseID>
    <CaseCallerID>2 3290</CaseCallerID>
    <CallerName>Joh n Doe</CallerName>
    <Address>123 Main Street</Address>
    <City>Brookly n</City>
    <State>NY</State>
    <ZipCode>1234 5</ZipCode>
    <Country>USA</Country>
    <Phone>12345678 90</Phone>
    <Relation>Sel f</Relation>
    </Table3>
    <Table4>
    <CaseID>10211 4</CaseID>
    <CasePatientID> 102114</CasePatientID>
    <Gender>Male</Gender>
    <ExposureTime />
    <ManagementSite >Managed on site</ManagementSite>
    <SymptomOnset>3 0 min or less</SymptomOnset>
    <SymptomDuratio n>Unknown</SymptomDuration >
    <Age>Unknown</Age>
    <Severity>Minor </Severity>
    <Table5>
    <CaseID>10211 4</CaseID>
    <CasePatientID> 102114</CasePatientID>
    <ExposureRoute> Dermal</ExposureRoute>
    </Table5>
    <Table6>
    <CaseID>10211 4</CaseID>
    <CasePatientID> 102114</CasePatientID>
    <Symptom>: Irritation/Pain</Symptom>
    </Table6>
    <Table7>
    <CaseID>10211 4</CaseID>
    <CasePatientID> 102114</CasePatientID>
    <Therapy>wash </Therapy>
    </Table7>
    <Table7>
    <CaseID>10211 4</CaseID>
    <CasePatientID> 102114</CasePatientID>
    <Therapy>Othe r</Therapy>
    </Table7>
    </Table4>
    <Table8>
    <Note>Caller used product this morning</Note>
    <CreatedOn>20 05-06-14T07:27:00.000 0000-05:00</CreatedOn>
    <CaseID>10211 4</CaseID>
    </Table8>
    <Table8>
    <Note>No Followup number. Close case.</Note>
    <CreatedOn>20 01-06-21T12:12:00.000 0000-05:00</CreatedOn>
    <CaseID>10211 4</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.

  • Patrick TJ McPhee

    #2
    Re: XSLT Simple Question

    In article <1118843236.460 866.248040@g43g 2000cwa.googleg roups.com>,
    Scott <scott.remiger@ 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:styleshe et 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:styleshe et 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="CaseNumbe r" select="string( CaseNumber)"/>
    <xsl:with-param name="DateOpene d" 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="CaseNumbe r"/>
    <xsl:param name="DateOpene d"/>
    <!-- etc -->

    <xsl:value-of select="$CaseID "/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="$CaseNu mber"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="$DateOp ened"/>
    <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="CaseNumbe r"/>
    <xsl:param name="DateOpene d"/>
    <!-- etc -->

    <xsl:value-of select="$CaseID "/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="$CaseNu mber"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="$DateOp ened"/>
    <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="CaseNumbe r" select="$CaseNu mber"/>
    <xsl:with-param name="DateOpene d" select="$DateOp ened"/>
    <!-- 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="CaseNumbe r"/>
    <xsl:param name="DateOpene d"/>
    <!-- etc -->

    <xsl:value-of select="$CaseID "/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="$CaseNu mber"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="$DateOp ened"/>
    <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="CaseNumbe r"/>
    <xsl:param name="DateOpene d"/>
    <!-- etc -->

    <xsl:value-of select="$CaseID "/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="$CaseNu mber"/>
    <xsl:text>,</xsl:text>
    <xsl:value-of select="$DateOp ened"/>
    <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
    ptjm@interlog.c om

    Comment

    Working...