Re: [xsl] Re-arranging an XML file

Subject: Re: [xsl] Re-arranging an XML file
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Tue, 13 Jan 2009 11:17:35 -0500
Hi,

Or, if using XSLT 2.0, you could approach this as a grouping problem (which it basically is, it's just disguised in 1.0):

<?xml version="1.0" encoding="UTF-8"?>
<Catalog>
  <cds>
    <cd>
      <number>000001</number>
      <title>Empire Burlesque</title>
    </cd>
    <cd>
      <number>000002</number>
      <title>Hide Your Heart</title>
    </cd>
  </cds>
  <artists>
    <artist>
      <number>000001</number>
      <name>Bob Dylan</name>
    </artist>
    <artist>
      <number>000002</number>
      <name>Bonnie Tyler</name>
    </artist>
  </artists>
 </Catalog>

<xsl:template match="Catalog"> <xsl:copy> <xsl:for-each-group select="cds/cd | artists/artist" group-by="number"> <cd> <xsl:apply-templates select="current-group()/(title, name)"/> </cd> </xsl:for-each-group> </xsl:copy> </xsl:template>

<xsl:template match="title">
  <xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="artist/name">
  <artist>
    <xsl:apply-templates/>
  </artist>
</xsl:template>

Cheers,
Wendell


Is there an easy way for an XSL rookie to transform it to look more like this?

<?xml version="1.0" encoding="UTF-8"?>
 <Catalog>
   <cd>
     <title>Empire Burlesque</title>
     <artist>Bob Dylan</artist>
    </cd>
    <cd>
      <title>Hide Your Heart</title>
    <artist>Bonnie Tyler></artist>
  </cd>
</Catalog>



====================================================================== 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