XML Tutorial/XPath/attribute — различия между версиями

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

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

Attributes can be accessed in similar way as elements

   <source lang="xml">

"@" is in the front of attribute names. "@name" matches name attribute of an element. "data/@color" matches color attribute of data element.

File: Data.xml <?xml version="1.0"?> <dog name="Joe">

 <data weight="18 kg" color="black"/>

</dog> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="dog">
     <paragraph>
       
         <xsl:text>Dog: </xsl:text>
       
       <xsl:value-of select="@name"/>
     </paragraph>
     <paragraph>
       
         <xsl:text>Color: </xsl:text>
       
       <xsl:value-of select="data/@color"/>
     </paragraph>
   </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><paragraph>Dog: Joe</paragraph><paragraph>Color: black</paragraph></source>


Attributes can be processed in the same way as elements

   <source lang="xml">

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

   Joe Smith

</employee> 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="employee">
     <xsl:value-of select="."/>
     <xsl:text>[</xsl:text>
     <xsl:apply-templates select="@id"/>
     <xsl:text>]</xsl:text>
   </xsl:template>
   <xsl:template match="@id">
     
       
         <xsl:value-of select="."/>
       
     
   </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>

   Joe Smith

[js0034]</source>


Check attribute existance

   <source lang="xml">

File: Data.xml <?xml version="1.0" standalone="yes"?> <poem author="jm" year="1667">

 <verse>line 1</verse>
 <verse>line 2</verse>
 <verse>line 3</verse>
 <verse>line 4</verse>

</poem> File: Transform.xslt <?xml version="1.0" standalone="yes"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="text" />
 <xsl:template match="poem">
   <xsl:if test="@author="jm"">1. The poem"s author is jm.</xsl:if>
 </xsl:template>

</xsl:stylesheet> Output: 1. The poem"s author is jm.</source>


Check value of attribute

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="Transform.xslt"?> <advertisement action="create">

  asdf

</advertisement> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="text" indent="no" />
 <xsl:variable select="advertisement/text" name="path" />
 <xsl:template match="/">
   <xsl:apply-templates select="$path" />
 </xsl:template>
 <xsl:template match="text">
   <xsl:apply-templates />
 </xsl:template>
 <xsl:template match="headline">
   <xsl:text>\F4</xsl:text>
   <xsl:value-of select="text()" />
 </xsl:template>
 <xsl:template match="para">
   <xsl:if test="self::para[@fontsize="agate"]">
     <xsl:text>&#13;\F1</xsl:text>
     <xsl:value-of select="text()" />
   </xsl:if>
   <xsl:if test="self::para[@justify="flushleft"]">
     <xsl:text><</xsl:text>
   </xsl:if>
 </xsl:template>

</xsl:stylesheet></source>


for-each select="attribute::*"

   <source lang="xml">

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

   <AAA id="a1" pos="start">
     <BBB id="b1"/>
     <BBB id="b2"/>
   </AAA>
   <AAA id="a2">
     <BBB id="b3"/>
     <BBB id="b4"/>
     <CCC id="c1">
       <CCC id="c2"/>
     </CCC>
     <BBB id="b5">
       <CCC id="c3"/>
     </BBB>
   </AAA>

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:for-each select="/source//*"> <xsl:call-template name="print"/> </xsl:for-each>
Axis: attribute
Element Node-set
   </xsl:template>
   <xsl:template name="print">
     <tr>
       <td>
         <xsl:value-of select="name()"/>
         <xsl:text> id = </xsl:text>
         <xsl:value-of select="./@id"/>
       </td>
       <td>
         <xsl:for-each select="attribute::*">
           <xsl:if test="not(@id)">
             <xsl:value-of select="name()"/>
           </xsl:if>
           <xsl:value-of select="./@id"/>
           <xsl:text/>
         </xsl:for-each>
       </td>
     </tr>
   </xsl:template>

</xsl:stylesheet> Output:

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


If the element has an attribute

   <source lang="xml">

File: Data.xml <?xml version="1.0" standalone="yes"?> <poem author="jm" year="1667">

 <verse>line 1</verse>
 <verse>line 2</verse>
 <verse>line 3</verse>
 <verse>line 4</verse>

</poem> File: Transform.xslt

<?xml version="1.0" standalone="yes"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

 version="1.0">
 <xsl:output method="text" />
 <xsl:template match="poem">
   <xsl:if test="@author">
     The poem has an author attribute.
   </xsl:if>
 </xsl:template>

</xsl:stylesheet> Output:

     The poem has an author attribute.</source>
   
  

includes or excludes elements if the specified attribute is present

   <source lang="xml">

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

    <car id="a234" checked="yes"/>
  <car id="a111" checked="yes"/>
  <car id="a005"/>

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="car[not(@checked)]">
     <paragraph>
       <xsl:text>Car: </xsl:text>
       <xsl:value-of select="@id"/>
     </paragraph>
   </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>


  <paragraph>Car: a005</paragraph></source>
   
  

Select elements, which contain or do not contain the given attribute

   <source lang="xml">

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

   <car id="a234" checked="yes"/>
 <car id="a111" checked="yes"/>
 <car id="a005"/>

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="car[@checked]">
     <paragraph>
       <xsl:text>Car: </xsl:text>
       <xsl:value-of select="@id"/>
     </paragraph>
   </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>

   <paragraph>Car: a234</paragraph>
 <paragraph>Car: a111</paragraph></source>
   
  

select="state/@joined"

   <source lang="xml">

File: Data.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="Transform.xslt" type="text/xsl"?> <member>

<state joined="1995">Austria</state>
<state joined="1950">Belgium</state>
<state joined="1973">Denmark</state>
<state joined="1995">Finland</state>
<state joined="1950">France</state>

</member>

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="member">
   <xsl:text>Number of EU Member States: </xsl:text>
   <xsl:value-of select="count(state)" />
   <xsl:text>
</xsl:text>
   <xsl:apply-templates select="state/@joined">
     <xsl:sort data-type="number" />
   </xsl:apply-templates>
   <xsl:text>
</xsl:text>
 </xsl:template>
 <xsl:template match="state/@joined">
   <xsl:text> - </xsl:text>
   <xsl:apply-templates select=".." />
   <xsl:text> (</xsl:text>
   <xsl:value-of select="." />
   <xsl:text>)
</xsl:text>
 </xsl:template>

</xsl:stylesheet> Output: Number of EU Member States: 5

- Belgium (1950)
- France (1950)
- Denmark (1973)
- Austria (1995)
- Finland (1995)</source>
Axis: attribute
Element</th><td>Node-set</th>