XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
zinc yoSubject: XML to CSV Matching Attributes
Author: zinc yo
Date: 02 Jul 2007 01:43 PM
Originally Posted: 02 Jul 2007 01:42 PM
I am currently trying to convert a XML file into a CSV. However, rather than having tags in all of the attributes, some of my nested code is without tags. I need to figure out how to match attributes in my stylesheet, so as to recognize all of my tags. Below is a small sample of the code:

- <MktDataFull src="Info1" AsOfDt="2007-06-28T20:51:50Z">
- <Instrmt id="20045476" src="MY_Source" mSym="INFO" finTyp="33" trdFactor="1">
<Control prcMethod="Info" ts="2007-06-28T05:00:00Z" statusMsg="" status="1" />
<Valuation Px="-13.0442" AccrdAmt="-0.35" ValueAsSpread="0.000434905" UnwindSpread="0.000419011" />
</Instrmt>

Essentially, I need to be able to access SRC and AsOfDt within MktDataFull, as well as id, src & mSym within /MktData/Instrmt/

Any help would be greatly appreciated!

Postnext
Tony LavinioSubject: XML to CSV Matching Attributes
Author: Tony Lavinio
Date: 02 Jul 2007 03:59 PM
You match them just the same as elements, but with a @.
So, to match the 'src' attribute, you'd match on @src
either like this:

<xsl:template match="@src">
...

or like this:

<xsl:value-of select="MktDataFull/@src"/>

Does this help?

Postnext
zinc yoSubject: XML to CSV Matching Attributes
Author: zinc yo
Date: 02 Jul 2007 04:42 PM
Very Much, Thank you!

I do, however, have one more question:

I was able to match all the data using the @ attribute. However, this only works for my first set of data. I now need to create some sort of loop so that it works for ALL of the subsequent data. Any Ideas?

Postnext
Alberto MassariSubject: XML to CSV Matching Attributes
Author: Alberto Massari
Date: 03 Jul 2007 07:43 AM
You will have to use xsl:for-each to enumerate all the nodes.

Alberto

Postnext
zinc yoSubject: XML to CSV Matching Attributes
Author: zinc yo
Date: 03 Jul 2007 03:11 PM
Alberto, could you please give me an example of what this would look like?

Postnext
Tony LavinioSubject: XML to CSV Matching Attributes
Author: Tony Lavinio
Date: 03 Jul 2007 03:45 PM
If you could create a SMALL sample of your XML,
and a SMALL subset of your XSLT which acts on it,
we could help.

Please make sure your XSLT is runnable. Often
people will submit a portion asking where the
trouble is, when the actual problem is caused by
some OTHER portion of their code that they haven't
shown.

Postnext
zinc yoSubject: XML to CSV Matching Attributes
Author: zinc yo
Date: 05 Jul 2007 08:59 AM
XML:
- <MktDataFull src="RiskMgmt" AsOfDt="2007-06-28T20:51:50Z">
- <Instrmt id="20045476" src="CIG" mSym="NWS/USCDS140F2008-2" finTyp="33" trdFactor="1">
<Control prcMethod="CFF_MODEL" ts="2007-06-28T05:00:00Z" statusMsg="" status="1" />
<Valuation Px="-13.0442" AccrdAmt="-0.35" ValueAsSpread="0.000434905" UnwindSpread="0.000419011" />
</Instrmt>

XSL:
<?xml version='1.0'?>
<?xsl:stylesheet version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:template match="/">
<xsl:apply-templates select="MktDataFull"/>
<xsl:apply-templates select="MktDataFull/Instrmt"/>
</xsl:template>

<xsl:template match="MktDataFull">
"<xsl:value-of select="@src"/>","<xsl:value-of select="@AsOfDt"/>",
"Instrmt id","src","mSym","finTyp","trdFactor","Control prcMethod","ts","statusMsg","status","Px","AccrdAmt","ValueAsSpread","UnwindSpread",
</xsl:template>
<xsl:template match="MktDataFull/Instrmt">
"<xsl:value-of select="@id"/>","<xsl:text/>
<xsl:value-of select="@src"/>","<xsl:text/>
<xsl:value-of select="@mSym"/>","<xsl:text/>
<xsl:value-of select="@finTyp"/>","<xsl:text/>
<xsl:value-of select="@trdFactor"/>","<xsl:text/>
<xsl:value-of select="/MktDataFull/Instrmt/Control/@prcMethod"/>","<xsl:text/>
<xsl:value-of select="(/MktDataFull/Instrmt/Control/@ts)"/>","<xsl:text/>
<xsl:value-of select="(/MktDataFull/Instrmt/Control/@statusMsg)"/>","<xsl:text/>
<xsl:value-of select="(/MktDataFull/Instrmt/Control/@status)"/>","<xsl:text/>
<xsl:value-of select="(/MktDataFull/Instrmt/Valuation/@Px)"/>","<xsl:text/>
<xsl:value-of select="(/MktDataFull/Instrmt/Valuation/@AccrdAmt)"/>","<xsl:text/>
<xsl:value-of select="/MktDataFull/Instrmt/Valuation/@ValueAsSpread"/>","<xsl:text/>
<xsl:value-of select="(/MktDataFull/Instrmt/Valuation/@UnwindSpread)"/>"<xsl:text/>
</xsl:template>
</xsl:stylesheet>

The problem which I am having, is that anything which is an attribute of instrmt, such as id, src, and msym, all pull data correctly, and the data updates for all the subsequent xml. However, once I try to access attributes of Control or Valuation, the data ONLY pulls from the FIRST set of data, and therefore my csv file contains many rows filled with the same data, rather than being updated from the subsequent data.

I hope that by integrating xsl:for-each into this, I can pull all of my data, and not just the first set.

Postnext
Tony LavinioSubject: XML to CSV Matching Attributes
Author: Tony Lavinio
Date: 05 Jul 2007 09:50 AM
When you use xsl:template, your current location is the node matched
in the match= attribute. So inside, you don't want to reference from
the /root/element, but from the current location.

As a side note, when outputting literal text, instead of doing
","<xsl:text/>, the proper way is to surround the text with the
tags, like this: <xsl:text>","</xsl:text>.

We can't tell exactly what you want your output XML to look like, but
try the attached. Note that in order to properly nest the output XML,
you should also nest the xsl:apply-templates that reference nested
XML elements within their parent xsl:templates.


Unknownm.xsl

Postnext
Tony LavinioSubject: XML to CSV Matching Attributes
Author: Tony Lavinio
Date: 05 Jul 2007 09:52 AM
Using the debugger in Stylus Studio would make this a snap.
Considering you asked this on our support forum, we do assume
that you are using our product. If not, please take at least
a few minutes to try the free 15-day evaluation, available at
http://www.stylusstudio.com/xml_download.html

Posttop
zinc yoSubject: XML to CSV Matching Attributes
Author: zinc yo
Date: 05 Jul 2007 09:59 AM
Hey, thanks for the suggestions--

The reason why I had all of the "," in my xsl is because I am trying to convert from xml to a csv (excel spreadsheet)...

Therefore, I need to somehow edit the code you gave me to have the data print out correctly

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.