473,766 Members | 2,026 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>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.

Jul 20 '05 #1
1 1788
In article <11************ **********@g43g 2000cwa.googleg roups.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: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
pt**@interlog.c om
Jul 20 '05 #2

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

Similar topics

3
2126
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 is to process the contacts in a single-pass XSLT process. That is why the "document()" function is what I need. I call the "document()" XPath function from a "for-each" instruction.
3
1675
by: Just Curious | last post by:
Hello, I have a following situation.. XML Document contains <PurchaseOrder> <LineItems> <LineItem> <Description></Description> </LineItem> <LineItem>
4
2124
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 provider who says we need to use XSLT and Web Services to pull the content from their site. Can someone give me a nutshell definition on how this works??? We use Cold Fusion MX on our web server, but I'm having trouble finding a
9
12629
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"> <xsl:param name="bar"/> <xsl:choose> <xsl:when test="$bar = 'a'">b</xsl:when> <xsl:when test="$bar = 'c'">d</xsl:when> <xsl:when test="$bar = 'e'">f</xsl:when>
5
1466
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 problem is i need to transform a xml file from this: <root>
3
3092
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> <OPTION>3<OPTION> </FIELD>
0
3303
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. Each section has a list of questions. Each question has responses. I need to display a list of responses to the questions (i.e. set:distinct), once and only once, each section. My second problem is that these questions can also have corrective...
0
4538
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 want to bring back the response only once (i.e. Na,Yes, No answers only once). I have this working. The part I'm having trouble with is the count. Now for each response I need to do a tally of how many times the respondent answered Yes or No. My...
3
1687
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 dealing with XML file tags used DOM. var options = xmlDocument.getElementsByTagNameNS(null,"myXMLTag"); doMyFunctionWithHTMLDOM(options);
3
2330
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 seem to display anything. It does display plain text I enter into the templates, the value-of tags just render whitespace. If anybody can write a template of just a few lines just to demonstrate how to get it to display something from this XML...
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2806
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.