Tip What is XPath

XML Path Language (XPath) is a query language used to create expressions for finding data in the XML. These expressions can manipulate strings, numbers, and Boolean values. They can also be used to navigate an XML tree structure and process its elements with XSLT instructions. XPath is designed to be used by XSL Transformations (XSLT) (see Tip 17-9). With XPath expressions, you can easily identify and extract from the XML document specific elements (nodes) based on their type, name, values, or the relationship of a node to other nodes. When preparing stylesheets for transforming your XML documents into HTML, you will often use various XPath expressions in the Select attribute. For example, to tell the XSLT processor to display the value of the current <Sessions> element, you will use the following tag:

<xsl:value-of select="Sessions"/>

To gather nodes of the same type so that you can iterate through them in your stylesheet, you might use the following tag:

<xsl:for-each select="Courses/Course"/>

The above directive tells the XSLT processor to go through all the course elements in the root node (Courses) and retrieve the value from the node's contents.

9. At this point, you can write the closing tags </CENTER>, </BODY>, and </HTML> to complete your stylesheet document, or you can proceed to add additional information that you want to display for users to see once the formatted XML document is viewed in the browser. Let's add the information about the author and the name of the stylesheet used. Because we don't want this information centered, we will start off by closing the <CENTER> tag:

<FONT face="Tahoma">

Using Sample XSL stylesheet 'Courses.xsl' prepared by 'put your name here'

Notice that before displaying the text, we placed a horizontal line using the <HR/> tag. While in a plain vanilla HTML document you can use the <HR> tag to place a horizontal line, the XSL stylesheet is an XML document and requires that all tags be closed. Hence, you need to write it as <HR/> or you'll get an error.

10. Save your document as Courses.xsl. Make sure to use the .xsl extension. Pay attention to this extension, as it is easy to get carried away and mix it up with the Excel .xls file extension. The completed XSL stylesheet is illustrated in Figure 17-13.

Äj r.nursi;* ml Wufrpnd ,

Fia Edt Ferrit Hdp j

*7xm; vereion='i .cr'?*


=HTML rnilnsMsl^hrtipaffftWff^.or^RWb-Ksr^


'TrrL^VB* course 8d»duie«/TTrLE>


"SODY agcalar^yellow*


« stro wo>vba course Gc iieduie -uetc oho <fj>

=T£BLE border = '1" cellRiddina^T cesiBpacins^T*


'"iH=-Ccurse TiUesiTH>

■iTH* Start Date'jTH>

=TH=No o* Sessions =fTH=


«.'tsLfor-each select='CoursesiCouFS(i's


«TO»««» I value- of p-'Jïd»

-TO =» «ksImIub-dî se leCfcTlB 3*

*TC'<xslvaiue-DT sEiEcfc" standate" l>«/TD>

eTC==xslxaluB-cf se Ibcë'Sbse id is1





cHË Edigns'leff^


= 'Of-JT facec'Tahom s*^

Usine SamplE XSL Gtye GhEat cours 3s.ner prepsred bv Julltta Kami




0 0

Post a comment