Hi, everyone.I am new to this forum and XSL in general. I am wondering if anyone can help me with a problem.I am trying to do an XML to XML transformation with the following input:
<item-1>Item1</item-1><elementA/><item-code-1>123</item-code-1><item-price-2>34.00</item-price-2><elementB/><item-code-2>456</item-code-2><item-price-1>35.00</item-price-1><elementC/><item-2>Item2</item-2>
The output should extract the any of the data on items and create new Item elements in the output xml:
<Item num=1> <Name>Item1</Name> <Code>123</Code> <Price>35.00</Price></Item><Item num=2> <Name>Item2</Name> <Code>456</Code> <Price>34.00</Price></Item>
The problem is, the order of the elements in the incoming xml is not always the same and the number of items are unknown. I have tried sorting the elements first:
<item-code-1>123</item-code-1><item-name-1>Item1</item-name-1><item-price-1>35.00</item-price-1><item-code-2>456</item-code-2><item-name-2>Item2</item-name-2><item-price-2>34.00</item-price-2><elementA/><elementB/><elementC/>
And then do this in my xslt:
<xsl:for-each select="*"><xsl:sort select="substring(name(), string-length(name()))"/><xsl:sort select="name()"/><xsl:if test="starts-with(name(), 'item-code')"><Item><Code><xsl:value-of select="."/></Code></xsl:if><xsl:if test="starts-with(name(), 'item-name')"><Name><xsl:value-of select="."/></Name></xsl:if><xsl:if test="starts-with(name(), 'item-price')"><Price><xsl:value-of select="."/></Price></Item></xsl:if></xsl:for-each>
But the code doesn't like having the <Item> tag incomplete within the first xsl:if statement. I am thinking of building the entire Item element within a single xsl:if statement, but I am not sure how to get the data of the proceeding sibling nodes to do so.Any tips will be much appreciated.