I have a requirement where there is a XML structure with a root element having 2 child element of array type and Sample request structure like below
<Root>
<Header>
<Inv>12</Inv>
</Header>
<Detail>
<Val>aa</Val>
<Line>1</Line>
</Detail>
<Header>
<Inv>15</Inv>
</Header>
<Detail>
<Val>bb</Val>
<Line>2</Line>
</Detail>
</Root>
I have to get response like below:
<CreateInvoice>
<Data>
<Invoice>
<Inv>12</Inv>
<InvoiceLine>
<Val>aa</Val>
<Line>1</Line>
</InvoiceLine>
</Invoice>
</Data>
<Data>
<Invoice>
<Inv>15</Inv>
<InvoiceLine>
<Val>bb</Val>
<Line>2</Line>
</InvoiceLine>
</Invoice>
</Data>
</CreateInvoice>
I tried using nested for-each on Data , but not able to get the response. Either only inv is populating or InvoiceLine is populating.
If each invoice has exactly one Header
and one Detail
then you can do simply:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Root">
<CreateInvoice>
<xsl:for-each select="Header">
<Data>
<Invoice>
<xsl:copy-of select="Inv"/>
<InvoiceLine>
<xsl:copy-of select="following-sibling::Detail[1]/*"/>
</InvoiceLine>
</Invoice>
</Data>
</xsl:for-each>
</CreateInvoice>
</xsl:template>
</xsl:stylesheet>