![]() |
|
#1
|
|||
|
|||
|
how to clean up empty elements after a transformation?
Hi,
I'm writing a mapping transformation between two schemas, both having about 600 elements each, all optional. There are documents that will use as many as all 600 and there are documents that will as few as 10 elements. If an element is not included in the source document, it should not appear in the output document either. There are two ways I can think of to achieve this: 1. surrounding every element's mapping with an 'if' 2. once this transformation is done, running another transformation that will strip all empty elements. Are there other easier solutions I'm not thinking of? Thanks Dmitry |
| Sponsored Links |
|
#2
|
|||
|
|||
|
Hope the below code gives you a hint to achieve what you need.
Code:
<xsl:template match="Flow"> <transform_tag> <xsl:apply-templates/> </transform_tag> </xsl:template> <xsl:template match="*[. = '']"> </xsl:template> <xsl:template match="empty"> <empty_tag> <xsl:text>Check, this text wont show up</xsl:text> <xsl:apply-templates/> </empty_tag> </xsl:template> <xsl:template match="*"> <xsl:copy-of select="."/> </xsl:template> Code:
<Flow> <Para>test</Para> <Para>test</Para> <Para>test</Para> <empty/> </Flow> Code:
<xsl:template match="*[. = '']"> </xsl:template> |
|
#3
|
|||
|
|||
|
Quote:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="foo">
<bar>
<xsl:apply-templates/>
</bar>
</xsl:template>
<xsl:template match="whatever">
<whatelse>
<xsl:apply-templates/>
</whatelse>
</xsl:template>
</xsl:stylesheet>
|
|
#4
|
|||
|
|||
|
Hmm... I haven't thought of this. Writing 600 templates? Wouldn't I also need as many apply-templates calls since the structure/groupings of elements in the source schema is different from that of the target schema?
|
|
#5
|
|||
|
|||
|
As for the apply-templates, if you use XSLT 2.0 then you can specify the order by using a sequence e.g.
Code:
<xsl:apply-templates select="bar, foo"/> With XSLT 1.0 you don't have that possibility, if you don't want to process in document order then you will indeed need e.g. Code:
<xsl:apply-templates select="bar"/> <xsl:apply-templates select="foo"/> |
|
#6
|
|||
|
|||
|
Quote:
The source xml has about 15 top level elements under which all others are grouped. I have a template for each of the top elements and then some (whenever I need to do a gnarly pivoting transformation on a group of related elements, I use a separate template). |
![]() |
| Thread Tools | |
| Display Modes | |
|
|