Reputation: 3
I have an xml wit repeating ITEM segment and its attributes needs to be handled using XSLT 1.0
<TransportationRequestQuotation>
<Item>
<GrossWeightTransportationQuantity>
<unitCode>KG</unitCode>
<text>60</text>
</GrossWeightTransportationQuantity>
<InsuranceDeclaredAmount>
<currencyCode>SAR</currencyCode>
<text>5000</text>
</InsuranceDeclaredAmount>
</Item>
<Item>
<GrossWeightTransportationQuantity>
<unitCode>KG</unitCode>
<text>80</text>
</GrossWeightTransportationQuantity>
<InsuranceDeclaredAmount>
<currencyCode>SAR</currencyCode>
<text>5000</text>
</InsuranceDeclaredAmount>
</Item>
</TransportationRequestQuotation>
The Expected xml is as below using the XSLT code:
<TransportationRequestQuotation>
<Item>
<GrossWeightTransportationQuantity unitCode="KG">60</GrossWeightTransportationQuantity>
<InsuranceDeclaredAmount currencyCode="SAR">5000</InsuranceDeclaredAmount>
</Item>
<Item>
<GrossWeightTransportationQuantity unitCode="KG">80</GrossWeightTransportationQuantity>
<InsuranceDeclaredAmount currencyCode="SAR">5000</InsuranceDeclaredAmount>
</Item>
</TransportationRequestQuotation>
I have used the below codes but unable to get the desired output :
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://sap.com/xi/SAPGlobal20/Global">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="GrossWeightTransportationQuantity">
<xsl:element name="GrossWeightTransportationQuantity">
<xsl:for-each select="*">
<xsl:attribute name="{name()}" >
<xsl:value-of select="text()" />
</xsl:attribute>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="InsuranceDeclaredAmount">
<xsl:element name="InsuranceDeclaredAmount">
<xsl:for-each select="*">
<xsl:attribute name="{name()}" >
<xsl:value-of select="text()" />
</xsl:attribute>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Requesting to help me handling repeating values in ITEM attributes level .
Upvotes: 0
Views: 223
Reputation: 52888
What you could do is apply-templates to children of GrossWeightTransportationQuantity
and InsuranceDeclaredAmount
except for text
and change those to attributes. Then just output the value of text
.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="GrossWeightTransportationQuantity|InsuranceDeclaredAmount">
<xsl:copy>
<xsl:apply-templates select="@*|node()[not(self::text)]"/>
<xsl:value-of select="text"/>
</xsl:copy>
</xsl:template>
<xsl:template match="GrossWeightTransportationQuantity/*|InsuranceDeclaredAmount/*">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1