topical media & game development

talk show tell print

professional-xml-08-convert-convert.xsl / xsl



  <?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet version="2.0" 
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
          xmlns:xs="http://www.w3.org/2001/XMLSchema" 
          xmlns:fn="http://www.w3.org/2005/xpath-functions" 
          xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
          <xsl:import href="formats.xslt"/>
          <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" exclude-result-prefixes="fn xs xdt"/>
          <xsl:template match="/">
                  <products>
                          <xsl:for-each-group select="catalog/row" group-by="category">
                                  <xsl:sort select="category" data-type="text"/>
                                  <xsl:variable name="cat" select="category"/>
                                  <xsl:variable name="catclean" select="lower-case(replace(category, '\s+|\\|/', '_'))"/>
                                  <xsl:element name="{catclean}">
                                          <xsl:apply-templates select="//row[category=cat]">
                                          </xsl:apply-templates>
                                  </xsl:element>
                          </xsl:for-each-group>
                  </products>
          </xsl:template>
          <xsl:template match="row">
                  <product>
                          <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
                          <xsl:call-template name="prod"/>
                  </product>
          </xsl:template>
          <xsl:template match="product" name="prod">
                  <xsl:attribute name="name"><xsl:value-of select="product/name"/></xsl:attribute>
                  <xsl:attribute name="priceUSD"><xsl:value-of select="format-number(product/price, '#.00', 'USD')"/></xsl:attribute>
                  <xsl:attribute name="priceEuro"><xsl:value-of select="format-number(product/price, '€#,00', 'EURO')"/></xsl:attribute>
          </xsl:template>
  </xsl:stylesheet>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.