XML/XSLT stylesheet/following sibling

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

following-sibling

File: Data.xml
<Employees>
 <Person>
  <FirstName>A</FirstName>
  <LastName>B</LastName>
  <DateOfBirth>2008-12-12</DateOfBirth>
 </Person>
 <Person>
  <FirstName>C</FirstName>
  <LastName>D</LastName>
  <DateOfBirth>2008-11-11</DateOfBirth>
 </Person>
 <Person>
  <FirstName>E</FirstName>
  <LastName>F</LastName>
  <DateOfBirth>2008-10-10</DateOfBirth>
 </Person>
</Employees>

File: Transform.xslt
<xsl:stylesheet  
     version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:template match="/">
         <html>
          <head>
           <title>following-sibling axis.</title>
          </head>
          <body>
           <h3>sibling axis demo.</h3>
           <xsl:apply-templates select="/Employees/Person[1]/FirstName" />
          </body>
         </html> 
    </xsl:template>
    <xsl:template match="FirstName">
         <xsl:for-each select="following-sibling::*">
              <paragraph>
                  <xsl:value-of select="name(.)" /> 
                  which contains the text 
                  "<xsl:value-of select="." />".
              </paragraph>
         </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>following-sibling axis.</title>
   </head>
   <body>
      <h3>sibling axis demo.</h3>
      <paragraph>LastName 
         which contains the text 
         "B".
         
      </paragraph>
      <paragraph>DateOfBirth 
         which contains the text 
         "2008-12-12".
         
      </paragraph>
   </body>
</html>



following-sibling demo

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="product">
    xsl:value-of:
    <xsl:value-of select="following-sibling::*" />
    xsl:copy-of:
    <xsl:copy-of select="following-sibling::*" />
  </xsl:template>
  
  <xsl:template match="winery | year | prices | list | discounted " />
</xsl:stylesheet>
Output:

  
  
    xsl:value-of:
    1996
    xsl:copy-of:
    <year>1996</year><prices date="12/1/01">
    <list>13.99</list>
    <discounted>11.00</discounted>
  </prices>



following-sibling::node()[1][not(self::NL)]

File: Data.xml
<?xml version="1.0"?>
<PLAY>
  <TITLE>Nothing</TITLE>
  <FM>
    <P>
      A
    </P>
    <P>
      T
    </P>
  </FM>
  <PERSONAE>
    <TITLE>D</TITLE>
    <PERSONA>D</PERSONA>
    <PGROUP>
      <PERSONA>C</PERSONA>
      <PERSONA>B</PERSONA>
      <GRPDESCR>f</GRPDESCR>
    </PGROUP>
    <PERSONA>F</PERSONA>
    <PERSONA>H</PERSONA>
    <PERSONA>B</PERSONA>
    <PGROUP>
      <PERSONA>MARGARET</PERSONA>
      <PERSONA>URSULA</PERSONA>
      <GRPDESCR>gentlewomen attending on Hero.</GRPDESCR>
    </PGROUP>
    <PERSONA>Messengers, Watch, Attendants, &amp;c.</PERSONA>
  </PERSONAE>
</PLAY>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">
  <xsl:template match="SCENE">
    <xsl:variable name="flat">
      <NL />
      <xsl:apply-templates mode="phase1" />
    </xsl:variable>
    <xsl:apply-templates
      select="$flat/NL[following-sibling::node()]" mode="phase2" />
  </xsl:template>
  <xsl:template match="SPEECH" mode="phase1">
    <speaker>
      <xsl:value-of select="SPEAKER" />
    </speaker>
    <xsl:copy-of select="node() except SPEAKER" />
  </xsl:template>
  <xsl:template match="NL" mode="phase2">
    <line>
      <xsl:apply-templates
        select="following-sibling::node()[1][not(self::NL)]" mode="phase2" />
    </line>
  </xsl:template>
  <xsl:template match="node()" mode="phase2">
    <xsl:copy-of select="." />
    <xsl:apply-templates
      select="following-sibling::node()[1][not(self::NL)]" mode="phase2" />
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
  Nothing
  
    
      A
    
    
      T
    
  
  
    D
    D
    
      C
      B
      f
    
    F
    H
    B
    
      MARGARET
      URSULA
      gentlewomen attending on Hero.
    
    Messengers, Watch, Attendants, &amp;c.



select="following-sibling::*[1]/contact/name/firstName"

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="Transform.xslt" type="text/xsl"?>
<employees xmlns="http://www.domain.ru/namespace/employee">
  <title>Employee Data File</title>
  <employee eid="1" dept="programming">
    <contact addInfo="info1">
      <name>
        <firstName>Joe</firstName>
        <middleName int="B">Brian</middleName>
        <lastName>Smith</lastName>
      </name>
      <address>
        <street>1 Drive</street>
        <city>Vancouver</city>
        <state>BC</state>
        <zipcode>80210</zipcode>
      </address>
      <phone>
        <tel type="wk">111-1111111</tel>
        <tel type="hm">222-222222</tel>
        <fax>303-4667357</fax>
      </phone>
      <email>a@a.ru</email>
    </contact>
    <hireDate>2008-10-29</hireDate>
  </employee>
  <employee eid="2" dept="training">
    <contact addInfo="info2">
      <name>
        <firstName>Sam</firstName>
        <middleName int="S">Stolte</middleName>
        <lastName>Williams</lastName>
        </name>
      <address>
        <street>1 St.</street>
        <city>Austin</city>
        <state>Texas</state>
        <zipcode>22222</zipcode>
      </address>
      <phone>
        <tel type="wk">512-3467899</tel>
        <tel type="hm">512-4623356</tel>
        <fax>512-3465655</fax>
      </phone>
      <email>s@s.ru</email>
    </contact>
    <hireDate>2000-03-11</hireDate>
  </employee>
</employees>

File: Transform.xslt
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="html" indent="yes" />
  <xsl:template match="/">
    <html>
      <head>
        <title>Employee Output</title>
      </head>
      <body>
        <xsl:apply-templates select="employees/employee" />
      </body>
    </html>
  </xsl:template>
  <xsl:template match="employee">
    <xsl:variable name="choose">
      <xsl:choose>
        <xsl:when
          test="following-sibling::*/contact/name/firstName">
          <xsl:value-of
            select="following-sibling::*[1]/contact/name/firstName" />
        </xsl:when>
        <xsl:otherwise>Nobody</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <p>
      The employee element node that is the following sibling of
      <xsl:value-of select="self::*/contact/name/firstName" />
      is for the following staff member:
      <xsl:value-of select="$choose" />
    </p>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Employee Output</title>
   </head>
   <body></body>
</html>



select="following-sibling::notification[count(preceding-sibling::employeeName[1] | current()) = 1]"

File: Data.xml
<?xml version="1.0"?>
<group>
  <employeeName>Samson</employeeName>
  <notification>0001</notification>
  <notification>name 1</notification>
  <notification>0003</notification>
  <employeeName>Delihla</employeeName>
  <notification>0004</notification>
  <notification>0005</notification>
  <notification>0006</notification>
</group>
File: Transform.xslt
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="html" indent="yes" />
  <xsl:template match="/">
    <xsl:apply-templates select="group" />
  </xsl:template>
  <xsl:template match="group">
    <xsl:for-each select="employeeName">
      <tr>
        <td>
          <xsl:value-of select="." />
        </td>
        <xsl:for-each
          select="following-sibling::notification[count(preceding-sibling::employeeName[1] | current()) = 1]">
          <td>
            <xsl:value-of select="." />
          </td>
        </xsl:for-each>
      </tr>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
Output:
<tr>
   <td>Samson</td>
   <td>0001</td>
   <td>name 1</td>
   <td>0003</td>
</tr>
<tr>
   <td>Delihla</td>
   <td>0004</td>
   <td>0005</td>
   <td>0006</td>
</tr>