XML/XSLT stylesheet/key

Материал из Web эксперт
Перейти к: навигация, поиск

define and use key

File: Data.xml
<shirts>
  <colors>
    <color cid="c1">yellow</color>
    <color cid="c2">black</color>
    <color cid="c3">red</color>
    <color cid="c4">blue</color>
    <color cid="c5">purple</color>
    <color cid="c6">white</color>
    <color cid="c7">orange</color>
    <color cid="c7">green</color>
  </colors>
  <shirt colorCode="c4">item 1</shirt>
  <shirt colorCode="c1">item 2</shirt>
  <shirt colorCode="c6">item 3</shirt>
</shirts>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" />
  <xsl:key name="colorNumKey" match="color" use="@cid" />
  <xsl:template match="colors" />
  <xsl:template match="shirt">
    <xsl:value-of select="key("colorNumKey",@colorCode)" />
    <xsl:text> </xsl:text>
    <xsl:apply-templates />
  </xsl:template>
</xsl:stylesheet>
Output:

  
  blue item 1
  yellow item 2
  white item 3



key function with different parameters

File: Data.xml
<shirts>
  <colors>
   <color cid="c1">yellow</color>
   <color cid="c2">black</color>
   <color cid="c3">red</color>
   <color cid="c4">blue</color>
   <color cid="c5">purple</color>
   <color cid="c6">white</color>
   <color cid="c7">orange</color>
   <color cid="c7">green</color>
  </colors>
  <shirt colorCode="c4">item 1</shirt>
  <shirt colorCode="c1">item 2</shirt>
  <shirt colorCode="c6">item 3</shirt>
</shirts>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" />
  <xsl:key name="colorNumKey" match="color" use="@cid" />
  <xsl:key name="colorKey" match="color" use="." />
  <xsl:variable name="testVar">c4</xsl:variable>
  <xsl:variable name="keyName">colorKey</xsl:variable>
  <xsl:template match="colors">
    Looking up the color name with the color ID:
    c3"s color:
    <xsl:value-of select="key("colorNumKey","c3")" />
    c4"s color:
    <xsl:value-of select="key("colorNumKey",$testVar)" />
    c8"s color:
    <xsl:value-of select="key("colorNumKey","c8")" />
    c7"s colors:
    <xsl:for-each select="key("colorNumKey","c7")">
      <xsl:value-of select="." />
      <xsl:text> </xsl:text>
    </xsl:for-each>
    Looking up the color ID with the color name:
    blue"s cid:
    <xsl:value-of select="key("colorKey","blue")/@cid" />
    black"s cid:
    <xsl:value-of select="key($keyName,"black")/@cid" />
    gray"s cid:
    <xsl:value-of select="key("colorKey","gray")/@cid" />
  </xsl:template>
  <xsl:template match="shirt" />
</xsl:stylesheet>
Output:

  
    Looking up the color name with the color ID:
    c3"s color:
    red
    c4"s color:
    blue
    c8"s color:
    
    c7"s colors:
    orange green 
    Looking up the color ID with the color name:
    blue"s cid:
    c4
    black"s cid:
    c2
    gray"s cid:



match key

File: Data.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<itinerary>
  <day number="1">day 1</day>
  <day number="2">day 2</day>
  <day number="3">day 3</day>
  <day number="4">day 4</day>
  <day number="5">day 5</day>
  <day number="6">day 6</day>
  <day number="7">day 7</day>
  <day number="8">day 8</day>
  <day number="9">day 9</day>
</itinerary>
File: Transform.xslt

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:import href="itinerary.xsl" />
  <xsl:param name="highlight-day" as="xs:integer" required="yes" />
  <xsl:key name="day-number" match="day" use="xs:integer(@number)" />
  <xsl:template match="key("day-number", $highlight-day)//text()">
    <font color="red">
      <xsl:value-of select="." />
    </font>
  </xsl:template>
</xsl:stylesheet>

File: itinerary.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <head>
        <title>Itinerary</title>
      </head>
      <body>
        <center>
          <xsl:apply-templates select="//day" />
        </center>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="day">
    <h3>
      Day
      <xsl:value-of select="@number" />
    </h3>
    <p>
      <xsl:apply-templates />
    </p>
  </xsl:template>
</xsl:stylesheet>



Use variable in key function

File: Data.xml
<shirts>
  <shirt colorCode="c4">item 1</shirt>
  <shirt colorCode="c1">item 2</shirt>
  <shirt colorCode="c6">item 3</shirt>
</shirts>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" />
  <xsl:variable name="colorLookupDoc" select="document("Data.xml")" />
  <xsl:key name="colorNumKey" match="color" use="@cid" />
  <xsl:template match="shirts">
    <xsl:apply-templates select="$colorLookupDoc" />
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="colors" />
  <xsl:template match="shirt">
    <xsl:variable name="shirtColor" select="@colorCode" />
    <xsl:for-each select="$colorLookupDoc">
      <xsl:value-of select="key("colorNumKey",$shirtColor)" />
    </xsl:for-each>
    <xsl:text> </xsl:text>
    <xsl:apply-templates />
    <xsl:text></xsl:text>
  </xsl:template>
</xsl:stylesheet>