XML Tutorial/XSLT stylesheet/generate id

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

Assign value from generate-id(.) to attribute

File: Data.xml
<?xml version="1.0"?>
<greet>
 <greeting xml:lang="en">Welcome</greeting>
 <greeting xml:lang="fr">Bienvenue</greeting>
 <greeting xml:lang="es">Bienvenido</greeting>
 <greeting xml:lang="de">Willkommen</greeting>
</greet>
File: Transform.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:output doctype-system="greeting.dtd" />
  <xsl:template match="greet">
    <xsl:element name="greeting">
      <xsl:apply-templates select="greeting" />
    </xsl:element>
  </xsl:template>
  <xsl:template match="greeting">
    <xsl:element name="welcome">
      <xsl:attribute name="xml:lang"><xsl:value-of
          select="@xml:lang" />
      </xsl:attribute>
      <xsl:attribute name="id"><xsl:value-of
          select="generate-id(.)" />
      </xsl:attribute>
      <xsl:value-of select="current()" />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE greeting
  SYSTEM "greeting.dtd">
<greeting>
   <welcome xml:lang="en" id="d2e3">Welcome</welcome>
   <welcome xml:lang="fr" id="d2e6">Bienvenue</welcome>
   <welcome xml:lang="es" id="d2e9">Bienvenido</welcome>
   <welcome xml:lang="de" id="d2e12">Willkommen</welcome>
</greeting>


for-each select=/addressbook/address[generate-id(.)=generate-id(key(zipcodes, zip)[1])]

File: Data.xml
<?xml version="1.0"?>
<addressbook>
  <address>
    <name>
      <title>Mr.</title>
      <first-name>Doris</first-name>
      <last-name>Smith</last-name>
    </name>
    <street>1234 Main Street</street>
    <city>Sheboygan</city>
    <state>WI</state>
    <zip>48392</zip>
  </address>
</addressbook>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:key name="zipcodes" match="address" use="zip"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>Customers grouped by Zip code</title>
      </head>
      <body>
        <table border="1" cellpadding="5">
          <xsl:for-each select="/addressbook/address[generate-id(.)=generate-id(key("zipcodes", zip)[1])]">
            <xsl:sort select="zip"/> 
            <xsl:for-each select="key("zipcodes", zip)">
              <xsl:sort select="name/last-name"/>
              <xsl:sort select="name/first-name"/>
              <tr>
                <xsl:if test="position() = 1">
                  <td rowspan="{count(key("zipcodes", zip))}">
                    <xsl:text>Zip code </xsl:text>
                    <br/>
                    <span style="font-size: 150%;">
                      <xsl:value-of select="zip"/>
                    </span>
                    <br/>
                    <xsl:value-of select="city"/>
                    <xsl:text>, </xsl:text>
                    <xsl:value-of select="state"/>
                  </td>
                </xsl:if>
                <td>
                  <xsl:value-of select="name/first-name"/>
                  <xsl:text> </xsl:text>
                  <span>
                    <xsl:value-of select="name/last-name"/>
                  </span>
                </td>
                <td>
                  <xsl:value-of select="street"/>
                </td>
              </tr>
            </xsl:for-each>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Customers grouped by Zip code</title>
   </head>
   <body>
      <table border="1" cellpadding="5">
         <tr>
            <td rowspan="1">Zip code <br><span style="font-size: 150%;">48392</span><br>Sheboygan, WI
            </td>
            <td>Doris <span>Smith</span></td>
            <td>1234 Main Street</td>
         </tr>
      </table>
   </body>
</html>


generate-id() generates id conforming to XML spec

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <AAA name="top">
      <BBB pos="1" val="bbb">11111</BBB>
      <BBB>22222</BBB>
    </AAA>
    <AAA name="bottom">
      <BBB>33333</BBB>
      <BBB>44444</BBB>
    </AAA>
</data>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <DIV>
        <B>
          <xsl:text>generate-id(//AAA)  : </xsl:text>
        </B>
        <xsl:value-of select="generate-id(//AAA)  "/>
      </DIV>
      <DIV>
        <B>
          <xsl:text>generate-id(//BBB)  : </xsl:text>
        </B>
        <xsl:value-of select="generate-id(//BBB)  "/>
      </DIV>
      <DIV>
        <B>
          <xsl:text>generate-id(//AAA[1])  : </xsl:text>
        </B>
        <xsl:value-of select="generate-id(//AAA[1])  "/>
      </DIV>
      <DIV>
        <B>
          <xsl:text>generate-id(//*[1])  : </xsl:text>
        </B>
        <xsl:value-of select="generate-id(//*[1])  "/>
      </DIV>
      <DIV>
        <B>
          <xsl:text>generate-id(//xslTutorial/*[1])  : </xsl:text>
        </B>
        <xsl:value-of select="generate-id(//xslTutorial/*[1])  "/>
      </DIV>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><DIV><B>generate-id(//AAA)  : </B>d2e3</DIV><DIV><B>generate-id(//BBB)  : </B>d2e5</DIV><DIV><B>generate-id(//AAA[1])  : </B>d2e3</DIV><DIV><B>generate-id(//*[1])  : </B>d2e1</DIV><DIV><B>generate-id(//xslTutorial/*[1])  : </B></DIV>


uses generate-id() to add id to all elements in source XML.

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <AAA name="top">
      <BBB pos="1" val="bbb">11111</BBB>
      <BBB>22222</BBB>
    </AAA>
    <AAA name="bottom">
      <BBB>33333</BBB>
      <BBB>44444</BBB>
    </AAA>
</data>
File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="*">
      <xsl:copy>
        <xsl:attribute name="id"><xsl:value-of select="generate-id()"/></xsl:attribute>
        <xsl:for-each select="@*">
          <xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
        </xsl:for-each>
        <xsl:apply-templates/>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>  
Output:
<?xml version="1.0" encoding="UTF-8"?><data id="d2e1">
    <AAA id="d2e3" name="top">
      <BBB id="d2e5" pos="1" val="bbb">11111</BBB>
      <BBB id="d2e8">22222</BBB>
    </AAA>
    <AAA id="d2e12" name="bottom">
      <BBB id="d2e14">33333</BBB>
      <BBB id="d2e17">44444</BBB>
    </AAA>
</data>