Create multiple records when child has multiple values.
I have an XML with parent and child nodes. Child nodes do have multiple values. I am trying to create separate records for each child nodes such that the tags are in the order displayed in the output (Desired).
My XML input is:
<Records count="65">
<Record contentId="781435" levelId="17" levelGuid="33fceb92-9ee6-458f-81f6-5bd28e4af22e" moduleId="70" parentId="0">
<Field id="15941" guid="75b528ad-2e19-42d6-9512-87b92bbf84d0" type="1">SPH0</Field>
<Field id="15997" guid="90507e16-35a1-407e-8b27-586e3e091ac3" type="9">
<Reference id="409826">Alberta</Reference>
</Field>
</Record>
<Record contentId="783299" levelId="17" levelGuid="33fceb92-9ee6-458f-81f6-5bd28e4af22e" moduleId="70" parentId="0">
<Field id="15941" guid="75b528ad-2e19-42d6-9512-87b92bbf84d0" type="1">SQV0</Field>
<Field id="15997" guid="90507e16-35a1-407e-8b27-586e3e091ac3" type="9">
<Reference id="409187">Ontario</Reference>
<Reference id="409826">Quebec</Reference>
</Field>
</Record>
</Records>
My XSLT code which I tried is:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Records">
<xsl:for-each select="Record">
<ArcherRecord>
<AppCode>
<xsl:value-of select="Field" />
</AppCode>
<xsl:for-each select="Field/Reference">
<Location>
<xsl:value-of select="." />
</Location>
</xsl:for-each>
</ArcherRecord>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
But the output I got is:
<ArcherRecord>
<AppCode>SPH0</AppCode>
<Location>Alberta</Location>
</ArcherRecord>
<ArcherRecord>
<AppCode>SQV0</AppCode>
<Location>Ontario</Location>
<Location>Quebec</Location>
</ArcherRecord>
instead of the output I desire which would look like this:
<?xml version="1.0" encoding="UTF-8"?>
<ArcherRecord>
<AppCode>SPH0</AppCode>
<Location>Alberta</Location>
</ArcherRecord>
<ArcherRecord>
<AppCode>SQV0</AppCode>
<Location>Ontario</Location>
</ArcherRecord>
<ArcherRecord>
<AppCode>SQV0</AppCode>
<Location>Quebec</Location>
</ArcherRecord>
Please try this:
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/Records">
<xsl:for-each select="Record/Field/Reference">
<ArcherRecord>
<AppCode>
<xsl:value-of select="../../Field[@type='1']" />
</AppCode>
<Location>
<xsl:value-of select="." />
</Location>
</ArcherRecord>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The output is:
<ArcherRecord>
<AppCode>SPH0</AppCode>
<Location>Alberta</Location>
</ArcherRecord>
<ArcherRecord>
<AppCode>SQV0</AppCode>
<Location>Ontario</Location>
</ArcherRecord>
<ArcherRecord>
<AppCode>SQV0</AppCode>
<Location>Quebec</Location>
</ArcherRecord>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.