XML Tutorial/XPath/Abbreivation

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

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

   <source lang="xml">

Axis attribute:: can be abbreviatet to @ . is short for self:: 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="AAA">

<xsl:value-of select="name()"/> <xsl:text/> <xsl:value-of select="@id"/>

<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"?>

AAAa1

full</TH>
         <td>abbreviated</TH>
<TR><TD>child::BBB/attribute::id</TD><TD>BBB/@id</TD></TR><TR><TD>b1</TD><TD>b1</TD></TR></TABLE>

AAAa2

full</TH><td>abbreviated</TH>
<TR><TD>child::BBB/attribute::id</TD><TD>BBB/@id</TD></TR><TR><TD>b3</TD><TD>b3</TD></TR></TABLE></source>


Count elements and attributes

   <source lang="xml">

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.
Have a great day!</xsl:text>
 </xsl:template>
 

</xsl:stylesheet> Output: Your document contains 9 elements and 5 attributes. Have a great day!</source>


"//" has two meanings

   <source lang="xml">

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

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

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">
       <xsl:value-of select="name()"/>
       <xsl:text> id=</xsl:text>
       <xsl:value-of select="@id"/>
   </xsl:template>
   <xsl:template match="BBB">
       <xsl:value-of select="name()"/>
       <xsl:text> id=</xsl:text>
       <xsl:value-of select="@id"/>
   </xsl:template>
   <xsl:template match="CCC">
       <xsl:value-of select="name()"/>
       <xsl:text> id=</xsl:text>
       <xsl:value-of select="@id"/>
   </xsl:template>
   <xsl:template match="DDD">
       <xsl:value-of select="name()"/>
       <xsl:text> id=</xsl:text>
       <xsl:value-of select="@id"/>
   </xsl:template>

</xsl:stylesheet>

<?xml version="1.0" encoding="UTF-8"?>
BBB id=b1
BBB id=b2
BBB id=b3
BBB id=b4
BBB id=b5
CCC id=c1
CCC id=c2
DDD id=d1
AAA id=a1
AAA id=a2
</source>


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

   <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="CCC">

<xsl:value-of select="name()"/> <xsl:text/> <xsl:value-of select="@id"/>

full</TH><td>abbreviated</TH>
<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"?>

CCCc1

full</TH>
         <td>abbreviated</TH>
<TR><TD>name(/descendant-or-self::*)</TD><TD>name(//*)</TD></TR><TR><TD>data</TD><TD>data</TD></TR></TABLE>

CCCc2

full</TH><td>abbreviated</TH>
<TR><TD>name(/descendant-or-self::*)</TD><TD>name(//*)</TD></TR><TR><TD>data</TD><TD>data</TD></TR></TABLE>


CCCc3

full</TH><td>abbreviated</TH>
<TR><TD>name(/descendant-or-self::*)</TD><TD>name(//*)</TD></TR><TR><TD>data</TD><TD>data</TD></TR></TABLE></source>


.. is short for parent::

   <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="BBB">

<xsl:value-of select="name()"/> <xsl:text/> <xsl:value-of select="@id"/>

full</TH><td>abbreviated</TH>
<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"?>

BBBb1

full</TH>
         <td>abbreviated</TH>
<TR><TD>parent::*/attribute::id</TD><TD>../@id</TD></TR><TR><TD>a1</TD><TD>a1</TD></TR></TABLE>

BBBb2

full</TH><td>abbreviated</TH>
<TR><TD>parent::*/attribute::id</TD><TD>../@id</TD></TR><TR><TD>a1</TD><TD>a1</TD></TR></TABLE>


BBBb3

full</TH><td>abbreviated</TH>
<TR><TD>parent::*/attribute::id</TD><TD>../@id</TD></TR><TR><TD>a2</TD><TD>a2</TD></TR></TABLE>

BBBb4

full</TH><td>abbreviated</TH>
<TR><TD>parent::*/attribute::id</TD><TD>../@id</TD></TR><TR><TD>a2</TD><TD>a2</TD></TR></TABLE>


BBBb5

full</TH><td>abbreviated</TH>
<TR><TD>parent::*/attribute::id</TD><TD>../@id</TD></TR><TR><TD>a2</TD><TD>a2</TD></TR></TABLE></source>


List the elements in an xml document

   <source lang="xml">

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:

</xsl:text>
   </xsl:value-of>
   <xsl:value-of select="//*/name()" separator="
"/>
 </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</source>


template match="/"

   <source lang="xml">

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


template match="*" (asterisk)

   <source lang="xml">

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


template match="brand|name|units"

   <source lang="xml">

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

value-of select="."

   <source lang="xml">

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>

<xsl:value-of select="."/>

     </body>
   </html>
 </xsl:template>

</xsl:stylesheet> Output: <html>

  <body>

Hello, World!

  </body>

</html></source>


Wildcard "*" selects all possibilities

   <source lang="xml">

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="*">
       <xsl:text>[template: </xsl:text>
       <xsl:value-of select="name()"/>
       <xsl:text> outputs </xsl:text>
       <xsl:apply-templates/>
       <xsl:text> ]</xsl:text>
   </xsl:template>

</xsl:stylesheet> Output:

<?xml version="1.0" encoding="UTF-8"?>
[template: employee outputs
[template: firstName outputs Joe ]
[template: surname outputs Smith ]
]
</source>
full</TH><td>abbreviated</TH>