Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


RE: [xsl] Empty nodes AND attributes stripping - I'm so lost - SOLUTION

From: "Passin, Tom" <tpassin@------------>
To:
Date: 9/2/2004 6:48:00 PM
> From: Kevinjohn Gallagher [mailto:web_design_guru@xxxxxxxxxxx]
>
> Firstly, thanks for all the help.
>
> I'm including my solution here so others might benefit.
>
> <?xml version="1.0" encoding="UTF-16"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
> <xsl:output method="xml" />
> <xsl:strip-space elements="*"/>
>
> 	<xsl:template match="//parent/myContent//@*|node()">
> 		<xsl:copy>
> 			<xsl:apply-templates select="@*|node()"/>
> 		</xsl:copy>
> 	</xsl:template>
>
> 	<xsl:template match="@*[.='']"/>
> 	<xsl:template match="//parent/myContent/*[.='' and
> not(attribute::*)]"/>
>
> </xsl:stylesheet>

Some suggestions... No need to write //parent, because parent is already
the document element.  /parent will do fine, an avoid possibly multiple
scanning of the whole tree.

In the third template match, you can just write
   match="*[.='' and not(@*)]"

Actually, your stylesheet does copy empty elements (if they have no
attributes), for example, if you add an empty element as an immediate
child of a myContent element, and I don't think that is what you want

However, you can simplify this further (and eliminate the empty child
element problem mentioned above) by doing all your filtering in the
select specification, like this -

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>

<xsl:template match="/parent/myContent//@*|node()">
   <xsl:copy>
      <xsl:apply-templates
     select="myContent | @*[.!='']|node()[@*[. != ''] or not(.='')]"/>
   </xsl:copy>
</xsl:template>

</xsl:stylesheet>

This selects for copying only those attributes that have text, or those
nodes that either have non-empty attributes or non-empty content, or all
myContent elements even if they are empty.  I believe that this is what
you want, and if not, it should be easy to adjust the filter.

The principle here is to select just the nodes you want, where that is
possible.  The selection works as simply as this here because of the
//@* |node() in the initial select.  Thus, all nodes of whatever depth
selected in the inner apply-templates get handled by the same template.
This way, there is no need for your second and third templates at all,
the one template handles everything.

 Sometimes you have to make sure that the default templates do not kick
in, but in this example there is no such problem.

BTW, the value of doing as much filtering as possible in your select
specifications is something I learned from this list, and it has stood
me in good stead.

Cheers,

Tom P


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent