|
11/23 |
2000/7/4-5 [Computer/SW/Languages/Web, Computer/SW/Languages/Misc] UID:18581 Activity:nil |
7/3 How does one tell a browser to use a specified stylesheet with a given xml doc? I use <?xml-stylesheet type="text/xml" href="./style.xsl"?> but my browser (IE) still displays it in the default xml-only format. Does using a stylesheet require a DTD? \_ No, IE is just very finicky about your stylesheet and has poor error reporting- if your xsl is bad, it just won't display anything. You have to specify the correct names space, and (probably) include some default templates to match for stuff that you otherwise wouldn't bother accounting for, for example: <xsl:template match="text()|@*"> <xsl:value-of select="."/> </xsl:template> see also http://metalab.unc.edu/xml/books/bible/updates/14.html#Default http://metalab.unc.edu/xml/books/bible/updates/14.html#Overview |
11/23 |
|
metalab.unc.edu/xml/books/bible/updates/14.html#Default -> www.ibiblio.org/xml/books/bible2/chapters/ch17.html So far, I've merely tested for the presence of various nodes. However, you can test for more details about the nodes that match a pattern using . You can perform many different tests including: * Whether an element contains a given child, attribute, or other node * Whether the value of an attribute is a certain string * Whether the value of an element matches a string * What position a given node occupies in the hierarchy For example, seaborgium, element 106, has only been created in microscopic quantities. Even its most long-lived isotope has a half-life of only 30 seconds. With such a hard-to-create, short-lived element, it's virtually impossible to measure the density, melting point, and other bulk properties. Consequently, the periodic table document omits the elements describing the bulk properties of seaborgium and similar atoms because the data simply doesn't exist. If you want to create a table of atomic number versus melting point, you should omit those elements with unknown melting points. This rule will override the previous one for those atoms that do have melting points. The test brackets can contain more than simply a child-element name. For example, this template rule matches ATOM elements with NAME or SYMBOL children. An equals sign can test whether the value of a node identically matches a given string. For example, this template finds the ATOM element that contains an ATOMIC_NUMBER element whose content is the string 10 (Neon). You may find it easier to test against attribute values since those are less likely to contain insignificant white space. For example, the style sheet in Listing 17-9 applies templates only to those ATOM elements whose STATE attribute value is the three letters GAS. For example, you can select all elements whose names begin with "A" or all elements with an atomic number less than 100. XPath Expressions for Selecting Nodes The select attribute is used in xsl:apply-templates, xsl:value-of, xsl:for-each, xsl:copy-of, xsl:variable, xsl:param, and xsl:sort to specify exactly which nodes are operated on. The value of this attribute is an expression written in the XPath language. The XPath language provides a means of identifying a particular element, group of elements, text fragment, or other part of an XML document. Expressions are a superset of the match patterns discussed in the last section. That is, all match patterns are expressions, but not all expressions are match patterns. Recall that match patterns enable you to match nodes by element name, child elements, descendants, and attributes, as well as by making simple tests on these items. XPath expressions allow you to select nodes through all these criteria but also by referring to ancestor nodes, parent nodes, sibling nodes, preceding nodes, and following nodes. Furthermore, expressions aren't limited to producing merely a list of nodes, but can also produce booleans, numbers, and strings. Node axes Expressions are not limited to specifying the children and descendants of the current node. XPath provides a number of axes that you can use to select from different parts of the tree relative to some particular node in the tree called the context node. In XSLT, the context node is normally initialized to the current node that the template matches, though there are ways to change this. The axis is generally followed by a double colon (::) and a node test that further winnows down this node set. For example, a node test may contain the name of the element to be selected as in the following template rule: <xsl:template match="ATOM"> <tr> <td> <xsl:value-of select="child::NAME"/> </td> <td> <xsl:value-of select="child::ATOMIC_NUMBER"/> </td> <td> <xsl:value-of select="child::ATOMIC_WEIGHT"/> </td> </tr> </xsl:template> The template rule matches ATOM elements. When an ATOM element is matched, that element becomes the context node. A NAME element, an ATOMIC_NUMBER element, and an ATOMIC_WEIGHT element are all selected from the children of that matched ATOM element and output as table cells. In fact select="ATOMIC_WEIGHT" is just an abbreviated form of select="child::ATOMIC_WEIGHT". Referring to the parent element is illegal in match patterns, but not in expressions. For example, this template matches BOILING_POINT elements but outputs the value of the parent ATOM element: <xsl:template match="BOILING_POINT"> <P><xsl:value-of select="parent::ATOM"/></P> </xsl:template> Some radioactive atoms such as polonium have half-lives so short that bulk properties such as the boiling point and melting point can't be measured. Therefore, not all ATOM elements necessarily have BOILING_POINT child elements. The above rule enables you to write a template that only outputs those elements that actually have boiling points. Expanding on this example, Listing 17-10 matches the MELTING_POINT elements but actually outputs the parent ATOM element using parent::ATOM. For example, this rule inserts the value of the nearest PERIODIC_TABLE element that contains the matched SYMBOL element. If the matched element is a PERIODIC_TABLE, then that very PERIODIC_TABLE is selected in xsl:value-of. The processing-instruction() function selects a processing instruction node, and the node() function selects any type of node. Hierarchy operators You can use the / and // operators to string expressions together. For example, Listing 17-11 prints a table of element names, atomic numbers, and melting points for only those elements that have melting points. It does this by selecting the parent of the MELTING_POINT element, then finding that parent's NAME and ATOMIC_NUMBER children with select="parent::*/child::NAME)". Melting Point</h1> <table> <th>Element</th> <th>Atomic Number</th> <th>Melting Point</th> <xsl:apply-templates select="child::ATOM"/> </table> </body> </html> </xsl:template> <xsl:template match="ATOM"> <xsl:apply-templates select="child::MELTING_POINT"/> </xsl:template> <xsl:template match="MELTING_POINT"> <tr> <td> <xsl:value-of select="parent::*/child::NAME"/> </td> <td> <xsl:value-of select="parent::*/child::ATOMIC_NUMBER"/> </td> <td> <xsl:value-of select="self::*"/> <xsl:value-of select="attribute::UNITS"/> </td> </tr> </xsl:template> </xsl:stylesheet> This is not the only way to solve the problem. Another possibility is to use the preceding-sibling and following-sibling axes, or both if the relative location (preceding or following) is uncertain. The necessary template rule for the MELTING_POINT element looks like this: <xsl:template match="MELTING_POINT"> <tr> <td> <xsl:value-of select="preceding-sibling::NAME | following-sibling::NAME"/> </td> <td> <xsl:value-of select="preceding-sibling::ATOMIC_NUMBER | following-sibling::ATOMIC_NUMBER"/> </td> <td> <xsl:value-of select="self::*"/> <xsl:value-of select="attribute::UNITS"/> </td> </tr> </xsl:template> Abbreviated syntax The various axes in Table 17-2 are a bit too wordy for comfortable typing. XPath also defines an abbreviated syntax that can substitute for the most common of these axes and is more used in practice. Table 17-3: Abbreviated Syntax for XPath Expressions Abbreviation: Full: . The output produced by the two style sheets is exactly the same, however. The full syntax using the axes of Table 17-2 is restricted to expressions. Expression types Every expression evaluates to a single value. For example, the expression 3 + 2 evaluates to the value 5. The expressions used so far have all evaluated to node sets. However, there are five types of expressions in XSLT: * Node sets * Booleans * Numbers * Strings * Result tree fragments Node sets A node set is an unordered group of nodes from the input document. The axes in Table 17-2 all return a node set containing the nodes they match. Which nodes are in the node set depends on the context node, the node test, and the axis. For example, when the context node is the PERIODIC_TABLE element of Listing 17-1, the XPath expression select="child::ATOM" returns a node set that contains both ATOM elements in that document. The XPath expression select="child::ATOM/child::NAME" returns a node set containing the two element nodes <NAME>Hydrogen</NAME> and <NAME>Helium</NAME> when ... |
metalab.unc.edu/xml/books/bible/updates/14.html#Overview -> www.ibiblio.org/xml/books/bible2/chapters/ch17.html So far, I've merely tested for the presence of various nodes. However, you can test for more details about the nodes that match a pattern using . You can perform many different tests including: * Whether an element contains a given child, attribute, or other node * Whether the value of an attribute is a certain string * Whether the value of an element matches a string * What position a given node occupies in the hierarchy For example, seaborgium, element 106, has only been created in microscopic quantities. Even its most long-lived isotope has a half-life of only 30 seconds. With such a hard-to-create, short-lived element, it's virtually impossible to measure the density, melting point, and other bulk properties. Consequently, the periodic table document omits the elements describing the bulk properties of seaborgium and similar atoms because the data simply doesn't exist. If you want to create a table of atomic number versus melting point, you should omit those elements with unknown melting points. This rule will override the previous one for those atoms that do have melting points. The test brackets can contain more than simply a child-element name. For example, this template rule matches ATOM elements with NAME or SYMBOL children. An equals sign can test whether the value of a node identically matches a given string. For example, this template finds the ATOM element that contains an ATOMIC_NUMBER element whose content is the string 10 (Neon). You may find it easier to test against attribute values since those are less likely to contain insignificant white space. For example, the style sheet in Listing 17-9 applies templates only to those ATOM elements whose STATE attribute value is the three letters GAS. For example, you can select all elements whose names begin with "A" or all elements with an atomic number less than 100. XPath Expressions for Selecting Nodes The select attribute is used in xsl:apply-templates, xsl:value-of, xsl:for-each, xsl:copy-of, xsl:variable, xsl:param, and xsl:sort to specify exactly which nodes are operated on. The value of this attribute is an expression written in the XPath language. The XPath language provides a means of identifying a particular element, group of elements, text fragment, or other part of an XML document. Expressions are a superset of the match patterns discussed in the last section. That is, all match patterns are expressions, but not all expressions are match patterns. Recall that match patterns enable you to match nodes by element name, child elements, descendants, and attributes, as well as by making simple tests on these items. XPath expressions allow you to select nodes through all these criteria but also by referring to ancestor nodes, parent nodes, sibling nodes, preceding nodes, and following nodes. Furthermore, expressions aren't limited to producing merely a list of nodes, but can also produce booleans, numbers, and strings. Node axes Expressions are not limited to specifying the children and descendants of the current node. XPath provides a number of axes that you can use to select from different parts of the tree relative to some particular node in the tree called the context node. In XSLT, the context node is normally initialized to the current node that the template matches, though there are ways to change this. The axis is generally followed by a double colon (::) and a node test that further winnows down this node set. For example, a node test may contain the name of the element to be selected as in the following template rule: <xsl:template match="ATOM"> <tr> <td> <xsl:value-of select="child::NAME"/> </td> <td> <xsl:value-of select="child::ATOMIC_NUMBER"/> </td> <td> <xsl:value-of select="child::ATOMIC_WEIGHT"/> </td> </tr> </xsl:template> The template rule matches ATOM elements. When an ATOM element is matched, that element becomes the context node. A NAME element, an ATOMIC_NUMBER element, and an ATOMIC_WEIGHT element are all selected from the children of that matched ATOM element and output as table cells. In fact select="ATOMIC_WEIGHT" is just an abbreviated form of select="child::ATOMIC_WEIGHT". Referring to the parent element is illegal in match patterns, but not in expressions. For example, this template matches BOILING_POINT elements but outputs the value of the parent ATOM element: <xsl:template match="BOILING_POINT"> <P><xsl:value-of select="parent::ATOM"/></P> </xsl:template> Some radioactive atoms such as polonium have half-lives so short that bulk properties such as the boiling point and melting point can't be measured. Therefore, not all ATOM elements necessarily have BOILING_POINT child elements. The above rule enables you to write a template that only outputs those elements that actually have boiling points. Expanding on this example, Listing 17-10 matches the MELTING_POINT elements but actually outputs the parent ATOM element using parent::ATOM. For example, this rule inserts the value of the nearest PERIODIC_TABLE element that contains the matched SYMBOL element. If the matched element is a PERIODIC_TABLE, then that very PERIODIC_TABLE is selected in xsl:value-of. The processing-instruction() function selects a processing instruction node, and the node() function selects any type of node. Hierarchy operators You can use the / and // operators to string expressions together. For example, Listing 17-11 prints a table of element names, atomic numbers, and melting points for only those elements that have melting points. It does this by selecting the parent of the MELTING_POINT element, then finding that parent's NAME and ATOMIC_NUMBER children with select="parent::*/child::NAME)". Melting Point</h1> <table> <th>Element</th> <th>Atomic Number</th> <th>Melting Point</th> <xsl:apply-templates select="child::ATOM"/> </table> </body> </html> </xsl:template> <xsl:template match="ATOM"> <xsl:apply-templates select="child::MELTING_POINT"/> </xsl:template> <xsl:template match="MELTING_POINT"> <tr> <td> <xsl:value-of select="parent::*/child::NAME"/> </td> <td> <xsl:value-of select="parent::*/child::ATOMIC_NUMBER"/> </td> <td> <xsl:value-of select="self::*"/> <xsl:value-of select="attribute::UNITS"/> </td> </tr> </xsl:template> </xsl:stylesheet> This is not the only way to solve the problem. Another possibility is to use the preceding-sibling and following-sibling axes, or both if the relative location (preceding or following) is uncertain. The necessary template rule for the MELTING_POINT element looks like this: <xsl:template match="MELTING_POINT"> <tr> <td> <xsl:value-of select="preceding-sibling::NAME | following-sibling::NAME"/> </td> <td> <xsl:value-of select="preceding-sibling::ATOMIC_NUMBER | following-sibling::ATOMIC_NUMBER"/> </td> <td> <xsl:value-of select="self::*"/> <xsl:value-of select="attribute::UNITS"/> </td> </tr> </xsl:template> Abbreviated syntax The various axes in Table 17-2 are a bit too wordy for comfortable typing. XPath also defines an abbreviated syntax that can substitute for the most common of these axes and is more used in practice. Table 17-3: Abbreviated Syntax for XPath Expressions Abbreviation: Full: . The output produced by the two style sheets is exactly the same, however. The full syntax using the axes of Table 17-2 is restricted to expressions. Expression types Every expression evaluates to a single value. For example, the expression 3 + 2 evaluates to the value 5. The expressions used so far have all evaluated to node sets. However, there are five types of expressions in XSLT: * Node sets * Booleans * Numbers * Strings * Result tree fragments Node sets A node set is an unordered group of nodes from the input document. The axes in Table 17-2 all return a node set containing the nodes they match. Which nodes are in the node set depends on the context node, the node test, and the axis. For example, when the context node is the PERIODIC_TABLE element of Listing 17-1, the XPath expression select="child::ATOM" returns a node set that contains both ATOM elements in that document. The XPath expression select="child::ATOM/child::NAME" returns a node set containing the two element nodes <NAME>Hydrogen</NAME> and <NAME>Helium</NAME> when ... |