250 likes | 389 Views
This article explores the concepts of XML namespaces, XPath, and XSLT (Extensible Stylesheet Language Transformations). XML namespaces help prevent element name conflicts and retain original meanings when combining documents, while XPath enables navigation within XML documents using path expressions. XSLT is essential for displaying and transforming XML data into various formats. The discussion includes practical examples, syntax, fundamental terminologies, and key relationships within XML structures.
E N D
Extensible Markup Language II Namespace, XPath and XSL
XML Namespace NamespaceS
XML Namespaces • Namespaces – provide method to avoid element name conflicts • Collections of names identified by URIs • Namespaces allow names within a document to retain their original meanings even when combined with other document • However, if you don’t need namespaces, don’t use them
Conflict examples <?xml version="1.0" encoding="ISO-8859-1"?><Order> <Customer> <Name>Johan Abdullah</Name> <Number>AE15562</Number> </Customer> <Items> <Item> <Name>Pioneer Hi-Fi</Name> <Number>PI-032-EX</Number> <Item> </Items> </Order>
Namespaces Solution <?xml version="1.0" encoding="ISO-8859-1"?><Order xmlns:cus=“http://example.com/Customer” xmlns:item=“http://example.com/Item”> <cus:Customer> <cus:Name>Johan Abdullah</cus:Name> <cus:Number>AE15562</cus:Number> </cus:Customer> <item:Items> <item:Item> <item:Name>Pioneer Hi-Fi</item:Name> <item:Number>PI-032-EX</item:Number> </item:Item> </item:Items> </Order>
Namespaces • Namespace declaration: • Defined by xmlns attribute xmlns:prefix = “URI” • Namespace URI is not used by parser to look for information
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"><html><body> <h2>My CD Collection</h2> <table border="1"> <tr> <th align="left">Title</th> <th align="left">Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table></body></html></xsl:template></xsl:stylesheet> Namespace for non-HTML tags (with xsl prefix, marked with red arrows)
Navigating XML Document XPath
XPath • Xpath – language to navigate and find information in XML document • Uses path expressions to navigate XML documents • Contains a library of standard functions
Xpath Path Expressions/Functions • Xpath uses path expressions • Use path expressions to select nodes/node-sets in XML documents • Path expressions – similar to computer file system • Xpath includes more than 100 functions • Functions for string and numeric values, date/time comparison, node/QName manipulation, Boolean values etc.
XPath Nodes - terminology • Seven kinds of nodes: • Element • Attribute • Text • Namespace • Processing-instruction (PI) • Comment • Document nodes • Atomic values – nodes without children or parent • Items – atomic values or nodes
XPath Nodes - relationship • Types of relationship: • Parent • Children • Siblings • Ancestors • Descendants
XPath Syntax • Selecting nodes
XPath Syntax • Predicates – used to find a specific node/node with a specific value
XPath Wildcards/| operator • Wildcards to select unknown XML elements • Use ‘|’ to select several paths
More XPath References • XPath Axes - http://www.w3schools.com/xpath/xpath_axes.asp • XPath Operators - http://www.w3schools.com/xpath/xpath_operators.asp • XPath functions (with XQuery, XSLT) - http://www.w3schools.com/xpath/xpath_functions.asp
XPath Example • Using Javascript – using “kereta.xml” • Edit the “ex1.html” , “ex2.html” and “ex3.html” files • Understand the usage of XPath and Javascript
Displaying XML • XML documents don’t carry information about how to display data • 3 ways to display data: • XSLT (Extensible Stylesheet Transformation Language) • CSS (Cascading Style Sheet) • JavaScript
XSL • XSL – Extensible Stylesheet Language • Style sheets for XML • Describes how the XML document should be displayed • Consisted of: • XSLT – transforming XML documents • XPath – navigating XML documents • XSL-FO – formatting XML documents
XSL Transformation • XSLT – XSL Transformation • Transform XML documents to other formats (e.g. XHTML) – transform each XML element to XHTML element • Supported by all major browsers • Most important part of XSL • Allows adding/removing elements and attributes to/from output file, rearranging/sorting elements, perform test etc.
XSLT - Transformation • Example of a transformation – steps: • Start with a raw XML document • Declare stylesheet • Create XSL style sheet • Link style sheet to the XML document
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <trbgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html></xsl:template></xsl:stylesheet> XSLT example
XSLT Example • Using “kereta.xml” • Create/Edit the XSLT files to show various combination of sorting, filtering, formatting etc. • Understand the usage of XSLT