XML Tutorial/XSLT stylesheet/match

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

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

   <source lang="xml">

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></source>


Match element by attribute value

   <source lang="xml">

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</source>


match sixth child

   <source lang="xml">

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</source>
   
  

match = "* | text()"

   <source lang="xml">

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"?></source>


match with operator |

   <source lang="xml">

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></source>


mode = toc

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="utf-8"?>

 <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>

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="/">

TOC

   <xsl:for-each select="//chapter">
     <xsl:apply-templates select="." mode="toc" />
   </xsl:for-each>

TEXT

   <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">

<xsl:value-of select="." />

 </xsl:template>
 <xsl:template match="title" mode="toc">

<xsl:value-of select="." />

 </xsl:template>
 <xsl:template match="comment" mode="toc">
   [
   
     <xsl:value-of select="." />
   
   ]
 </xsl:template>
 <xsl:template match="comment">
   
     <xsl:value-of select="." />
   
 </xsl:template>
 <xsl:template match="p" mode="toc" />

</xsl:stylesheet> Output:

<?xml version="1.0" encoding="UTF-8"?>

TOC

Chapter 1

   [
   This is a comment
   ]

Chapter 2

TEXT

Chapter 1

This is a commentAnd this is some text

Chapter 2

This is some text as well</source>