XML Tutorial/XSLT stylesheet/select — различия между версиями

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

Текущая версия на 08:26, 26 мая 2010

Get value from an element with value-of and select

File: Data.xml 
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="Transform.xslt"?>
<greeting>Hello, world!</greeting>
File: Transform.xslt
<?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>Today"s greeting</title>
      </head>
      <body>
        <paragraph>
          <xsl:value-of select="greeting" />
        </p>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Today"s greeting</title>
   </head>
   <body>
      <paragraph>Hello, world!</p>
   </body>
</html>


Match a certain element

File: Data.xml
<poem year="1667" type="epic">
  <verse>line 3</verse>
  <verse>line 4</verse>
</poem>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="poem">
    <ode>
      <author>Jack</author>
      <year>
        <xsl:value-of select="@year" />
      </year>
      <xsl:apply-templates />
    </ode>
  </xsl:template>
  <xsl:template match="verse">
    <verse>
      <xsl:apply-templates />
    </verse>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><ode><author>Jack</author><year>1667</year>
  <verse>line 3</verse>
  <verse>line 4</verse>
</ode>


match parent and select children

File: Data.xml
<wine grape="Las">
  <winery>Moti</winery>
  <year>1998</year>
  <prices>
    <list>13.99</list>
    <discounted>11.99</discounted>
    <case>143.50</case>
  </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="wine">
    <xsl:value-of select="price" />
  </xsl:template>
  
</xsl:stylesheet>


Match parent and select grand-children

File: Data.xml

<wine grape="Las">
  <winery>Moti</winery>
  <year>1998</year>
  <prices>
    <list>13.99</list>
    <discounted>11.99</discounted>
    <case>143.50</case>
  </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="wine">
    <xsl:value-of select="prices/retail" />
  </xsl:template>
  
</xsl:stylesheet>


select attribute

File: Data.xml
<poem year="1667" type="epic">
  <verse>line 3</verse>
  <verse>line 4</verse>
</poem>
File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="poem">
    <xsl:element name="ode">
      <xsl:element name="author">Jack</xsl:element>
      <xsl:element name="year">
        <xsl:value-of select="@year" />
      </xsl:element>
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>
  <xsl:template match="verse">
    <verse>
      <xsl:apply-templates />
    </verse>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><ode><author>Jack</author><year>1667</year>
  <verse>line 3</verse>
  <verse>line 4</verse>
</ode>


select=count(document(other.xml)/eu/other/state)

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<eu>
 <member>
  <state>Austria</state>
  <state founding="yes">Belgium</state>
 </member>
 <candidate>
  <state>Poland</state>
  <state>Romania</state>
  <state>Slovakia</state>
  <state>Slovenia</state>
  <state>Turkey</state>
 </candidate>
</eu>

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:template match="eu">
    <xsl:copy>
      <xsl:comment>
        <xsl:text>Member states: </xsl:text>
        <xsl:value-of select="count(member/state)" />
      </xsl:comment>
      <xsl:copy-of select="member" />
      <xsl:comment>
        <xsl:text>Candidate states: </xsl:text>
        <xsl:value-of select="count(candidate/state)" />
      </xsl:comment>
      <xsl:copy-of select="candidate" />
      <xsl:comment>
        <xsl:text>Other states: </xsl:text>
        <xsl:value-of
          select="count(document("other.xml")/eu/other/state)" />
      </xsl:comment>
      <xsl:copy-of select="document("other.xml")/eu/other" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<eu><!--Member states: 2--><member>
      <state>Austria</state>
      <state founding="yes">Belgium</state>
   </member>
   <!--Candidate states: 5--><candidate>
      <state>Poland</state>
      <state>Romania</state>
      <state>Slovakia</state>
      <state>Slovenia</state>
      <state>Turkey</state>
   </candidate>
   <!--Other states: 0--></eu>


select element and attribute

File: Data.xml
<employee hireDate="09/01/1998">
  <last>Smith</last>
  <first>Joe</first>
  <salary>95000</salary>
</employee>

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" />
  <xsl:template match="employee">
    <xsl:apply-templates select="@hireDate" /><!-- note space -->
    <xsl:apply-templates select="first" />
    <xsl:apply-templates select="last" />
  </xsl:template>
  
</xsl:stylesheet>
Output:
09/01/1998JoeSmith


select element by attribute value

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<greeting>
 <welcome xml:lang="en" id="001">Welcome</welcome>
 <welcome xml:lang="fr" id="002">Bienvenue</welcome>
 <welcome xml:lang="es" id="003">Bienvenido</welcome>
 <welcome xml:lang="de" id="004">Willkommen</welcome>
</greeting>

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="greeting">
    <xsl:apply-templates select="welcome[lang("de")]" />
  </xsl:template>
  <xsl:template match="welcome[lang("de")]">
    <xsl:text>German: </xsl:text>
    <xsl:value-of select="." />
    <xsl:text> (ID: </xsl:text>
    <xsl:value-of select="@id" />
    <xsl:text>)</xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:
German: Willkommen (ID: 004)


select element by index

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<eu>
 <member>
  <state>Austria</state>
  <state founding="yes">Belgium</state>
 </member>
 <candidate>
  <state>Bulgaria</state>
  <state>Cyprus</state>
  <state>Czech Republic</state>
 </candidate>
</eu>

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" />
  <xsl:template match="eu">
    <xsl:apply-templates select="member" />
  </xsl:template>
  <xsl:template match="member">
    <xsl:apply-templates select="state[2]" />
  </xsl:template>
  <xsl:template match="state">
    <xsl:copy />
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><state/>


Select element out

File: Data.xml
<?xml version="1.0"?>
<?xml-stylesheet type="application/xml" href="people.xsl"?>
<people>
  <person born="2008" died="2008" id="1">
    <name>
      <first_name>A</first_name>
      <last_name>B</last_name>
    </name>
  </person>
  <person born="2007" died="2007" id="2">
    <name>
      <first_name>D</first_name>
      <middle_initial>E</middle_initial>
      <last_name>F</last_name>
    </name>
  </person>
</people>
File: Transform.xslt
<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" 
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="people">
    <xsl:apply-templates select="person"/>
  </xsl:template>
  <xsl:template match="person">
    <xsl:value-of select="name"/>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
      A
      B
    
      D
      E
      F


Selecting elements based on values of other ones.

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<DOCUMENT>
  <TAG1>Hello</TAG1>
  <TAG2>Hello</TAG2>
  <TAG3>World</TAG3>
  <TAG4>Europe<TAG5>World</TAG5>
  </TAG4>
</DOCUMENT>
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="html"/>
    <xsl:template match="/">
      <xsl:if test="//DOCUMENT[TAG1 = TAG2]">
        <xsl:value-of select="//DOCUMENT/TAG1"/>
      </xsl:if>
      <BR/>
      <xsl:if test="//DOCUMENT[TAG3 = TAG4/TAG5]">
        <xsl:value-of select="//TAG4/text()"/>
      </xsl:if>
    </xsl:template>
</xsl:stylesheet>
Output:
Hello<BR>Europe


select="@*|node()"

File: Data.xml
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<state member="true" xmlns="http://www.wbex.ru">Belgium</state>

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" encoding="US-ASCII" />
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="US-ASCII"?>
<?xml-stylesheet?><state xmlns="http://www.wbex.ru" member="true">Belgium</state>


select="operand[(. < 50) and (. > 30)]"

File: Data.xml
<math>
     <operand>12</operand>
     <operand>23</operand>
     <operand>45</operand>
     <operand>56</operand>
     <operand>75</operand>
</math>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />
  <xsl:template match="math">
    <xsl:apply-templates
      select="operand[(. &lt; 50) and (. &gt; 30)]" />
  </xsl:template>
</xsl:stylesheet>
Output:
45


select="state[not(@founding)]"

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<eu>
 <member>
  <state>Austria</state>
  <state founding="yes">Belgium</state>
 </member>
 <candidate>
  <state>Bulgaria</state>
  <state>Cyprus</state>
  <state>Czech Republic</state>
 </candidate>
</eu>

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:attribute-set name="new">
    <xsl:attribute name="founding">no</xsl:attribute>
  </xsl:attribute-set>
  <xsl:template match="eu">
    <xsl:apply-templates select="member" />
  </xsl:template>
  <xsl:template match="member">
    <eu>
      <members>
        <xsl:apply-templates select="state[not(@founding)]" />
      </members>
    </eu>
  </xsl:template>
  <xsl:template match="state">
    <xsl:copy use-attribute-sets="new">
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<eu>
   <members>
      <state founding="no">Austria</state>
   </members>
</eu>


select value from attribute

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<eu>
 <member>
  <state>Austria</state>
  <state founding="yes">Belgium</state>
 </member>
 <candidate>
  <state>Bulgaria</state>
  <state>Cyprus</state>
  <state>Turkey</state>
 </candidate>
</eu>

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:template match="eu">
    <xsl:apply-templates select="member" />
  </xsl:template>
  <xsl:template match="member">
    <eu-members>
      <xsl:apply-templates select="state[@founding]" />
    </eu-members>
  </xsl:template>
  <xsl:template match="state">
    <xsl:copy>
      <xsl:apply-templates />
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<eu-members>
   <state>Belgium</state>
</eu-members>