xsl-list
[Top] [All Lists]

Re: [xsl] Noobie: normalize <b><a>...</a></b> to <a><b>...</b></a>

2010-02-19 10:57:38
  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/


      


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>