XML Tutorial/XPath/Abbreivation

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

Axis child:: can be be omitted from a location step as it is the default axis.

Axis attribute:: can be abbreviatet to @ 
. is short for self:: 
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="AAA">
      <H3>
        <xsl:value-of select="name()"/>
        <xsl:text/>
        <xsl:value-of select="@id"/>
      </H3>
      <TABLE border="1">
        <TR>
          <td>full</TH>
          <td>abbreviated</TH>
        </TR>
        <TR>
          <TD>
            <xsl:text>child::BBB/attribute::id</xsl:text>
          </TD>
          <TD>
            <xsl:text>BBB/@id</xsl:text>
          </TD>
        </TR>
        <TR>
          <TD>
            <xsl:value-of select="child::BBB/attribute::id"/>
          </TD>
          <TD>
            <xsl:value-of select="BBB/@id"/>
          </TD>
        </TR>
      </TABLE>
    </xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
    <H3>AAAa1</H3><TABLE border="1"><TR><td>full</TH><td>abbreviated</TH></TR><TR><TD>child::BBB/attribute::id</TD><TD>BBB/@id</TD></TR><TR><TD>b1</TD><TD>b1</TD></TR></TABLE>
    <H3>AAAa2</H3><TABLE border="1"><TR><td>full</TH><td>abbreviated</TH></TR><TR><TD>child::BBB/attribute::id</TD><TD>BBB/@id</TD></TR><TR><TD>b3</TD><TD>b3</TD></TR></TABLE>


Count elements and attributes

File: Data.xml
<?xml version="1.0"?>
<list xml:lang="en">
  <title>title 1</title>
  <listitem>item 1</listitem>
  <listitem>item 2</listitem>
  <listitem>item 3</listitem>
  <listitem xml:lang="sw">item 4</listitem>
  <listitem xml:lang="en-gb">item 5</listitem>
  <listitem xml:lang="zu">item 6</listitem>
  <listitem xml:lang="jz">item 7</listitem>
</list>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:text>Your document contains </xsl:text>
    <xsl:value-of select="count(//*)"/>
    <xsl:text> elements and </xsl:text>
    <xsl:value-of select="count(//@*)"/>
    <xsl:text> attributes.&#xA;Have a great day!</xsl:text>
  </xsl:template>
  
</xsl:stylesheet>
Output:
Your document contains 9 elements and 5 attributes.
Have a great day!


"//" has two meanings

When using "//" at the beginning of a location path, "//" selects all nodes in the document of the specified type. 
When using "//" in the middle of a location path, "//" selects all nodes which appear in the node selected with the first part of the location path.
File: Data.xml
<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">
        <DDD id="d1"/>
      </CCC>
      <BBB id="b5">
        <CCC id="c2"/>
      </BBB>
    </AAA>
</data>
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="//BBB"/>
      <xsl:apply-templates select="//CCC"/>
      <xsl:apply-templates select="//DDD"/>
      <xsl:apply-templates select="//AAA"/>
    </xsl:template>
    <xsl:template match="AAA">
      <div style="color:navy">
        <xsl:value-of select="name()"/>
        <xsl:text> id=</xsl:text>
        <xsl:value-of select="@id"/>
      </div>
    </xsl:template>
    <xsl:template match="BBB">
      <div style="color:purple">
        <xsl:value-of select="name()"/>
        <xsl:text> id=</xsl:text>
        <xsl:value-of select="@id"/>
      </div>
    </xsl:template>
    <xsl:template match="CCC">
      <div style="color:red">
        <xsl:value-of select="name()"/>
        <xsl:text> id=</xsl:text>
        <xsl:value-of select="@id"/>
      </div>
    </xsl:template>
    <xsl:template match="DDD">
      <div style="color:blue">
        <xsl:value-of select="name()"/>
        <xsl:text> id=</xsl:text>
        <xsl:value-of select="@id"/>
      </div>
    </xsl:template>

</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?><div style="color:purple">BBB id=b1</div><div style="color:purple">BBB id=b2</div><div style="color:purple">BBB id=b3</div><div style="color:purple">BBB id=b4</div><div style="color:purple">BBB id=b5</div><div style="color:red">CCC id=c1</div><div style="color:red">CCC id=c2</div><div style="color:blue">DDD id=d1</div><div style="color:navy">AAA id=a1</div><div style="color:navy">AAA id=a2</div>


// is short for /descendant-or-self::

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="CCC">
      <H3>
        <xsl:value-of select="name()"/>
        <xsl:text/>
        <xsl:value-of select="@id"/>
      </H3>
      <TABLE border="1">
        <TR>
          <td>full</TH>
          <td>abbreviated</TH>
        </TR>
        <TR>
          <TD>
            <xsl:text>name(/descendant-or-self::*)</xsl:text>
          </TD>
          <TD>
            <xsl:text>name(//*)</xsl:text>
          </TD>
        </TR>
        <TR>
          <TD>
            <xsl:value-of select="name(/descendant-or-self::*)"/>
          </TD>
          <TD>
            <xsl:value-of select="name(//*)"/>
          </TD>
        </TR>
      </TABLE>
      <xsl:apply-templates/>
    </xsl:template>
</xsl:stylesheet>  
Output:
<?xml version="1.0" encoding="UTF-8"?>
    
      
      
    
    
      
      
      <H3>CCCc1</H3><TABLE border="1"><TR><td>full</TH><td>abbreviated</TH></TR><TR><TD>name(/descendant-or-self::*)</TD><TD>name(//*)</TD></TR><TR><TD>data</TD><TD>data</TD></TR></TABLE>
        <H3>CCCc2</H3><TABLE border="1"><TR><td>full</TH><td>abbreviated</TH></TR><TR><TD>name(/descendant-or-self::*)</TD><TD>name(//*)</TD></TR><TR><TD>data</TD><TD>data</TD></TR></TABLE>
      
      
        <H3>CCCc3</H3><TABLE border="1"><TR><td>full</TH><td>abbreviated</TH></TR><TR><TD>name(/descendant-or-self::*)</TD><TD>name(//*)</TD></TR><TR><TD>data</TD><TD>data</TD></TR></TABLE>


.. is short for parent::

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="BBB">
      <H3>
        <xsl:value-of select="name()"/>
        <xsl:text/>
        <xsl:value-of select="@id"/>
      </H3>
      <TABLE border="1">
        <TR>
          <td>full</TH>
          <td>abbreviated</TH>
        </TR>
        <TR>
          <TD>
            <xsl:text>parent::*/attribute::id</xsl:text>
          </TD>
          <TD>
            <xsl:text>../@id</xsl:text>
          </TD>
        </TR>
        <TR>
          <TD>
            <xsl:value-of select="parent::*/attribute::id"/>
          </TD>
          <TD>
            <xsl:value-of select="../@id"/>
          </TD>
        </TR>
      </TABLE>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
    
      <H3>BBBb1</H3><TABLE border="1"><TR><td>full</TH><td>abbreviated</TH></TR><TR><TD>parent::*/attribute::id</TD><TD>../@id</TD></TR><TR><TD>a1</TD><TD>a1</TD></TR></TABLE>
      <H3>BBBb2</H3><TABLE border="1"><TR><td>full</TH><td>abbreviated</TH></TR><TR><TD>parent::*/attribute::id</TD><TD>../@id</TD></TR><TR><TD>a1</TD><TD>a1</TD></TR></TABLE>
    
    
      <H3>BBBb3</H3><TABLE border="1"><TR><td>full</TH><td>abbreviated</TH></TR><TR><TD>parent::*/attribute::id</TD><TD>../@id</TD></TR><TR><TD>a2</TD><TD>a2</TD></TR></TABLE>
      <H3>BBBb4</H3><TABLE border="1"><TR><td>full</TH><td>abbreviated</TH></TR><TR><TD>parent::*/attribute::id</TD><TD>../@id</TD></TR><TR><TD>a2</TD><TD>a2</TD></TR></TABLE>
      
        
      
      <H3>BBBb5</H3><TABLE border="1"><TR><td>full</TH><td>abbreviated</TH></TR><TR><TD>parent::*/attribute::id</TD><TD>../@id</TD></TR><TR><TD>a2</TD><TD>a2</TD></TR></TABLE>


List the elements in an xml document

File: Data.xml
<?xml version="1.0"?>
<list xml:lang="en">
  <title>title 1</title>
  <listitem>item 1</listitem>
  <listitem>item 2</listitem>
  <listitem>item 3</listitem>
  <listitem xml:lang="sw">item 4</listitem>
  <listitem xml:lang="en-gb">item 5</listitem>
  <listitem xml:lang="zu">item 6</listitem>
  <listitem xml:lang="jz">item 7</listitem>
</list>

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:value-of>
      <xsl:text>Here is a list of the </xsl:text>
      <xsl:value-of select="count(//*)"/>
      <xsl:text> elements in your document:&#xA;&#xA;</xsl:text>
    </xsl:value-of>
    <xsl:value-of select="//*/name()" separator="&#xA;"/>
  </xsl:template>
  
</xsl:stylesheet>
Output:
Here is a list of the 9 elements in your document:
list
title
listitem
listitem
listitem
listitem
listitem
listitem
listitem


template match="/"

File: Data.xml
<?xml version="1.0"?>
<list xml:lang="en">
  <title>title 1</title>
  <listitem>item 1</listitem>
  <listitem>item 2</listitem>
  <listitem>item 3</listitem>
  <listitem xml:lang="sw">item 4</listitem>
  <listitem xml:lang="en-gb">item 5</listitem>
  <listitem xml:lang="zu">item 6</listitem>
  <listitem xml:lang="jz">item 7</listitem>
</list>
File: Transform.xslt

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml"/>
  <xsl:template match="/">
    <xsl:copy-of select="."/>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><list xml:lang="en">
  <title>title 1</title>
  <listitem>item 1</listitem>
  <listitem>item 2</listitem>
  <listitem>item 3</listitem>
  <listitem xml:lang="sw">item 4</listitem>
  <listitem xml:lang="en-gb">item 5</listitem>
  <listitem xml:lang="zu">item 6</listitem>
  <listitem xml:lang="jz">item 7</listitem>
</list>


template match="*" (asterisk)

File: Data.xml
<?xml version="1.0"?>
<list xml:lang="en">
  <title>title 1</title>
  <listitem>item 1</listitem>
  <listitem>item 2</listitem>
  <listitem>item 3</listitem>
  <listitem xml:lang="sw">item 4</listitem>
  <listitem xml:lang="en-gb">item 5</listitem>
  <listitem xml:lang="zu">item 6</listitem>
  <listitem xml:lang="jz">item 7</listitem>
</list>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml"/>
  <xsl:template match="*">
    <xsl:copy/>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><list/>


template match="brand|name|units"

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<report month="8" year="2006">
  <title>Chocolate bar sales</title>
  <brand>
    <name>Lindt</name>
    <units>27408</units>
  </brand>
  <brand>
    <name>Callebaut</name>
    <units>8203</units>
  </brand>
</report>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml"/>
  <xsl:template match="report">
    <report>
      <xsl:apply-templates select="brand"/>
    </report>
  </xsl:template>
  <xsl:template match="brand|name|units">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>    
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><report><brand>
    <name>Lindt</name>
    <units>27408</units>
  </brand><brand>
    <name>Callebaut</name>
    <units>8203</units>
  </brand></report>


value-of select="."

File: Data.xml
<?xml version="1.0"?>
<greeting>
  Hello, World!
</greeting>

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <xsl:apply-templates select="greeting"/>
  </xsl:template>
  
  <xsl:template match="greeting">
    <html>
      <body>
        <h1>
          <xsl:value-of select="."/>
        </h1>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Output:
<html>
   <body>
      <h1>
         Hello, World!
         
      </h1>
   </body>
</html>


Wildcard "*" selects all possibilities

File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<employee>
  <firstName>Joe</firstName>
  <surname>Smith</surname>
</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="*">
      <div>
        <xsl:text>[template: </xsl:text>
        <xsl:value-of select="name()"/>
        <xsl:text> outputs </xsl:text>
        <xsl:apply-templates/>
        <xsl:text> ]</xsl:text>
      </div>
    </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><div>[template: employee outputs 
  <div>[template: firstName outputs Joe ]</div>
  <div>[template: surname outputs Smith ]</div>
 ]</div>