Need help with XSLT
-
I have a problem I'm looking to solve with XSLT, but I'm a bit stumped at how to implement the syntax. I basically have a XML file with a structure narrowed down to this: <ITEM> <NAMEVALUE> <NAME>From1</NAME> <VALUE>From1 Value</VALUE> </NAMEVALUE> <NAMEVALUE> <NAME>From2</NAME> <VALUE>From2 Value</VALUE> </NAMEVALUE> <NAMEVALUE> <NAME>Price1</NAME> <VALUE>Price1 Value</VALUE> </NAMEVALUE> <NAMEVALUE> <NAME>Price2</NAME> <VALUE>Price2 Value</VALUE> </NAMEVALUE> . . . . </ITEM> (I can't change the format of the delivered file). I would like to transform it into something equivalent to: <Prices> <Price from="From1 Value">Price1 Value</Price> <Price from="From2 Value">Price2 Value</Price> </Prices> And I'm quite bugged down in it. I can most likely fix it using scripts in my XML file and some temporary variables, but I was wondering if it would be possible to either use more pure XSL syntaxt to solve this problem.
--------------------------- Blogging about SQL Server and related topics.
-
I have a problem I'm looking to solve with XSLT, but I'm a bit stumped at how to implement the syntax. I basically have a XML file with a structure narrowed down to this: <ITEM> <NAMEVALUE> <NAME>From1</NAME> <VALUE>From1 Value</VALUE> </NAMEVALUE> <NAMEVALUE> <NAME>From2</NAME> <VALUE>From2 Value</VALUE> </NAMEVALUE> <NAMEVALUE> <NAME>Price1</NAME> <VALUE>Price1 Value</VALUE> </NAMEVALUE> <NAMEVALUE> <NAME>Price2</NAME> <VALUE>Price2 Value</VALUE> </NAMEVALUE> . . . . </ITEM> (I can't change the format of the delivered file). I would like to transform it into something equivalent to: <Prices> <Price from="From1 Value">Price1 Value</Price> <Price from="From2 Value">Price2 Value</Price> </Prices> And I'm quite bugged down in it. I can most likely fix it using scripts in my XML file and some temporary variables, but I was wondering if it would be possible to either use more pure XSL syntaxt to solve this problem.
--------------------------- Blogging about SQL Server and related topics.
-
Alsvha wrote:
I was wondering if it would be possible to either use more pure XSL syntaxt to solve this problem.
I don't understand how "From1 Value" is related to "Price1 Value" in the XML source?
They're related due to the name From1 is connected to Price1 and From2 to Price2 and so forth. It is an unfortunate format, but it is what I have to work with :)
--------------------------- Blogging about SQL Server and related topics.
-
I have a problem I'm looking to solve with XSLT, but I'm a bit stumped at how to implement the syntax. I basically have a XML file with a structure narrowed down to this: <ITEM> <NAMEVALUE> <NAME>From1</NAME> <VALUE>From1 Value</VALUE> </NAMEVALUE> <NAMEVALUE> <NAME>From2</NAME> <VALUE>From2 Value</VALUE> </NAMEVALUE> <NAMEVALUE> <NAME>Price1</NAME> <VALUE>Price1 Value</VALUE> </NAMEVALUE> <NAMEVALUE> <NAME>Price2</NAME> <VALUE>Price2 Value</VALUE> </NAMEVALUE> . . . . </ITEM> (I can't change the format of the delivered file). I would like to transform it into something equivalent to: <Prices> <Price from="From1 Value">Price1 Value</Price> <Price from="From2 Value">Price2 Value</Price> </Prices> And I'm quite bugged down in it. I can most likely fix it using scripts in my XML file and some temporary variables, but I was wondering if it would be possible to either use more pure XSL syntaxt to solve this problem.
--------------------------- Blogging about SQL Server and related topics.
The following works (i.e. I've tested it) in Microsoft XSLT - but it. It creates a key containing all 'Price' NAMEVALUE nodes, indexed on the text after 'Price' in the NAMEVALUE/NAME node. This is used in the 'From' NAMEVALUE node template, to establish the linkage between 'From' and 'Price' nodes.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:key name="prices" match="/ITEM/NAMEVALUE[starts-with(NAME/text(), 'Price')]" use="substring-after(NAME/text(), 'Price')"/>
<xsl:template match="/">
<prices>
<xsl:apply-templates select="//NAMEVALUE"/>
</prices>
</xsl:template><xsl:template match="NAMEVALUE[starts-with(NAME/text(), 'From')]">
<Price from="{VALUE/text()}">
<xsl:value-of select="key('prices', substring-after(NAME/text(), 'From'))/VALUE"/>
</Price>
</xsl:template><xsl:template match="NAMEVALUE"/>
</xsl:stylesheet>
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
The following works (i.e. I've tested it) in Microsoft XSLT - but it. It creates a key containing all 'Price' NAMEVALUE nodes, indexed on the text after 'Price' in the NAMEVALUE/NAME node. This is used in the 'From' NAMEVALUE node template, to establish the linkage between 'From' and 'Price' nodes.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:key name="prices" match="/ITEM/NAMEVALUE[starts-with(NAME/text(), 'Price')]" use="substring-after(NAME/text(), 'Price')"/>
<xsl:template match="/">
<prices>
<xsl:apply-templates select="//NAMEVALUE"/>
</prices>
</xsl:template><xsl:template match="NAMEVALUE[starts-with(NAME/text(), 'From')]">
<Price from="{VALUE/text()}">
<xsl:value-of select="key('prices', substring-after(NAME/text(), 'From'))/VALUE"/>
</Price>
</xsl:template><xsl:template match="NAMEVALUE"/>
</xsl:stylesheet>
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
They're related due to the name From1 is connected to Price1 and From2 to Price2 and so forth. It is an unfortunate format, but it is what I have to work with :)
--------------------------- Blogging about SQL Server and related topics.
Alsvha wrote:
It is an unfortunate format
Yes. I can't help you exercise the demons from the people that created that XML nightmare, however Stuart has provided an example for you. :laugh: :laugh: exercise exorcise
modified on Thursday, June 4, 2009 1:45 PM
-
The following works (i.e. I've tested it) in Microsoft XSLT - but it. It creates a key containing all 'Price' NAMEVALUE nodes, indexed on the text after 'Price' in the NAMEVALUE/NAME node. This is used in the 'From' NAMEVALUE node template, to establish the linkage between 'From' and 'Price' nodes.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:key name="prices" match="/ITEM/NAMEVALUE[starts-with(NAME/text(), 'Price')]" use="substring-after(NAME/text(), 'Price')"/>
<xsl:template match="/">
<prices>
<xsl:apply-templates select="//NAMEVALUE"/>
</prices>
</xsl:template><xsl:template match="NAMEVALUE[starts-with(NAME/text(), 'From')]">
<Price from="{VALUE/text()}">
<xsl:value-of select="key('prices', substring-after(NAME/text(), 'From'))/VALUE"/>
</Price>
</xsl:template><xsl:template match="NAMEVALUE"/>
</xsl:stylesheet>
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Thank you so much - I'll try to take a look at it.
--------------------------- Blogging about SQL Server and related topics.