XML Tutorial/XPath/current — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:26, 26 мая 2010
Содержание
current() and name()
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<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>
</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="/data">
<TABLE border="1">
<xsl:apply-templates select="*[@cell="1"]"/>
</TABLE>
</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"?><TABLE border="1"><TR><TD> cell 1 </TD><TD> cell 2 </TD></TR><TR><TD> name 1 </TD><TD> name 2 </TD></TR><TR><TD> size 1 </TD><TD> size 2 </TD></TR></TABLE>
current name
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>
current() returns a node-set that has the current node as its only member
File: Data.xml
<?xml version="1.0" encoding="utf-8"?>
<data>
<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>
</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">
<TR>
<td> . </TH>
<td>current()</TH>
</TR>
<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"?><TABLE border="1"><TR><td> .
</TH><td>current()</TH></TR><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>
get current node with current() function
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>
Referring to the Current Node
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>
<b>
<xsl:value-of select="." />
:
</b>
</nobr>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<nobr><b>T1
:
</b></nobr>T2
Sort by current value
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