Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 23rd, 2012, 05:27 PM
Registered User
 
Join Date: May 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Thumbs up Transform boolean and enumerated XML data

Hello,

I'm new to XSLT and would appreciate help transforming the below XML data that I'm trying to convert to CSV format. The first 2 fields are converting fine, however dynamicFlag and SelMode are returning blanks.

XML:

<CDR>
<DRTA>
<callType>11</callType>
<callTime>1108251352422</callTime>
<dynamicFlag><true/></dynamicFlag>
<SelMode><servingLinkSupplied/></SelMode>
</DRTA>
</CDR>

XSL:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:strip-space elements = "CDR"/>
<xsl:template match="/CDR/DRTA">"<xsl:value-of select="normalize-space(/CDR/DRTA/callType)"/>","<xsl:value-of select="normalize-space(/CDR/DRTA/callTime)"/>","<xsl:value-of select="normalize-space(/CDR/DRTA/dynamicFlag)"/>","<xsl:value-of select="normalize-space(/CDR/DRTA/SelMode)"/>"
</xsl:template>
</xsl:stylesheet>
 
Old May 23rd, 2012, 05:44 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You haven't said what you want to output for these two elements. You're currently trying to output the string value, which is empty. If you want to output the name of the contained element, use

Code:
<xsl:value-of select="name(/CDR/DRTA/dynamicFlag/*)"/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 23rd, 2012, 05:53 PM
Registered User
 
Join Date: May 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Hi Michael,

Thank you for your response. I'd like the output to be

"11","1108251352422","true","servingLinkSuppli ed"

or

"11","1108251352422","1","1" (if I can somehow transform "true" and "servingLinkSupplied" to 1)


Much Thanks again,
-Vijay
 
Old May 24th, 2012, 10:25 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you are in a template matching against /CDR/DRTA then you don't need the /CDR/DRTA in from of every value of instruction (in fact if you had more than one DRTA element this would produce something I am sure you wouldnt want).

Also, if you want to transform a particular element into something else then use a apply-templates rather than a value-of and have other templates to handle the transformation.

Code:
<xsl:template match="/CDR/DRTA">"<xsl:value-of select="normalize-space(callType)"/>","<xsl:value-of select="normalize-space(callTime)"/>","<xsl:apply-template select="dynamicFlag"/>","<xsl:apply-template select="SelMode"/>"
</xsl:template>

<xsl:template match="dynamicFlag/true">1</xsl:template>
<xsl:template match="dynamicFlag/false">0</xsl:template>
<xsl:template match="SelMode/servingLinkSupplied">1</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
ovjrao (May 24th, 2012)
 
Old May 24th, 2012, 05:15 PM
Registered User
 
Join Date: May 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Sam,

Thanks for your response. I tried your suggestion for the first boolean field and received the below error

SystemId Unknown; Line #-1; Column #-1; XSLT Error (javax.xml.transform.TransformerConfigurationExcep tion): javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: xsl:apply-template is not allowed in this position in the stylesheet!
Code:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:strip-space elements = "CDR"/>
<xsl:template match="/CDR/DRTA">"<xsl:value-of select="normalize-space(callType)"/>","<xsl:value-of select="normalize-space(callTime)"/>","<xsl:>","<xsl:apply-template select="dynamicFlag"/>",)"/>","<xsl:value-of select="normalize-space(SelMode)"/>"
</xsl:template>

<xsl:template match="dynamicFlag/true">1</xsl:template>
<xsl:template match="dynamicFlag/false">0</xsl:template>
</xsl:stylesheet>
 
Old May 24th, 2012, 06:50 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Typo: it's xsl:apply-templates.

We put typos in our answers deliberately, it forces people to pore over the code and try to understand it, which helps them learn the language and reduces the number of questions they ask in the future. Sometimes.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
ovjrao (May 25th, 2012)
 
Old May 25th, 2012, 12:06 AM
Registered User
 
Join Date: May 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Thumbs up

That worked like a charm. Thank you very much.

One more question - is there a way to convert sets of data that are missing a parent node but otherwise XML compliant? I had to go through some hoops to create an entirely new file just to add a parent node (to use XSLT) but was curious if there is a better way to process the below XML example without an enclosing parent node.

Code:
<CDR>
   <TagA1>
      <TagB>
         <TagC>
         </TagC>
      </TagB>
  </TagA1>
</CDR>

<CDR>
   <TagA2>
      <TagB>
         <TagD>
         </TagD>
      </TagB>
  </TagA2>
</CDR>
Thank you again.
Vijay
 
Old May 25th, 2012, 04:01 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's best to post a new question on a new thread.

The way that most people tackle XML with multiple top level elements is to treat it as an external parsed entity, and copy it into a wrapper document like this example of mine:

Code:
<!DOCTYPE orders [
<!ENTITY e SYSTEM "orderlog.xml">
]>
<?xml-stylesheet href="report.xsl" type="text/xsl"?>
<orders>&e;</orders>
The wrapper document can then be presented to the parser.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old June 19th, 2012, 02:07 PM
Registered User
 
Join Date: May 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default xml to csv returns duplicate records

why does the below xsl code returns duplicate records when converting to csv ?

The number of output records matches with number of XML blocks but the csv values are duplicate (from the first xml block).

Code:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:strip-space elements = "CDR"/>
<xsl:template match="/CDR/DRTA">"<xsl:value-of select="normalize-space(callType)"/>","<xsl:value-of select="normalize-space(callTime)"/>","<xsl:>","<xsl:apply-templates select="dynamicFlag"/>",)"/>","<xsl:value-of select="normalize-space(SelMode)"/>"
</xsl:template>

<xsl:template match="dynamicFlag/true">1</xsl:template>
<xsl:template match="dynamicFlag/false">0</xsl:template>
</xsl:stylesheet>
 
Old June 19th, 2012, 03:39 PM
Registered User
 
Join Date: May 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default disregard my previous post regarding duplicate records

I was able to figure it out myself. Much Thanks.





Similar Threads
Thread Thread Starter Forum Replies Last Post
XML Transform moving data around using xslt BivsAi XSLT 0 February 23rd, 2012 12:41 PM
Transform xml to xml changing one tag. surfer97301 XSLT 2 April 21st, 2010 05:14 PM
XLST noob - XML -> XML transform help required Crass1968 XSLT 6 March 4th, 2010 07:32 AM
Split xml file with result document and javax.xml.transform.Transformer. nisargmca XSLT 3 January 12th, 2010 06:26 AM
The boolean data type in C++? code_lover C++ Programming 2 July 14th, 2008 01:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.