XML/XSLT stylesheet/match

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

Match among a list of target values

File: Data.xml 
<?xml version="1.0"?>
<books>
  <book category="reference">
      <author>author1</author>
      <title>title 1</title>
      <price>8.95</price>
   </book>
   <book category="fiction">
      <author>author 2</author>
      <title>title 2</title>
      <price>12.99</price>
   </book>
   <book category="fiction">
      <author>author 3</author>
      <title>title 3</title>
      <price>8.99</price>
   </book>
</books>
File: Transform.xslt
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  <xsl:template match="books">
    <html>
      <body>
        <h1>A list of books</h1>
        <table width="640">
          <xsl:apply-templates />
        </table>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="book">
    <tr>
      <td>
        <xsl:number />
      </td>
      <xsl:apply-templates />
    </tr>
  </xsl:template>
  <xsl:template match="author | title | price">
    <td>
      <xsl:value-of select="." />
    </td>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <body>
      <h1>A list of books</h1>
      <table width="640">
         
         <tr>
            <td>1</td>
            
            <td>author1</td>
            
            <td>title 1</td>
            
            <td>8.95</td>
            
         </tr>
         
         <tr>
            <td>2</td>
            
            <td>author 2</td>
            
            <td>title 2</td>
            
            <td>12.99</td>
            
         </tr>
         
         <tr>
            <td>3</td>
            
            <td>author 3</td>
            
            <td>title 3</td>
            
            <td>8.99</td>
            
         </tr>
         
      </table>
   </body>
</html>



match: mode="cast-list"

File: Data.xml
<?xml version="1.0"?>
<SCENE>
  <TITLE>title 1</TITLE>
  <STAGEDIR>A</STAGEDIR>
  <SPEECH>
    <SPEAKER>B</SPEAKER>
    <LINE>line 1</LINE>
    <LINE>line 2</LINE>
    <LINE>line 3</LINE>
    <LINE>line 4</LINE>
    <LINE>line 5</LINE>
    <LINE>line 6</LINE>
  </SPEECH>
</SCENE>
File: Transform.xsl
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  <xsl:template match="SCENE">
    <html>
      <body>
        <xsl:apply-templates select="TITLE" />
        <xsl:variable name="speakers" as="element()*">
          <xsl:for-each-group select="//SPEAKER"
            group-by=".">
            <xsl:sequence select="current-group()[1]" />
          </xsl:for-each-group>
        </xsl:variable>
        <h2>
          Cast:
          <xsl:apply-templates select="$speakers"
            mode="cast-list" />
        </h2>
        <xsl:apply-templates select="* except TITLE" />
      </body>
    </html>
  </xsl:template>
  <xsl:template match="SPEAKER" mode="cast-list">
    <xsl:value-of select="." />
    <xsl:if test="not(position()=last())">,</xsl:if>
  </xsl:template>
  <xsl:template match="TITLE">
    <h1>
      <xsl:apply-templates />
    </h1>
  </xsl:template>
  <xsl:template match="STAGEDIR">
    <i>
      <xsl:apply-templates />
    </i>
  </xsl:template>
  <xsl:template match="SPEECH">
    <p>
      <xsl:apply-templates />
    </p>
  </xsl:template>
  <xsl:template match="SPEAKER">
    <b>
      <xsl:apply-templates />
    </b>
    <br />
  </xsl:template>
  <xsl:template match="LINE">
    <xsl:apply-templates />
    <br />
  </xsl:template>
</xsl:transform>
Output:
<html>
   <body>
      <h1>title 1</h1>
      <h2>
                   Cast:
                   B
      </h2><i>A</i><p>
             <b>B</b><br>
             line 1<br>
             line 2<br>
             line 3<br>
             line 4<br>
             line 5<br>
             line 6<br>
           
      </p>
   </body>
</html>



match more than one value

<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="/">
    <html>
      <body>
        <h1>Stylesheet Module Structure</h1>
        <ul>
          <xsl:apply-templates
            select="*/xsl:include | */xsl:import" />
        </ul>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="xsl:include | xsl:import">
    <li>
      <xsl:value-of select="concat(local-name(),"s ",@href)" />
      <xsl:variable name="module" select="document(@href)" />
      <ul>
        <xsl:apply-templates
          select="$module/*/xsl:include | $module/*/xsl:import" />
      </ul>
    </li>
  </xsl:template>
</xsl:transform>
Output:
<html>
   <body>
      <h1>Stylesheet Module Structure</h1>
      <ul></ul>
   </body>
</html>



Match root

File: Data.xml
<?xml version="1.0"?>
<PersonData>
    <Name DOB="2008/11/11">
         <FirstName>first name</FirstName>
         <LastName>last name</LastName>
    </Name>
</PersonData>

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="/">
    <html>
      <head>
        <title>
          Title:
          <xsl:value-of select="/PersonData/Name/FirstName" />
          <xsl:text></xsl:text>
          <xsl:value-of select="/PersonData/Name/LastName" />
        </title>
      </head>
      <body>
        <paragraph>
          <xsl:value-of select="/PersonData/Name/FirstName" />
          <xsl:text> </xsl:text>
          <xsl:value-of select="/PersonData/Name/LastName" />
          was born on
          <xsl:value-of select="/PersonData/Name/@DOB" />
        </paragraph>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>
                   Title:
                   first namelast name
      </title>
   </head>
   <body>
      <paragraph>first name last name
                   was born on
                   2008/11/11
      </paragraph>
   </body>
</html>



| (or) with level

File: Data.xml
<chapter>
  <title>The Chapter</title>
  <sect1>
    <title>First Section</title>
    <figure>
      <title>First picture in book</title>
      <graphic fileref="pic1.jpg" />
    </figure>
  </sect1>
  <sect1>
    <title>Second Section</title>
    <sect2>
      <title>Second Section, First Subsection</title>
      <figure>
        <title>Second picture in book</title>
        <graphic fileref="pic2.jpg" />
      </figure>
    </sect2>
    <sect2>
      <title>Second Section, Second Subsection</title>
      <para>This one has no figure.</para>
    </sect2>
    <sect2>
      <title>Second Section, Third Subsection</title>
      <figure>
        <title>Fourth picture in book</title>
        <graphic fileref="pic3.jpg" />
      </figure>
    </sect2>
  </sect1>
</chapter>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text" />
  <xsl:strip-space elements="*" />
  <xsl:template match="figure">
    [
    <xsl:apply-templates />
    ]
  </xsl:template>
  <xsl:template
    match="para | chapter/title | sect1/title | sect2/title " />
</xsl:stylesheet>
Output:

    [
    First picture in book
    ]
  
    [
    Second picture in book
    ]
  
    [
    Fourth picture in book
    ]