Tizag Webmaster Forums  

Go Back   Tizag Webmaster Forums > Web Development > Markup Languages and Styling > XSLT Forum

Reply
 
Thread Tools Display Modes
  #1  
Old 11-06-2009, 10:46 AM
dberansky dberansky is offline
Curious Visitor
 
Join Date: Oct 2009
Posts: 6
dberansky will become famous soon enough
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
Reply With Quote
Sponsored Links
  #2  
Old 11-07-2009, 01:14 AM
rummy rummy is offline
Student
 
Join Date: Nov 2009
Location: Pondicherry, India
Posts: 110
rummy will become famous soon enough
Thumbs up

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>
Input XML:
Code:
<Flow>
	<Para>test</Para>
	<Para>test</Para>
	<Para>test</Para>
	<empty/>
</Flow>
Check the result, with and without the below code:
Code:
<xsl:template match="*[. = '']">
  </xsl:template>
Reply With Quote
  #3  
Old 11-07-2009, 05:14 AM
Martin Honnen Martin Honnen is offline
Student
 
Join Date: Aug 2009
Location: Germany
Posts: 301
Martin Honnen has a spectacular aura about
Quote:
Originally Posted by dberansky View Post
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
I am not sure I see the problem, if you simply write a template for each element mapping and use xsl:apply-templates to process the elements in the XML input then your stylesheet will only create result elements for those elements that exist in the input. Example:
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>
Now if the XML input has no 'foo' elements then the output will not contain any 'bar' elements either.
__________________
Martin Honnen
Microsoft MVP - Data Platform Development
My Blog
Reply With Quote
  #4  
Old 11-09-2009, 10:32 AM
dberansky dberansky is offline
Curious Visitor
 
Join Date: Oct 2009
Posts: 6
dberansky will become famous soon enough
Quote:
Originally Posted by Martin Honnen View Post
I am not sure I see the problem, if you simply write a template for each element mapping and use xsl:apply-templates to process the elements in the XML input then your stylesheet will only create result elements for those elements that exist in the input.
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?
Reply With Quote
  #5  
Old 11-09-2009, 10:43 AM
Martin Honnen Martin Honnen is offline
Student
 
Join Date: Aug 2009
Location: Germany
Posts: 301
Martin Honnen has a spectacular aura about
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"/>
will process 'bar' elements first, then 'foo' elements.
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"/>
How does your current stylesheet look, is that one big template handling all the different elements?
__________________
Martin Honnen
Microsoft MVP - Data Platform Development
My Blog
Reply With Quote
  #6  
Old 11-09-2009, 03:51 PM
dberansky dberansky is offline
Curious Visitor
 
Join Date: Oct 2009
Posts: 6
dberansky will become famous soon enough
Quote:
Originally Posted by Martin Honnen View Post
How does your current stylesheet look, is that one big template handling all the different elements?
I'm stuck in the 1.0 world unfortunately and indefinitely.

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).
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 04:13 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
© 2008, Tizag