next
|
Subject: 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.
|
next
|
Subject: 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. m.xsl
|
|