XSL to flatten selective node in XML Doc

D

delgados129

I've seen a few XSL examples to flatten a specific node, but none that
address this specific scenario:

Given the following generic XML:

<DocElement>
<A attr="attribForA">TextForA</A>
<B>TextForB</B>
<C>
<D>TextForD1</D>
<E>TextForE1</E>
</C>
<C>
<D>TextForD2</D>
<E>
<F>TextForF</F>
</E>
</C>
</DocElement>

XSL should transform it at a designated node (example <C>) and flatten
any all all (all is important) children to the following:

<DocElement>
<A attr="attribForA">TextForA</A>
<B>TextForB</B>
<C_D>TextForD1</C_D>
<C_E>TextForE1</C_E>
<C_E_F>TextForF</C_E_F>
<!-- Continue on in this fashion as deep as the tree goes -->
</DocElement>


I've struggled with dynamically creating the new elements as a "_"
delimitted concatenation of all the child elements. Any and all help
would be appreciated.

Mario
 
D

David Carlisle

something like this:

flat.xml
========

<DocElement>
<A attr="attribForA">TextForA</A>
<B>TextForB</B>
<C>
<D>TextForD1</D>
<E>TextForE1</E>
</C>
<C>
<D>TextForD2</D>
<E>
<F>TextForF</F>
</E>
</C>
</DocElement>



flat.xsl
========

<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
<xsl:strip-space elements="*"/>
<xsl:eek:utput 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="a">
<xsl:with-param name="x" select="'C'"/>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="*" mode="a">
<xsl:param name="x"/>
<xsl:if test="text()">
<xsl:element name="{$x}_{name()}">
<xsl:value-of select="text()"/>
</xsl:element>
</xsl:if>
<xsl:apply-templates mode="a" select="*">
<xsl:with-param name="x" select="concat($x,'_',name())"/>
</xsl:apply-templates>
</xsl:template>

</xsl:stylesheet>






$ saxon flat.xml flat.xsl
<?xml version="1.0" encoding="utf-8"?>
<DocElement>
<A attr="attribForA">TextForA</A>
<B>TextForB</B>
<C_D>TextForD1</C_D>
<C_E>TextForE1</C_E>
<C_D>TextForD2</C_D>
<C_E_F>TextForF</C_E_F>
</DocElement>
 
D

delgados129

David:

Certainly an algorithm of elegance and succinctness! The quick
response is much appreciated. I hope I can return the favor at some
point.

Mario
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top