XML Tutorial/XPath/current

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

current() and name()

   <source lang="xml">

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

   <item cell="1"> cell 1 </item>
   <item cell="2"> cell 2 </item>
   <name cell="1"> name 1 </name>
   <name cell="2"> name 2 </name>
   <size cell="1"> size 1 </size>
   <size cell="2"> size 2 </size>

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="/data">
<xsl:apply-templates select="*[@cell="1"]"/>
   </xsl:template>
   <xsl:template match="*">
     <TR>
       <TD>
         <xsl:value-of select="."/>
       </TD>
       <TD>
         <xsl:value-of select="//*[name()=name(current()) and @cell="2"]"/>
       </TD>
     </TR>
   </xsl:template>

</xsl:stylesheet> Output:

<?xml version="1.0" encoding="UTF-8"?>
cell 1 cell 2
name 1 name 2
size 1 size 2
</source>


current name

   <source lang="xml">

File: Data.xml <?xml version="1.0"?> <root ref="rootref" name="blue">

 <glossary>
   <item ref="blue" name="rootref">rootref</item>
 </glossary>
 <glossary>
   <item ref="itemref" name="itemref">itemref</item>
 </glossary>

</root> 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" indent="yes" />
 <xsl:template match="/">
   <xsl:apply-templates select="root" />
 </xsl:template>
 <xsl:template match="root">
   <xsl:for-each select="//glossary/item[@name=current()/@ref]">
     <xsl:copy-of select="." />
     <xsl:for-each select=".">
       <xsl:for-each select="//glossary/item[@name=current()/@ref]">
         <xsl:copy-of select="." />
       </xsl:for-each>
     </xsl:for-each>
   </xsl:for-each>
   <xsl:for-each select="//glossary/item[@name=./@ref]">
     <xsl:copy-of select="." />
   </xsl:for-each>
   <xsl:for-each select=".">
     <xsl:copy-of select="." />
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?> <item ref="blue" name="rootref">rootref</item> <item ref="itemref" name="itemref">itemref</item> <root ref="rootref" name="blue">

   <glossary>
       <item ref="blue" name="rootref">rootref</item>
   </glossary>
   <glossary>
       <item ref="itemref" name="itemref">itemref</item>
   </glossary>

</root></source>


current() returns a node-set that has the current node as its only member

   <source lang="xml">

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

   <Choices name="first">
     <Choice name="first">11111</Choice>
     <Choice name="second">22222</Choice>
   </Choices>
   <Choices name="second">
     <Choice name="first">33333</Choice>
     <Choice name="second">44444</Choice>
   </Choices>

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:apply-templates select="//Choices"/> </TABLE> </xsl:template> <xsl:template match="Choices"> <TR> <TD> <xsl:value-of select="./@name"/> </TD> <TD> <xsl:value-of select="current()/@name"/> </TD> </TR> <TR> <TD> <xsl:apply-templates select="Choice[./@name="first"]"/> </TD> <TD> <xsl:apply-templates select="Choice[current()/@name="first"]"/> </TD> </TR> </xsl:template> </xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>
. </TH>
         <td>current()</TH>
<TR><TD>first</TD><TD>first</TD></TR><TR> <TD>11111</TD><TD>1111122222</TD></TR><TR><TD>second</TD><TD>second</TD></TR><TR><TD>33333</TD><TD/></TR></TABLE></source>

get current node with current() function

   <source lang="xml">

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

 <PRODUCT>
   <ID>ABC</ID>
   <NAME>SQL server</NAME>
 </PRODUCT>
 <PRODUCT>
   <ID>DEF</ID>
   <NAME>Black cat</NAME>
 </PRODUCT>
 <PRODUCT>
   <ID>API</ID>
   <NAME>Swing</NAME>
 </PRODUCT>
 <PRODUCT>
   <ID>JKL</ID>
   <NAME>Oracle</NAME>
 </PRODUCT>
 <SELECTEDPRODUCT>ABC</SELECTEDPRODUCT>
 <SELECTEDPRODUCT>API</SELECTEDPRODUCT>

</RESPONSE> 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="//SELECTEDPRODUCT">
       <Paragraph>
         <xsl:text>NAME: </xsl:text>
         <xsl:value-of select="//PRODUCT/NAME[../ID=current()]"/>
       </Paragraph>
     </xsl:for-each>
   </xsl:template>

</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?><Paragraph>NAME: SQL server</Paragraph><Paragraph>NAME: Swing</Paragraph></source>


Referring to the Current Node

   <source lang="xml">

File: Data.xml

<?xml version="1.0"?> <employees>

 <animal>
   <name language="English">T1</name>
   <name language="Latin">T2</name>
   <projects>
     <project>project1</project>
   </projects>
 </animal>

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

 version="1.0">
 <xsl:template match="animal">
   <xsl:apply-templates select="name" />
 </xsl:template>
 <xsl:template match="name[@language="English"]">
   <nobr>
     
       <xsl:value-of select="." />
       :
     
   </nobr>
 </xsl:template>

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

 <nobr>T1
       :
     </nobr>T2</source>
   
  

Sort by current value

   <source lang="xml">

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

 <number>127</number>
 <number>23</number>
 <number>10</number>

</numberlist> 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" indent="no"/>
 <xsl:strip-space elements="*"/>
 <xsl:variable name="newline">
 <xsl:text></xsl:text>
 </xsl:variable>
 <xsl:template match="/">
   <xsl:for-each select="numberlist/number">
     <xsl:sort select="."/>
     <xsl:value-of select="."/>
     <xsl:value-of select="$newline"/>
   </xsl:for-each>
 </xsl:template>

</xsl:stylesheet> Output: 1012723</source>

. </TH><td>current()</TH>