XML Tutorial/XSLT stylesheet/count

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

count elements which occured in XML source

   <source lang="xml">

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

   <AAA>
     <BBB id="b1">
       <CCC name="q" size="12"/>
       <EEE id="e1">
         <SSS/>
       </EEE>
       <CCC weight="45"/>
       <CCC/>
     </BBB>
   </AAA>
   <AAA>
     <EEE id="e2"/>
     <CCC>
       <DDD/>
       <DDD/>
     </CCC>
   </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="/descendant::*"> <xsl:variable name="pos" select="position()"/> <xsl:if test="not(/descendant::*[position() < $pos and name()=name(current())])"> <TR> <TD> <xsl:value-of select="name()"/> </TD> <TD> <xsl:value-of select="count(/descendant::*[name()=name(current())])"/> </TD> </TR> </xsl:if> </xsl:for-each> <TR> <td>Total count</TH> <td> <xsl:value-of select="count(//*)"/> </TH> </TR> </TABLE> </xsl:template> </xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>
Element</TH>
         <td>Occurs</TH>
<TR><TD>data</TD><TD>1</TD></TR><TR><TD>AAA</TD><TD>2</TD></TR><TR><TD>BBB</TD><TD>1</TD></TR><TR><TD>CCC</TD><TD>4</TD></TR><TR><TD>EEE</TD><TD>2</TD></TR><TR><TD>SSS</TD><TD>1</TD></TR><TR><TD>DDD</TD><TD>2</TD></TR><TR><td>Total count</TH><td>13</TH></TR></TABLE></source>


Count matches

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <results group="A">

 <match>
   <date>10-Jun-98</date>
   <team score="2">team 1</team>
   <team score="1">team 2</team>
 </match>
 <match>
   <date>10-Jun-98</date>
   <team score="2">team 3</team>
   <team score="2">team 4</team>
 </match>
 <match>
   <date>16-Jun-98</date>
   <team score="1">team 2</team>
   <team score="1">team 4</team>
 </match>

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

 version="2.0">
 <xsl:variable name="teams" select="distinct-values(//team)" />
 <xsl:variable name="matches" select="//match" />
 <xsl:template match="results">
   <league>
     <xsl:for-each select="$teams">
       <xsl:variable name="this" select="." />
       <xsl:variable name="played"
         select="count($matches[team=$this])" />
       <xsl:variable name="won"
         select="count($matches[team[.=$this]/@score > team[.!=$this]/@score])" />
       <xsl:variable name="lost"
         select="count($matches[team[.=$this]/@score < team[.!=$this]/@score])" />
       <xsl:variable name="drawn"
         select="count($matches[team[.=$this]/@score = team[.!=$this]/@score])" />
       <xsl:variable name="for"
         select="sum($matches/team[.=current()]/@score)" />
       <xsl:variable name="against"
         select="sum($matches[team=current()]/team/@score) - $for" />
       <team name="{.}" played="{$played}" won="{$won}"
         drawn="{$drawn}" lost="{$lost}" for="{$for}" against="{$against}" />
     </xsl:for-each>
   </league>
 </xsl:template>

</xsl:transform> Output: <?xml version="1.0" encoding="UTF-8"?><league> <team name="team 1" played="1" won="1" drawn="0" lost="0" for="2" against="1"/><team name="team 2" played="2" won="0" drawn="1" lost="1" for="2" against="3"/> <team name="team 3" played="1" won="0" drawn="1" lost="0" for="2" against="2"/><team name="team 4" played="2" won="0" drawn="2" lost="0" for="3" against="3"/></league></source>


Count node

   <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="people">
   <xsl:apply-templates select="person"/>
 </xsl:template>
 <xsl:template match="person">
   Person <xsl:value-of select="position()"/> 
   of <xsl:value-of select="count(//person)"/>:
   <xsl:value-of select="name"/>
 </xsl:template>

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

   Person 1 
   of 2:
   
     A
     B
   
   Person 2 
   of 2:
   
     D
     E
     F</source>
   
  

count()- Takes a node-set argument and returns a value equal to the number of nodes in the node-set

   <source lang="xml">

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

 <state>Belgium</state>
 <state>Germany</state>
 <state>Finland</state>
 <state>Greece</state>
 <state>San Marino</state>
 <state>Switzerland</state>

</europe>

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="europe">
   <xsl:text>
Total Number of States: </xsl:text>
   <xsl:value-of select="count(state)" />
   <xsl:text>

</xsl:text>
   <xsl:apply-templates select="state">
     <xsl:sort />
   </xsl:apply-templates>
 </xsl:template>
 <xsl:template match="state">
   <xsl:text> - </xsl:text>
   <xsl:apply-templates />
   <xsl:text>
</xsl:text>
 </xsl:template>

</xsl:stylesheet> Output: Total Number of States: 6

- Belgium
- Finland
- Germany
- Greece
- San Marino
- Switzerland</source>
   
  

The count function returns the number of nodes in the argument node-set.

   <source lang="xml">

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

   <AAA>
     <CCC/>
     <BBB>
       <CCC>Carl</CCC>
     </BBB>
     <BBB/>
     <BBB/>
   </AAA>
   <AAA>
     <CCC/>
     <BBB/>
     <BBB>
       <CCC>John</CCC>
       <CCC>Charles</CCC>
       <CCC>Robert</CCC>
       <CCC>Anthony</CCC>
     </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:text>//AAA : </xsl:text>
       
       <xsl:value-of select="count(//AAA)"/>
       
         <xsl:text>//CCC : </xsl:text>
       
       <xsl:value-of select="count(//CCC)"/>
       
         <xsl:text>//AAA/CCC : </xsl:text>
       
       <xsl:value-of select="count(//AAA/CCC)"/>
       
         <xsl:text>//CCC[text()]) : </xsl:text>
       
       <xsl:value-of select="count(//CCC[text()])"/>
   </xsl:template>

</xsl:stylesheet> Output:

<?xml version="1.0" encoding="UTF-8"?>
//AAA : 2
//CCC : 7
//AAA/CCC : 2
//CCC[text()]) : 5
</source>


Use count function in math calculation

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <numbers>

 <x>4</x>
 <y>3.2</y>
 <z>11</z>

</numbers>

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

 version="1.0">
 <xsl:output method="xml" omit-xml-declaration="yes" />
 <xsl:template match="numbers">
   11 + count(*) =
   <xsl:value-of select="11+count(*)" />
 </xsl:template>

</xsl:stylesheet> Output:

   11 + count(*) =
   14</source>
Element</TH><td>Occurs</TH>