XML/XSLT stylesheet/value of

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

Get value from element and attribute

File: Data.xml
<?xml version="1.0"?>
<?xml-stylesheet type="application/xml" href="Transform.xslt"?>
<people>
  <person born="2008" died="2008" id="1">
    <name>
      <first_name>A</first_name>
      <last_name>B</last_name>
    </name>
    <profession>A</profession>
    <profession>B</profession>
    <profession>C</profession>
  </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>
    <profession>G</profession>
    <hobby>H</hobby>
  </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="/">
    <html>
      <xsl:apply-templates select="people"/>
    </html>
  </xsl:template>
  <xsl:template match="people">
    <table>
      <xsl:apply-templates select="person"/>
    </table>
  </xsl:template>
  <xsl:template match="person">
    <tr>
      <td><xsl:value-of select="name"/></td>
      <td><xsl:value-of select="@born"/></td>
      <td><xsl:value-of select="@died"/></td>
    </tr>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <table>
      <tr>
         <td>
            A
            B
            
         </td>
         <td>2008</td>
         <td>2008</td>
      </tr>
      <tr>
         <td>
            D
            E
            F
            
         </td>
         <td>2007</td>
         <td>2007</td>
      </tr>
   </table>
</html>



Get value of element

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<Persons>
  <Person>
    <FirstName>A</FirstName>
    <LastName>B</LastName>
  </Person>
  <Person>
    <FirstName>C</FirstName>
    <LastName>D</LastName>
  </Person>
  <Person>
    <FirstName>E</FirstName>
    <LastName>F</LastName>
  </Person>
</Persons>

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:template match="/">
    <Persons>
      <xsl:apply-templates select="/Persons/Person" />
    </Persons>
  </xsl:template>
  <xsl:template match="Person">
    <xsl:copy>
      <xsl:attribute name="FirstName">
                  <xsl:value-of select="FirstName" />
              </xsl:attribute>
      <xsl:attribute name="LastName">
                  <xsl:value-of select="LastName" />
              </xsl:attribute>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Output:
<?xml version="1.0" encoding="UTF-8"?><Persons><Person FirstName="A" LastName="B"/><Person FirstName="C" LastName="D"/><Person FirstName="E" LastName="F"/></Persons>



Get value of element with @

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<Persons>
  <Person>
    <FirstName>A</FirstName>
    <LastName>B</LastName>
  </Person>
  <Person>
    <FirstName>C</FirstName>
    <LastName>D</LastName>
  </Person>
  <Person>
    <FirstName>E</FirstName>
    <LastName>F</LastName>
  </Person>
</Persons>

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:template match="/">
    <Persons>
      <xsl:apply-templates select="/Persons/Person" />
    </Persons>
  </xsl:template>
  <xsl:template match="Person">
    <xsl:copy>
      <xsl:element name="FirstName">
        <xsl:value-of select="@FirstName" />
      </xsl:element>
      <xsl:element name="LastName">
        <xsl:value-of select="@LastName" />
      </xsl:element>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><Persons><Person><FirstName/><LastName/></Person><Person><FirstName/><LastName/></Person><Person><FirstName/><LastName/></Person></Persons>



Get value out of two tags

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<emailList>
  <person>
    <name>person1</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person2</name>
    <email>p@hotmail.ru</email>
  </person>
  <person>
    <name>person3</name>
    <email>p3@hotmail.ru</email>
  </person>
</emailList>

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="/emailList/person">
    <xsl:value-of select="name"/>,<xsl:value-of select="email"/>
  </xsl:template>
</xsl:stylesheet>
Output:

  person1,p@hotmail.ru
  person2,p@hotmail.ru
  person3,p3@hotmail.ru



select="child::name/following-sibling::*"

File: Data.xml
<?xml version="1.0"?>
<?xml-stylesheet type="application/xml" href="Transform.xslt"?>
<people>
  <person born="2008" died="2008" id="1">
    <name>
      <first_name>A</first_name>
      <last_name>B</last_name>
    </name>
    <profession>A</profession>
    <profession>B</profession>
    <profession>C</profession>
  </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>
    <profession>G</profession>
    <hobby>H</hobby>
  </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="/">
    <dl>
      <xsl:apply-templates select="descendant::person"/>
    </dl>
  </xsl:template>
  
  <xsl:template match="person">
    <dt><xsl:value-of select="child::name"/></dt>
    <dd>
      <ul>
        <xsl:apply-templates select="child::name/following-sibling::*"/>
      </ul>
    </dd>
  </xsl:template>
  <xsl:template match="*">
    <li><xsl:value-of select="self::*"/></li>
  </xsl:template>
  <xsl:template match="homepage" 
               xmlns:xlink="http://www.w3.org/1999/xlink">
    <li><xsl:value-of select="attribute::xlink:href"/></li>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><dl><dt>
      A
      B
    </dt><dd><ul><li>A</li><li>B</li><li>C</li></ul></dd><dt>
      D
      E
      F
    </dt><dd><ul><li>G</li><li>H</li></ul></dd></dl>



Use value-of to get the size of text

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<Book>
 <Chapter number="c1">the first chapter</Chapter>
 <Chapter number="c2">the second chapter</Chapter>
 <Chapter number="c3">the third chapter</Chapter>
 <Chapter number="c4">the fourth chapter</Chapter>
 <Chapter number="c5">the fifth chapter</Chapter>
</Book>
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="/">
        <xsl:apply-templates select="/Book/Chapter" />
  </xsl:template>
  <xsl:template match="Chapter">
    <xsl:if test="position()=2">
      <paragraph>
        The text the Chapter element node contains is "<xsl:value-of select="." />".
      </paragraph>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><paragraph>
        The text the Chapter element node contains is "the second chapter".
      </paragraph>



value-of select="$sortedCities" separator=" "

File: Data.xml
<?xml version="1.0"?>
<addressbook>
  <address>
    <name>
      <title>Mr.</title>
      <first-name>Chester Hasbrouck</first-name>
      <last-name>Frisby</last-name>
    </name>
    <street>1234 Main Street</street>
    <city>Sheboygan</city>
    <state>WI</state>
    <zip>48392</zip>
  </address>
</addressbook>

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:variable name="sortedCities" as="xs:string*">
      <xsl:perform-sort select="addressbook/address/city">
        <xsl:sort select="."/>
      </xsl:perform-sort>
    </xsl:variable>
    <xsl:text>Our customers live in these cities:&#xA;&#xA;</xsl:text>
    <xsl:value-of select="$sortedCities" separator="&#xA;"/>
  </xsl:template>
</xsl:stylesheet>
Output:
Our customers live in these cities:
Sheboygan