XML/XSLT stylesheet/perform sort

Материал из Web эксперт
Версия от 11:26, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

perform-sort and sequence

File: Data.xml
<?xml version="1.0"?>
<addressbook>
  <address>
    <name>
      <title>Mr.</title>
      <first-name>A</first-name>
      <last-name>B</last-name>
    </name>
    <street>1234 Main Street</street>
    <city>Regina</city>
    <state>WI</state>
    <zip>48392</zip>
  </address>
</addressbook>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:variable name="sortedCities" as="xs:string*">
      <xsl:perform-sort>
        <xsl:sort select="."/>
        <xsl:apply-templates select="addressbook/address/city"/>
        <xsl:sequence select="("London", "Adelaide", "Rome")"/>
        <xsl:sequence select="("Jakarta", "Sao Paulo", "Timbuktu")"/>
      </xsl:perform-sort>
    </xsl:variable>
    <xsl:text>Our customers live in these cities:&#xA;&#xA;</xsl:text>
    <xsl:value-of select="$sortedCities" separator="&#xA;"/>
  </xsl:template>
</xsl:stylesheet>
Output:
Our customers live in these cities:
Adelaide
Jakarta
London
Regina
Rome
Sao Paulo
Timbuktu



perform-sort demo

File: Data.xml
<?xml version="1.0"?>
<addressbook>
  <address>
    <name>
      <title>Mr.</title>
      <first-name>A</first-name>
      <last-name>B</last-name>
    </name>
    <street>1234 Main Street</street>
    <city>Vancouver</city>
    <state>WI</state>
    <zip>48392</zip>
  </address>
</addressbook>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:variable name="sortedCities" as="xs:string*">
      <xsl:perform-sort>
        <xsl:sort select="."/>
        <xsl:sequence select="addressbook/address/city"/>
      </xsl:perform-sort>
    </xsl:variable>
    <xsl:text>Our customers live in these cities:&#xA;&#xA;</xsl:text>
    <xsl:value-of select="$sortedCities" separator="&#xA;"/>
  </xsl:template>
</xsl:stylesheet>
Output:
Our customers live in these cities:
Vancouver