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

Subject: Re: [xsl] Noobie: normalize <b><a>...</a></b> to <a><b>...</b></a>
From: Syd Bauman <Syd_Bauman@xxxxxxxxx>
Date: Thu, 18 Feb 2010 13:12:41 -0500
> I'm converting a non-XML data-dump into XML,

If it's really non-XML data, then XSLT can't help you. I presume
(since you're asking here), that you've munged it into XML somehow.

> and the document contains examples of both
>    <a><b>...</b></a>
> and
>    <b><a>...</a></b>
> which (in this document) are equivalent. I'd like to use XSLT to
> convert all examples of the latter to the former, with the
> following caveats:
>  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>
> How can I do this?

I am sure others can provide better methods, but I *think* the
following will do what you ask:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="";
  <xsl:template match="/">
    <xsl:apply-templates select="node()"/>
  <xsl:template match="@*|node()">
      <xsl:apply-templates select="@*|node()"/>
  <xsl:template match="b[not(text())][a][not(*[2])]">
    <a><b><xsl:apply-templates select="a" mode="slurp"/></b></a>
  <xsl:template match="a" mode="slurp">

Current Thread