D
delgados129
In a previous post (XSL to Flatten Selective Node in XML:
http://groups-beta.google.com/group...19953e7?q=delgados129&rnum=1#16104948219953e7)
I was skillfully pointed in the right direction with XSL logic to
flatten a selected node.
Given:
<A>
<B/>
<C>
<D>DText1</D>
<D>DText2</D>
<D>
<E>EText1</E>
</D>
<D>DText3</D>
<D>
<E>EText2</E>
</D>
</C>
</A>
Tranforming the XML with the following XSL:
<?xml version="1.0" encoding="UTF-16"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl
utput method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="C">
<xsl:apply-templates mode="flatten">
<xsl:with-param name="currentNode" select="'C'"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*" mode="flatten">
<xsl
aram name="currentNode"/>
<xsl:variable name="delimitter" select="'-'" />
<xsl:if test="text()">
<xsl:element name="{$currentNode}{$delimitter}{name()}">
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:if>
<xsl:apply-templates select="*" mode="flatten">
<xsl:with-param name="currentNode"
select="concat($currentNode,$delimitter,name())"/>
</xsl:apply-templates>
</xsl:template>
</xsl:transform>
Will yield:
<A>
<B/>
<C-D>DText1</C-D>
<C-D>DText2</C-D>
<C-D-E>EText1</C-D-E>
<C-D>DText3</C-D>
<C-D-E>EText2</C-D-E>
</A>
Any suggestions on enhancing the transformation to yield the
grandchildren's (great grandchild, great-great grandchild, etc.)
relative element name count. (Note that text 'DText3' within its parent
node is not necessarily an immediate sequential sibling to its logical
sibling.) This is only necessary, as noted in the XSL, for terminating
elements with text. To be more specific, the transform should then
yield:
<A>
<B/>
<C-D-1>DText1</C-D-1>
<C-D-2>DText2</C-D-2>
<C-D-E-1>EText1</C-D-E-1>
<C-D-3>DText3</C-D-3>
<C-D-E-2>EText2</C-D-E-2>
</A>
BTW:
A heartfelt thanks to all that contribute to making this group so
helpful. I enjoy reading through many of the solutions provided here.
http://groups-beta.google.com/group...19953e7?q=delgados129&rnum=1#16104948219953e7)
I was skillfully pointed in the right direction with XSL logic to
flatten a selected node.
Given:
<A>
<B/>
<C>
<D>DText1</D>
<D>DText2</D>
<D>
<E>EText1</E>
</D>
<D>DText3</D>
<D>
<E>EText2</E>
</D>
</C>
</A>
Tranforming the XML with the following XSL:
<?xml version="1.0" encoding="UTF-16"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="C">
<xsl:apply-templates mode="flatten">
<xsl:with-param name="currentNode" select="'C'"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*" mode="flatten">
<xsl
<xsl:variable name="delimitter" select="'-'" />
<xsl:if test="text()">
<xsl:element name="{$currentNode}{$delimitter}{name()}">
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:if>
<xsl:apply-templates select="*" mode="flatten">
<xsl:with-param name="currentNode"
select="concat($currentNode,$delimitter,name())"/>
</xsl:apply-templates>
</xsl:template>
</xsl:transform>
Will yield:
<A>
<B/>
<C-D>DText1</C-D>
<C-D>DText2</C-D>
<C-D-E>EText1</C-D-E>
<C-D>DText3</C-D>
<C-D-E>EText2</C-D-E>
</A>
Any suggestions on enhancing the transformation to yield the
grandchildren's (great grandchild, great-great grandchild, etc.)
relative element name count. (Note that text 'DText3' within its parent
node is not necessarily an immediate sequential sibling to its logical
sibling.) This is only necessary, as noted in the XSL, for terminating
elements with text. To be more specific, the transform should then
yield:
<A>
<B/>
<C-D-1>DText1</C-D-1>
<C-D-2>DText2</C-D-2>
<C-D-E-1>EText1</C-D-E-1>
<C-D-3>DText3</C-D-3>
<C-D-E-2>EText2</C-D-E-2>
</A>
BTW:
A heartfelt thanks to all that contribute to making this group so
helpful. I enjoy reading through many of the solutions provided here.