[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Noobie: normalize ... to ...


Subject: Re: [xsl] Noobie: normalize <b><a>...</a></b> to <a><b>...</b></a>
From: Florent Georges <lists@xxxxxxxxxxxx>
Date: Fri, 19 Feb 2010 16:57:16 +0000 (GMT)

  Hi,

> 1.  <b> can contain mixed text, in which case nothing should be
>
changed.
> 2.  <b><a>...</a></b> should be changed to <a><b>...</b></a>
>
only if the <a>...</a> element is the unique child node of
>     <b>...</b>
The following transform, based on the Modified Identity
Transform pattern,
should do that (not tested though):

    <xsl:template match="node()">
<xsl:copy>
          <xsl:copy-of select="@*"/>
          <xsl:apply-templates
select="node()"/>
       </xsl:copy>
    </xsl:template>

    <xsl:template
match="b[count(a) eq 1][empty(node() except a)]">
       <a>
          <b>
<xsl:apply-templates select="a/node()"/>
          </b>
       </a>
</xsl:template>

  This is simple: the first rule catches every node and
copies it,
then continues navigating through its descendents.  But the second
rule applies when the transform go through a special node: a "b"
element with
exactly one child (an "a" element).  In that specific
case it creates two
elements (a "b" within an "a") and continues
navigating through "a"'s
descendents.

  Regards,

-- 
Florent Georges
http://www.fgeorges.org/


Current Thread