XML : eXtensible Markup Language :: Introduction to XPath
What is XPath |
- XPath is a language for finding information in an XML document.
- XPath is used to navigate through elements and attributes in an XML document.
- XPath is a major element in the W3C’s XSLT standard – and XQuery and XPointer are both built on XPath expressions.
- So an understanding of XPath is fundamental to a lot of advanced XML usage.
Features of XPath? |
- XPath is a syntax for defining parts of an XML document
- XPath uses path expressions to navigate in XML documents
- XPath contains a library of standard functions
- XPath is a major element in XSLT
- XPath is a W3C Standard
Example of XPath |
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee id="e001">
<image url="http://payroll.com/john.gif" />
<name>John</name>
<age>20</age>
</employee>
<employee id="e002">
<image url="http://payroll.com/james.gif" />
<name>James</name>
<age>30</age>
</employee>
</employees>
XPath Expression Output
/employees/employee[1] :: Selects the first employee element that is the child of the employees element
/employees/employee[last()] :: Selects the last employee element that is the child of the employees element
/employees/employee[last()-1] :: Selects the last but one employee element that is the child of the employees element
/employees/employee[position()<3] :: Selects the first two employee elements that are children of the employees element
Example 1 :
Product.xls
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:for-each select="PRODUCTDATA/PRODUCT[@PRODID='P002']">
PoductName: <xsl:value-of select="PRODUCTNAME"/><br/>
PoductCategory: <xsl:value-of select="@CATEGORY"/><br/>
PoductPrice of Current (N1): <xsl:value-of select='(PRICE)' /><br/> <!--Current -->
PoductPrice of Current (N2): <xsl:value-of select='(./PRICE)' /><br/> <!--Current -->
PoductPrice of CurrentNode of Parent (N3): <xsl:value-of select='(..//PRICE)' /><br/> <!--Current of Parent-->
<hr/>
Sum of N1 and N2: <xsl:value-of select='(./PRICE +..//PRICE)'/> <br/>
Sub of N1 and N2: <xsl:value-of select='(./PRICE - ..//PRICE)'/> <br/>
Mul of N1 and N2: <xsl:value-of select='(./PRICE * ..//PRICE)'/> <br/>
Div of N1 and N2: <xsl:value-of select='(./PRICE div ..//PRICE)'/> <br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Product.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="Products.xsl"?>
<PRODUCTDATA>
<PRODUCT PRODID="P001" CATEGORY="Toy">
<PRODUCTNAME>Mini Bus</PRODUCTNAME>
<DESCRIPTION>This is a toy for children aged 4 and above </DESCRIPTION>
<PRICE>54</PRICE>
<QUANTITY>100</QUANTITY>
</PRODUCT>
<PRODUCT PRODID="P002" CATEGORY="Book">
<PRODUCTNAME>The English Patient</PRODUCTNAME>
<DESCRIPTION>This is a book set during the second world war</DESCRIPTION>
<PRICE>19</PRICE>
<QUANTITY>75</QUANTITY>
</PRODUCT>
<PRODUCT PRODID="P003" CATEGORY="Toy">
<PRODUCTNAME>Race Car</PRODUCTNAME>
<DESCRIPTION>This is a toy for children aged 12 and above</DESCRIPTION>
<PRICE>45</PRICE>
<QUANTITY>54</QUANTITY>
</PRODUCT>
</PRODUCTDATA>
Example 2 :
Product.xls
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<table border="1">
<tr><td>ProductId</td><td>Price</td><td>Tax in %</td><td>Total</td></tr>
<xsl:for-each select="PRODUCTDATA/PRODUCT">
<tr>
<td><xsl:value-of select="@PRODID"/></td>
<td><xsl:value-of select="PRICE"/></td>
<td><xsl:value-of select="TAX"/></td>
<td><xsl:value-of select='(PRICE)+ (PRICE * TAX div 100)'/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Product.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="Product.xsl"?>
<PRODUCTDATA>
<PRODUCT PRODID="P001" CATEGORY="Toy">
<PRODUCTNAME>Mini Bus</PRODUCTNAME>
<DESCRIPTION>This is a toy for children aged 4 and above</DESCRIPTION>
<PRICE>752</PRICE>
<TAX>10</TAX>
<QUANTITY>54</QUANTITY>
</PRODUCT>
<PRODUCT PRODID="P002" CATEGORY="Toy">
<PRODUCTNAME>Barbie Doll</PRODUCTNAME>
<DESCRIPTION>This toy is for children in the age group of 5-8</DESCRIPTION>
<PRICE>200</PRICE>
<TAX>10</TAX>
<QUANTITY>200</QUANTITY>
</PRODUCT>
<PRODUCT PRODID="P003" CATEGORY="Game">
<PRODUCTNAME>Video Game </PRODUCTNAME>
<DESCRIPTION>This toy is for children in the age group of 5-10</DESCRIPTION>
<PRICE>600</PRICE>
<TAX>10</TAX>
<QUANTITY>300</QUANTITY>
</PRODUCT>
<PRODUCT PRODID="P004" CATEGORY="Game">
<PRODUCTNAME>Video Game CD </PRODUCTNAME>
<DESCRIPTION>This toy is for children in the age group of 5-10</DESCRIPTION>
<PRICE>986</PRICE>
<TAX>5</TAX>
<QUANTITY>300</QUANTITY>
</PRODUCT>
</PRODUCTDATA>
Example 3 :
Student.xls
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<TABLE border="5" >
<xsl:apply-templates select="//Student"/>
</TABLE>
</xsl:template>
<xsl:template match="//Student">
<xsl:if test="position() mod 2 = 0">
<TR BGCOLOR ="Yellow">
<TD><xsl:value-of select ="RollNo"/></TD>
<TD><xsl:value-of select ="Name"/></TD>
<TD><xsl:value-of select ="@Div"/></TD>
</TR>
</xsl:if>
<xsl:if test="position() mod 2 != 0">
<TR BGCOLOR ="wheat">
<TD><xsl:value-of select ="RollNo"/></TD>
<TD><xsl:value-of select ="Name"/></TD>
<TD><xsl:value-of select ="@Div"/></TD>
</TR>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Student.xml
<?xml-stylesheet type="text/xsl" href="Student.xsl" ?>
<School>
<Student Div="A">
<RollNo>1001</RollNo>
<Name>Jack</Name>
<Std>5</Std>
</Student>
<Student Div="B">
<RollNo>1002</RollNo>
<Name>Jill</Name>
<Std>5</Std>
</Student>
<Student Div="B">
<RollNo>1003</RollNo>
<Name>Jill</Name>
<Std>5</Std>
</Student>
<Student Div="B">
<RollNo>1004</RollNo>
<Name>Jill</Name>
<Std>5</Std>
</Student>
<Student Div="B">
<RollNo>1005</RollNo>
<Name>Jill</Name>
<Std>5</Std>
</Student>
</School>
Recent Comments