XML Tutorial/XPath/attribute

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

Attributes can be accessed in similar way as elements

"@" 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>
        <b>
          <xsl:text>Dog: </xsl:text>
        </b>
        <xsl:value-of select="@name"/>
      </paragraph>
      <paragraph>
        <b>
          <xsl:text>Color: </xsl:text>
        </b>
        <xsl:value-of select="data/@color"/>
      </paragraph>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><paragraph><b>Dog: </b>Joe</paragraph><paragraph><b>Color: </b>black</paragraph>


Attributes can be processed in the same way as elements

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">
      <b>
        <i>
          <xsl:value-of select="."/>
        </i>
      </b>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
    Joe Smith
[<b><i>js0034</i></b>]


Check attribute existance

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.


Check value of attribute

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>&lt;</xsl:text>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>


for-each select="attribute::*"

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
    <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>
</data>
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="/">
      <table border="1" cellpadding="6">
        <tr>
          <td colspan="2">Axis: attribute</td>
        </tr>
        <tr>
          <td>Element</td>
          <td>Node-set</td>
        </tr>
        <xsl:for-each select="/source//*">
          <xsl:call-template name="print"/>
        </xsl:for-each>
      </table>
    </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 border="1" cellpadding="6"><tr><th colspan="2">Axis: attribute</th></tr><tr><td>Element</th><td>Node-set</th></tr></table>


If the element has an attribute

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.


includes or excludes elements if the specified attribute is present

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>    
     <car id="a234" checked="yes"/>
   <car id="a111" checked="yes"/>
   <car id="a005"/>
</data>
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>


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

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>    
    <car id="a234" checked="yes"/>
  <car id="a111" checked="yes"/>
  <car id="a005"/>
</data>

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>


select="state/@joined"

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>&#10;</xsl:text>
    <xsl:apply-templates select="state/@joined">
      <xsl:sort data-type="number" />
    </xsl:apply-templates>
    <xsl:text>&#10;</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>)&#10;</xsl:text>
  </xsl:template>
</xsl:stylesheet>
Output:
Number of EU Member States: 5
 - Belgium (1950)
 - France (1950)
 - Denmark (1973)
 - Austria (1995)
 - Finland (1995)