XML Tutorial/XSLT stylesheet/match

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

match=county[starts-with(.,K)] priority=2

File: Data.xml
<?xml version="1.0" encoding="US-ASCII"?>
<state name="Rhode Island">
 <county>Bristol</county>
 <county>Kent</county>
 <county>Newport</county>
 <county>Providence</county>
 <county>Washington</county>
</state>

File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/">
    <county state="{state/@name}">
      <xsl:apply-templates select="state" />
    </county>
  </xsl:template>
  <xsl:template match="state">
    <xsl:apply-templates select="county" />
  </xsl:template>
  <xsl:template match="county[starts-with(.,"K")]" priority="2">
    <first-match>
      <xsl:apply-templates />
    </first-match>
  </xsl:template>
  <xsl:template match="county[2]" priority="1">
    <last-match>
      <xsl:apply-templates />
    </last-match>
  </xsl:template>
  <xsl:template match="county">
    <name>
      <xsl:apply-templates />
    </name>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<county state="Rhode Island">
   <name>Bristol</name>
   <first-match>Kent</first-match>
   <name>Newport</name>
   <name>Providence</name>
   <name>Washington</name>
</county>


Match element by attribute value

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="text" />
  <xsl:template match="greet">
    <xsl:apply-templates select="greeting[lang("fr")]" />
  </xsl:template>
  <xsl:template match="greeting[lang("fr")]">
    <xsl:text>French: </xsl:text>
    <xsl:value-of select="." />
  </xsl:template>
</xsl:stylesheet>
Output:
French: Bienvenue


match sixth child

File: Data.xml
<?xml version="1.0"?>
<mammals locale="North America">
 <mammal junk="yyy">A</mammal>
 <mammal>B</mammal>
 <mammal>C</mammal>
 <mammal>D</mammal>
 <mammal>E</mammal>
 <mammal>F</mammal>
 <mammal>G</mammal>
 <mammal>H</mammal>
</mammals>

File: Transform.xslt
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:template match="mammals/mammal[6]">
    Found
    <xsl:value-of select="." />
    !
  </xsl:template>
</xsl:stylesheet>
Output:

 A
 B
 C
 D
 E
 
    Found
    F
    !
  
 G
 H


match = "* | text()"

File: Data.xml
<?xml version = "1.0"?>
<product>
   <books>
      <book>
         Getting Started with Microsoft Visual C++ 
      </book>
      <book>C How to Program</book>
   </books>
</product>
File: Transform.xslt
<?xml version = "1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="* | text()" />
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>


match with operator |

File: Data.xml
<wine grape="Cabernet">
  <winery>shop 1</winery>
  <product>product 1</product>
  <year>1996</year>
  <prices date="12/1/01">
    <list>13.99</list>
    <discounted>11.00</discounted>
  </prices>
</wine>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
  
  <xsl:template match="prices">
    <xsl:copy />
  </xsl:template>
  
  <xsl:template match="winery | product | year" />
</xsl:stylesheet>


mode = toc

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
  <chapter>
    <title>Chapter 1</title>
    <comment>This is a comment</comment>
    <paragraph>And this is some text</p>
  </chapter>
  <chapter>
    <title>Chapter 2</title>
    <paragraph>This is some text as well</p>
  </chapter>
</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="/">
    <H2>TOC</H2>
    <xsl:for-each select="//chapter">
      <xsl:apply-templates select="." mode="toc" />
    </xsl:for-each>
    <H2>TEXT</H2>
    <xsl:for-each select="//chapter">
      <xsl:apply-templates select="." />
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="chapter">
    <xsl:apply-templates select="*" />
  </xsl:template>
  <xsl:template match="chapter" mode="toc">
    <xsl:apply-templates select="*" mode="toc" />
  </xsl:template>
  <xsl:template match="title">
    <H3 style="color:blue">
      <xsl:value-of select="." />
    </H3>
  </xsl:template>
  <xsl:template match="title" mode="toc">
    <H3 style="color:red">
      <xsl:value-of select="." />
    </H3>
  </xsl:template>
  <xsl:template match="comment" mode="toc">
    [
    <i>
      <xsl:value-of select="." />
    </i>
    ]
  </xsl:template>
  <xsl:template match="comment">
    <i>
      <xsl:value-of select="." />
    </i>
  </xsl:template>
  <xsl:template match="p" mode="toc" />
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><H2>TOC</H2><H3 style="color:red">Chapter 1</H3>
    [
    <i>This is a comment</i>
    ]
  <H3 style="color:red">Chapter 2</H3><H2>TEXT</H2><H3 style="color:blue">Chapter 1</H3><i>This is a comment</i>And this is some text<H3 style="color:blue">Chapter 2</H3>This is some text as well