sidz Posted July 21, 2011 Share Posted July 21, 2011 Hi Guys,I am new to XSL and need help with transforming data in a very specific way. I have an XML file with multiple nodes: <parentNode> <Header> <Identifier type="Identifier"> <reference href="US"/> <id id="id">123</id> <id2> <reference href="US"/> <id id="id">123_A</id> </id2> <id3> <reference href="US"/> <id id="id">123_B</id> </id3> </Identifier> <Info> <reference href="US"/> <name name="BOB">BOB</name> </Info> <date>2011-01-19</date> <dateTime>2011-01-19T14:26:39Z</dateTime> </Header> <TYPE_SPECIAL> <product>cool</product> <single> <c1> <reference href="US"/> <pReference href="pty"/> <amount> <currency>dollar</currency> <amount>5555</amount> </amount> </c1> <c2> <reference href="YOU"/> <pReference href="pty"/> <amount> <currency>gold</currency> <amount>100</amount> </amount> </c2> <date>2011-01-28</date> <rate> <pair> <cc1>dollar</cc1> <cc2>gold</cc2> </pair> </rate> </single> <single> <c1> <reference href="US"/> <pReference href="pty"/> <amount> <currency>dollar</currency> <amount>5555</amount> </amount> </c1> <c2> <reference href="YOU"/> <pReference href="pty"/> <amount> <currency>gold</currency> <amount>100</amount> </amount> </c2> <date>2011-01-28</date> <rate> <pair> <cc1>dollar</cc1> <cc2>gold</cc2> </pair> </rate> </single> </TYPE_SPECIAL></parentNode><parentNode> <Header> <Identifier type="Identifier"> <reference href="US"/> <id id="id">123</id> <id2> <reference href="US"/> <id id="id">123_A</id> </id2> <id3> <reference href="US"/> <id id="id">123_B</id> </id3> </Identifier> <Info> <reference href="US"/> <name name="BOB">BOB</name> </Info> <date>2011-01-19</date> <dateTime>2011-01-19T14:26:39Z</dateTime> </Header> <TYPE_SPECIAL> <product>cool</product> <single> <c1> <reference href="US"/> <pReference href="pty"/> <amount> <currency>dollar</currency> <amount>5555</amount> </amount> </c1> <c2> <reference href="YOU"/> <pReference href="pty"/> <amount> <currency>gold</currency> <amount>100</amount> </amount> </c2> <date>2011-01-28</date> <rate> <pair> <cc1>dollar</cc1> <cc2>gold</cc2> </pair> </rate> </single> <single> <c1> <reference href="US"/> <pReference href="pty"/> <amount> <currency>dollar</currency> <amount>5555</amount> </amount> </c1> <c2> <reference href="YOU"/> <pReference href="pty"/> <amount> <currency>gold</currency> <amount>100</amount> </amount> </c2> <date>2011-01-28</date> <rate> <pair> <cc1>dollar</cc1> <cc2>gold</cc2> </pair> </rate> </single> </TYPE_SPECIAL></parentNode> The XSL should be able to process n number of <parentNode></parentNode> pairs in the XML file and transform each section. What I am not sure about is how do I do this in XSL i.e. how do I make sure no sections are skipped and resulting output from each is present in the file I am trying to transform this into. The resulting file is another XML structure created from the above and must have all the <parentNode>'s present.Looking on forums etc a possible option would be to use <xsl:for-each>?Thanks in advance. Link to comment Share on other sites More sharing options...
Martin Honnen Posted July 24, 2011 Share Posted July 24, 2011 Try to reduce samples you post to the minimum length necessary to demonstrate the problem you face. And then post both the input you have and the output you want, then we can help write XSLT code.Your verbal description does not seem to require anything complex for XSLT abilities, if you want to copy all elements besides the parentNode elements you want to transform all you need is <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy></xsl:template><xsl:template match="parentNode"> <!-- now transform here e.g. to change the name you could do --> <foo> <xsl:apply-templates select="@* | node()"/> </foo></xsl:template> If any child or descendant element nodes of parentNode need further transformation then you can add further templates for them doing that. Link to comment Share on other sites More sharing options...
sidz Posted July 25, 2011 Author Share Posted July 25, 2011 Hello Martin,Thank you for the sample and the advice, the code you provided has proven very useful in another piece of XSL I was working on. Apologies, my post wasn't clear. I will attempt to provide more detail below (and a cut down version of the source) <parentNode> <Header> <Identifier type="Identifier"> <reference href="US"/> <id id="id">123</id> <id2> <reference href="US"/> <id id="id">123_A</id> </id2> </Identifier> </Header> <TYPE_SPECIAL> <single> <c1> <amount> <currency>dollar</currency> <amount>5555</amount> </amount> </c1> </single> <single> <rate> <pair> <cc1>dollar</cc1> <cc2>gold</cc2> </pair> </rate> </single> </TYPE_SPECIAL> <TYPE_SPECIAL> <single> <c1> <amount> <currency>dollar</currency> <amount>4444</amount> </amount> </c1> </single> </TYPE_SPECIAL></parentNode> I have one massive XML starting and ending with <parentNode>. Each XML will have 1 header only and can have multiple <TYPE_SPECIAL> groups (i.e. in my example I have two but I could have 50). I need to be able to produce output similar to: Header Contains:Identifier=USID=123ID2=123_ASPECIAL1 contains:Single Amount1=dollarSingle Value1=5555Single Pair=dollar/goldSPECIAL2 contains:Single Amount1=dollarSingle Value1=4444 So each output could have 'n' number of SPECIAL sections with different values in and with different number of values i.e. SPECIAL1 has Amount1, Value1 ... SPECIAL2 has Amount1 only ... SPECIAL3 could have Amount, Value and Date etc. I should be able to return the exact number of groups by using for-each I am sure, however, how do I make sure when the for loop runs I get the values for each group in its own SPECIAL<#> section.Hope this clarifies, thanks in advance for your help.Sid. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.