[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: flat xml tree to indent one

Subject: Re: flat xml tree to indent one
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 21 Dec 2010 12:17:35 -0500
Re:  flat xml tree to indent one
Hi,

Mike's elegant 2.0 recursive variable processing can also be extended easily to support a more granular mapping of attribute/value combinations to elements, something like this:

<xsl:template match="p[@*]">
  <xsl:variable name="temp">
    <p>
      <xsl:copy-of select="remove(@*, 1)"/>
      <xsl:apply-templates select="@*[1]" mode="map-attribute"/>
    </p>
  </xsl:variable>
  <xsl:apply-templates select="$temp"/>
</xsl:template>

<xsl:template match="p[not(@*)]">
  <xsl:sequence select="."/>
</xsl:template>

<xsl:template match="@*" mode="map-attribute">
  <xsl:element name="{name(.)}">
    <xsl:copy-of select="../node()"/>
  </xsl:element>
</xsl:template>

<xsl:template match="@style[.='bold']" mode="map-attribute">
  <b>
    <xsl:copy-of select="../node()"/>
  </b>
</xsl:template>

Here's an old XSLT 1.0 precursor:

http://markmail.org/message/nxrz4kk42lr4jxo2


Cheers, Wendell

On 12/21/2010 7:47 AM, Michael Kay wrote:
On 21/12/2010 12:30, Matthieu Ricaud-Dussarget wrote:
Hi all,

This is a grouping question one more time.
It's not actually a grouping problem as commonly understood.

For such an input :


<p foo="foo_att" bar="bar_att" foobar="foobar_att">my text</p>

I'd like such an output :

<p>
<foo><bar><foobar>my text</foobar></bar></foo>
</p>

the order foo/bar/foobar isn't important here.
<foobar><bar><foo>my text</foo></bar></foobar> would also be ok.

This should work for any number of attributes (including none)

You need to process the attributes one at a time, recursively:

<xsl:template match="p[@*]">
<xsl:variable name="temp">
<p>
<xsl:copy-of select="remove(@*, 1)"/>
<xsl:element name="{name(@*[1])}">
<xsl:copy-of select="child::node()"/>
</xsl:element>
</p>
</xsl:variable>
<xsl:apply-templates select="$temp"/>
</xsl:template>

<xsl:template match="p[not(@*)]">
<xsl:sequence select="."/>
</xsl:template>


--
======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2011 All Rights Reserved.