<Records count="1">
<Metadata>
<FieldDefinitions>
<FieldDefinition id="25675" name="GrandpaID" alias="GrandpaID" />
<FieldDefinition id="123" name="Father ID" alias="FatherID" />
<FieldDefinition id="1923" name="Son ID" alias="SonID" />
</FieldDefinitions>
</Metadata>
<LevelCounts>
<LevelCount id="1" count="2" />
<LevelCount id="2" count="2" />
<LevelCount id="3" count="3" />
</LevelCounts>
<Record contentId="578859" levelId="1" moduleId="648" parentId="0">
<Record contentId="138286" levelId="2" moduleId="68" parentId="0">
<Record contentId="107826" levelId="3" moduleId="152" parentId="0">
<Field id="1923" type="1">Grandson Record 1</Field>
</Record>
<Record contentId="107830" levelId="3" moduleId="152" parentId="0">
<Field id="1923" type="1">Grandson Record 2</Field>
</Record>
<Field id="123" type="1">Son Record</Field>
</Record>
<Field id="25675" type="6">Grandpa Record</Field>
</Record>
</Records>
I have the aforementioned XML. What I need to do is look at each "Grandson Record" and create a record for each. That is to say that for each Grandson Record found under Grandpa/Son, I need Grandpa/Son/Grandson 1 and a second one for Grandpa/Son/Grandson 2. I have the following XSLT that gives me both Grandson Records at the same time.
XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsi="http://ift.tt/ra1lAU" xmlns:xsl="http://ift.tt/tCZ8VR" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name ="fields" select="//Metadata/FieldDefinitions" />
<!--match the root node-->
<xsl:template match="Records">
<ArcherRecords >
<xsl:apply-templates select="Record" />
</ArcherRecords>
</xsl:template>
<!-- match child relationships -->
<xsl:template match="Record">
<xsl:variable name="fieldName" select="translate(@levelId, ': ', '__')" />
<xsl:element name="Relationship_{$fieldName}">
<xsl:apply-templates select="@contentId" />
<xsl:apply-templates select="Field" />
<xsl:apply-templates select="Record" />
</xsl:element>
</xsl:template>
<!--get field name-->
<xsl:template name="getName">
<xsl:param name="fieldId" />
<xsl:choose>
<xsl:when test="$fields/FieldDefinition[@id=$fieldId]">
<xsl:value-of select="$fields/FieldDefinition[@id=$fieldId]/@alias"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'Field_'"/>
<xsl:value-of select="translate(@id, ': ', '__')" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--match basic field types-->
<xsl:template match="Field[@type='1' or @type='2' or @type='3' or @type='6' or @type='19' or @type='20' or @type='21' or @type='22']" >
<xsl:variable name="fieldName">
<xsl:call-template name="getName">
<xsl:with-param name="fieldId" select="@id" />
</xsl:call-template>
</xsl:variable>
<xsl:element name="{$fieldName}">
<xsl:if test=". = ''">
<xsl:attribute name="nil" namespace="http://ift.tt/ra1lAU">true</xsl:attribute>
</xsl:if>
<xsl:choose>
<xsl:when test="@xmlConvertedValue">
<xsl:value-of select="@xmlConvertedValue" />
</xsl:when>
<xsl:otherwise>
<xsl:if test=".">
<xsl:value-of select="."/>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</xsl:template>
<!--match all attributes and write them as a field-->
<xsl:template match="@*">
<xsl:element name="Field_{translate(name(), ': ', '__')}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Outcome:
<?xml version="1.0" encoding="UTF-8"?>
<ArcherRecords xmlns:xsi="http://ift.tt/ra1lAU">
<Relationship_1>
<Field_contentId>578859</Field_contentId>
<GrandpaID>Grandpa Record</GrandpaID>
<Relationship_2>
<Field_contentId>138286</Field_contentId>
<FatherID>Son Record</FatherID>
<Relationship_3>
<Field_contentId>107826</Field_contentId>
<SonID>Grandson Record 1</SonID>
</Relationship_3>
<Relationship_3>
<Field_contentId>107830</Field_contentId>
<SonID>Grandson Record 2</SonID>
</Relationship_3>
</Relationship_2>
</Relationship_1>
</ArcherRecords>
Desired Outcome:
<?xml version="1.0" encoding="UTF-8"?>
<ArcherRecords xmlns:xsi="http://ift.tt/ra1lAU">
<Relationship_1>
<Field_contentId>578859</Field_contentId>
<GrandpaID>Grandpa Record</GrandpaID>
<Relationship_2>
<Field_contentId>138286</Field_contentId>
<FatherID>Son Record</FatherID>
<Relationship_3>
<Field_contentId>107826</Field_contentId>
<SonID>Grandson Record 1</SonID>
</Relationship_3>
</Relationship_2>
</Relationship_1>
<Relationship_1>
<Field_contentId>578859</Field_contentId>
<GrandpaID>Grandpa Record</GrandpaID>
<Relationship_2>
<Field_contentId>138286</Field_contentId>
<FatherID>Son Record</FatherID>
<Relationship_3>
<Field_contentId>107830</Field_contentId>
<SonID>Grandson Record 2</SonID>
</Relationship_3>
</Relationship_2>
</Relationship_1>
</ArcherRecords>
Any help would be greatly appreciated!
No comments:
Post a Comment