Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

Stuck with fetching first and last child element using XSLT

$
0
0

Hi,

Below is the XML sample and I'm trying to pull first and last child element values using XSLT.

<Records count="1"><Metadata><FieldDefinitions><FieldDefinition id="1566" guid="f1c5f114-d4db-4737-a7ae-56d9f1832b9e" name="Standard Name" alias="Standard_Name" /><FieldDefinition id="1568" guid="34535e5d-2289-4b5d-bd2b-25864b635aa5" name="Standard ID" alias="Standard_ID" /><FieldDefinition id="1569" guid="e393cfb0-840a-4256-be8b-073700ac73be" name="Statement" alias="Control_Std_Statement" /><FieldDefinition id="4474" guid="ea74f9d5-5ea6-4daf-9123-8a1930716b1a" name="Adopt or Not?" alias="Adopt_or_Not" /><FieldDefinition id="67" guid="4ee1b5a4-59d8-4866-a447-47b897be8573" name="Source Name" alias="Source_Name" /><FieldDefinition id="74" guid="d67f344b-bbc9-4f0f-a991-2d7b776430ec" name="Topic Name" alias="Topic_Name" /><FieldDefinition id="83" guid="d12f2e77-4901-49bb-aa96-4d2f7113703a" name="Section Name" alias="Section_Name" /><FieldDefinition id="99" guid="f07b5fae-e065-4488-9284-cb489ce79bf8" name="Sub Section Name" alias="Sub_Section_Name" /></FieldDefinitions></Metadata><LevelCounts><LevelCount id="53" guid="260186b2-dd3d-43d7-8d8c-d8cd986af335" count="1161" /><LevelCount id="6" guid="eed9bc75-e37f-442e-94ab-e0e3ea79f999" count="3717" /><LevelCount id="7" guid="ff28895a-fdd4-4a07-8e01-760448fc2eb6" count="5425" /><LevelCount id="8" guid="5faea532-35d4-4587-9581-30c6859fc7df" count="7521" /><LevelCount id="9" guid="f9a67499-fe43-4249-9170-05fa7fb5b8e7" count="5278" /></LevelCounts><Record contentId="404" levelId="53" levelGuid="260186b2-dd3d-43d7-8d8c-d8cd986af335" moduleId="66" parentId="0"><Record contentId="710339" levelId="6" levelGuid="eed9bc75-e37f-442e-94ab-e0e3ea79f999" moduleId="67" parentId="0"><Record contentId="710344" levelId="7" levelGuid="ff28895a-fdd4-4a07-8e01-760448fc2eb6" moduleId="67" parentId="0"><Record contentId="710376" levelId="8" levelGuid="5faea532-35d4-4587-9581-30c6859fc7df" moduleId="67" parentId="0"><Field id="83" guid="d12f2e77-4901-49bb-aa96-4d2f7113703a" type="1">IS-01:  Management Program</Field></Record><Field id="74" guid="d67f344b-bbc9-4f0f-a991-2d7b776430ec" type="1">Information Security</Field></Record><Field id="67" guid="4ee1b5a4-59d8-4866-a447-47b897be8573" type="1">Cloud Security</Field></Record><Record contentId="284359" levelId="6" levelGuid="eed9bc75-e37f-442e-94ab-e0e3ea79f999" moduleId="67" parentId="0"><Record contentId="863" levelId="7" levelGuid="ff28895a-fdd4-4a07-8e01-760448fc2eb6" moduleId="67" parentId="0"><Record contentId="1043" levelId="8" levelGuid="5faea532-35d4-4587-9581-30c6859fc7df" moduleId="67" parentId="0"><Record contentId="1546" levelId="9" levelGuid="f9a67499-fe43-4249-9170-05fa7fb5b8e7" moduleId="67" parentId="0"><Field id="99" guid="f07b5fae-e065-4488-9284-cb489ce79bf8" type="1">(c)(1) Integrity</Field></Record><Field id="83" guid="d12f2e77-4901-49bb-aa96-4d2f7113703a" type="1">Integrity</Field></Record><Field id="74" guid="d67f344b-bbc9-4f0f-a991-2d7b776430ec" type="1">G. Technical Safeguard</Field></Record><Field id="67" guid="4ee1b5a4-59d8-4866-a447-47b897be8573" type="1">HIPAA: Security</Field></Record><Field id="1568" guid="34535e5d-2289-4b5d-bd2b-25864b635aa5" type="1">ATCS-001</Field><Field id="1566" guid="f1c5f114-d4db-4737-a7ae-56d9f1832b9e" type="1">Information Security</Field><Field id="1569" guid="e393cfb0-840a-4256-be8b-073700ac73be" type="1">Sample text goes here</Field><Field id="4474" guid="ea74f9d5-5ea6-4daf-9123-8a1930716b1a" type="4"><ListValues><ListValue id="4124" displayName="Adopt">Adopt</ListValue></ListValues></Field></Record>


The final output should be like

<Records xmlns="http://www.archer-tech.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Record><Standard_ID>ATCS-001</Standard_ID><Standard_Name>Information Security</Standard_Name><Control_Std_Statement>Sample text goes here</Control_Std_Statement><Adopt_or_Not>Adopt</Adopt_or_Not><Source_Name>Cloud Security</Source_Name><Section_Name>IS-01:  Management Program</Section_Name><Source_Name>HIPAA: Security</Source_Name><Sub_Section_Name>(c)(1) Integrity</Sub_Section_Name></Record></Records>

The xslt I tried building is

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns="http://www.archer-tech.com/"><xsl:output method="xml" indent="yes"/><xsl:variable name ="fields" select="//Metadata/FieldDefinitions" /><!--match the root node--><xsl:template match="Records"><Records ><xsl:apply-templates select="Record" /></Records></xsl:template><!--match record nodes and put in the id field--><xsl:template match="Record" ><Record><xsl:apply-templates select="@id" /><xsl:apply-templates select="Field" /><xsl:apply-templates select="Record" /></Record></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 child relationships --><xsl:template match="Record/Record"><xsl:apply-templates select="Field" /><xsl:apply-templates select="Record" /><!--<xsl:variable name="total-el"><xsl:value-of select="count(descendant::Record)"/></xsl:variable>--><!--<xsl:choose><xsl:when test="(position()=1 or position()=$total-el)"><xsl:apply-templates select="Field" /><xsl:apply-templates select="Record" /></xsl:when><xsl:otherwise><xsl:apply-templates select="Record" /></xsl:otherwise></xsl:choose>--><!--<xsl:if test="(position()=1 or position()=$total-el)"><xsl:apply-templates select="Field" /></xsl:if><Item><xsl:value-of select="$total-el"/></Item><xsl:apply-templates select="Field" />--><xsl:for-each select="*"><xsl:variable name="total" select="position()" /><Count><xsl:value-of select="$total"/></Count><!--<xsl:if test="($total=$total-el)"><xsl:apply-templates select="Record" /></xsl:if>--></xsl:for-each></xsl:template><!--match basic field types--><xsl:template match="Field[@type='1' or @type='2' or @type='3']" ><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://www.w3.org/2001/XMLSchema-instance">true</xsl:attribute></xsl:if><xsl:if test="."><xsl:value-of select="."/></xsl:if></xsl:element></xsl:template><!--values list field type--><xsl:template match="Field[@type='4']" ><xsl:variable name="fieldName"><xsl:call-template name="getName"><xsl:with-param name="fieldId" select="@id" /></xsl:call-template></xsl:variable><xsl:for-each select="ListValues/ListValue"><xsl:element name="{$fieldName}"><xsl:value-of select="@displayName" /></xsl:element></xsl:for-each></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>

I'm stuck at getting just first child and last child element. Please if someone can help me out. Thank you and wish you a Happy New year.

Thanks,

Prem


Prem


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>