XSL transformation for non-nested XML



This is some XML data that I have -



<?xml version="1.0" encoding="UTF-8"?>
<OrderDisplayRequest>
<facXML>
<faRecord>
<recordCode>11</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>2</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>5</itemMapCode>
<itemValue>TEXT FOR WORKCODE 1 IN ITEMS LIST. Adding a lot of extra text. This is so horrendous. I hate this XML structure, this needs major tweaking so that we get XML that can be consumed properly. What is the</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>11</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>2</itemMapCode>
<itemValue>2</itemValue>
</item>
<item>
<itemMapCode>5</itemMapCode>
<itemValue>maximum amount of text that you can add at the workcode level?</itemValue>
</item>
</faRecord>
</facXML>
</OrderDisplayRequest>


There are two faRecord elements. The first two elements are the sequence number and subsequence number. This is the XSL I had before:



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://ift.tt/tCZ8VR" version="1.0">
<xsl:template match="OrderDisplayRequest">
<MCSResponse>
<OrderDisplayResponse>
<xsl:apply-templates select="facXML/faRecord[recordCode='11']" mode="OrderDisplayResponse" />
</OrderDisplayResponse>
</MCSResponse>
</xsl:template>
<xsl:template match="faRecord[recordCode='11']" mode="OrderDisplayResponse">
<TextValueDetail>
<xsl:apply-templates select="item" mode="TextValueDetail" />
</TextValueDetail>
</xsl:template>
<!--Text value detail for order -->
<xsl:template match="item[itemMapCode='1']" mode="TextValueDetail">
<SequenceNumber>
<xsl:value-of select="./itemValue" />
</SequenceNumber>
</xsl:template>
<xsl:template match="item[itemMapCode='2']" mode="TextValueDetail">
<SubsequenceNumber>
<xsl:value-of select="./itemValue" />
</SubsequenceNumber>
</xsl:template>
<xsl:template match="item[itemMapCode='5']" mode="TextValueDetail">
<ItemisationText>
<xsl:value-of select="./itemValue" />
</ItemisationText>
</xsl:template>
<xsl:template match="*" mode="TextValueDetail" />
<!--Ignore all other data-->
</xsl:stylesheet>


This will generate two TextValueDetail elements - a 'flat' structure when in fact the text in both elements belongs to the same Workcode. Is there any way using Keys or something else that I can generate an output that looks like this instead - with multiple ItemisationText elements inside a single TextValueDetail element, using the SequenceNumber and SubSequenceNumber fields. TIA! [I am trying SequenceNumber same and SubSequenceNumber greater than 1 as some sort of a key but I am struggling.]



<?xml version="1.0" encoding="UTF-8"?>
<MCSResponse>
<OrderDisplayResponse>
<TextValueDetail>
<SequenceNumber>1</SequenceNumber>
<SubsequenceNumber>1</SubsequenceNumber>
<ItemisationText>TEXT FOR WORKCODE 1 IN ITEMS LIST. Adding a lot of extra text. This is so horrendous. I hate this XML structure, this needs major tweaking so that we get XML that can be consumed properly. What is the</ItemisationText>
<ItemisationText>maximum amount of text that you can add at the workcode level?</ItemisationText>
</TextValueDetail>
</OrderDisplayResponse>
</MCSResponse>

No comments:

Post a Comment