XML/XSLT stylesheet/value of

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

Get value from element and attribute

   <source lang="xml">

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">
<xsl:apply-templates select="person"/>
 </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>

           A
           B
           
2008 2008
           D
           E
           F
           
2007 2007

</html>

</source>
   
  


Get value of element

   <source lang="xml">

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>

</source>
   
  


Get value of element with @

   <source lang="xml">

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>

</source>
   
  


Get value out of two tags

   <source lang="xml">

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


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

   <source lang="xml">

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="/">
<xsl:apply-templates select="descendant::person"/>
 </xsl:template>
 
 <xsl:template match="person">
   
<xsl:value-of select="child::name"/>
    <xsl:apply-templates select="child::name/following-sibling::*"/>
   
 </xsl:template>
 <xsl:template match="*">
  • <xsl:value-of select="self::*"/>
  •  </xsl:template>
     <xsl:template match="homepage" 
                  xmlns:xlink="http://www.w3.org/1999/xlink">
    
  • <xsl:value-of select="attribute::xlink:href"/>
  •  </xsl:template>
    

    </xsl:stylesheet> Output:

    <?xml version="1.0" encoding="UTF-8"?>
    A B
    • A
    • B
    • C
         D
         E
         F
    
    • G
    • H
    </source>
       
      
    


    Use value-of to get the size of text

       <source lang="xml">
    

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


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

       <source lang="xml">
    

    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:
    
    </xsl:text>
       <xsl:value-of select="$sortedCities" separator="
    "/>
     </xsl:template>
    

    </xsl:stylesheet> Output: Our customers live in these cities: Sheboygan

    </source>