Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 20 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
20
Dung lượng
225,72 KB
Nội dung
xsl:text Syntax The xsl:text element is used to insert text verbatim into the document: <xsl:text disable-output-escaping = “yes” | “no”> string data </xsl:text> By default, special XML characters will be escaped. By setting disable-output- escaping to “yes”, they will not be. Table 13.19 lists the parent-child relationships. Table 13.19 Parent-Child Relationships CAN BE A CHILD OF . . . CAN BE A PARENT OF . . . xsl:attribute xsl:comment xsl:copy xsl:element xsl:fallback xsl:for-each xsl:if xsl:message xsl:otherwise xsl:param xsl:processing-instruction xsl:template xsl:variable xsl:when 320 Chapter 13 Generally, xsl:text is used to include arbitrary amounts of whitespace in the end document. With the disable-output-escaping attribute set to yes, you can also use it to create XML tags in the text. However, you have to use the escaped characters inside the xml:text tag: <pre> <xsl:text> W o w ! ! ! ! W h i t e s p a c e ! ! ! Default tag: <aTag/> </xsl:text> <xsl:text disable-output-escaping=”yes”> disable-output-escaping tag: <aTag/> </xsl:text> </pre> xsl:comment Syntax The xsl:comment element generates an XML comment in the output: <xsl:comment> XSLT and XML </xsl:comment> Table 13.20 lists the parent-child relationships. Table 13.20 Parent-Child Relationships CAN BE A CHILD OF . . . CAN BE A PARENT OF . . . xsl:copy xsl:apply-imports xsl:element xsl:apply-templates xsl:fallback xsl:call-templates xsl:for-each xsl:choose (continues) XSLT In-Depth 321 Table 13.20 Parent-Child Relationships (Continued) CAN BE A CHILD OF . . . CAN BE A PARENT OF . . . xsl:if xsl:copy xsl:message xsl:copy-of xsl:otherwise xsl:fallback xsl:param xsl:for-each xsl:template xsl:if xsl:variable xsl:message xsl:when xsl:number xsl:text xsl:value-of xsl:variable The start tag is replaced with <!— and the end tag is replaced with —>. Here is an example: <xsl:comment> This is my comment. </xsl:comment> xsl:copy Syntax The xsl:copy element allows you to copy the current node. It won’t copy the attrib- utes or child elements. <xsl:copy use-attribute-sets = “attribute_set_list”> </xsl:copy> The sole attribute, use-attribute-sets, is set to a whitespace-delimited list of attribute set names. These are merged and the attributes are set on the merged node. You call also use the attribute element to accomplish the same thing. Table 13.21 lists the parent-child relationships. 322 Chapter 13 Table 13.21 Parent-Child Relationships CAN BE A CHILD OF . . . CAN BE A PARENT OF . . . xsl:comment xsl:apply-imports xsl:copy xsl:apply-templates xsl:element xsl:attribute xsl:fallback xsl:call-template xsl:for-each xsl:choose xsl:if xsl:comment xsl:message xsl:copy xsl:otherwise xsl:copy-of xsl:param xsl:element xsl:processing-instruction xsl:fallback xsl:template xsl:for-each xsl:variable xsl:if xsl:when xsl:message xsl:number xsl:processing-instruction xsl:text xsl:value-of xsl:variable The following example uses the xsl:copy element to copy only those rows of the clerk employees: <?xml version=”1.0”?> <xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:template match=”page”> <xsl:element name=”ROWSET”> <xsl:apply-templates select=”ROWSET/ROW”/> </xsl:element> </xsl:template> XSLT In-Depth 323 <xsl:template match=”ROW”> <xsl:if test=”JOB=’CLERK’”> <xsl:copy> <xsl:for-each select=”@*”> <xsl:copy/> </xsl:for-each> <xsl:apply-templates select=”ENAME | SAL”/> </xsl:copy> </xsl:if> </xsl:template> <xsl:template match=”ENAME | SAL”> <xsl:copy> <xsl:value-of select=”.”/> </xsl:copy> </xsl:template> </xsl:stylesheet> xsl:copy-of Syntax The xsl:copy-of element allows you to copy the tree fragment specified by the select attribute: <xsl:copy-of select = “XPath_expression” /> The single attribute, select, is an expression that should evaluate to a node set. The members of the node set and their children will be copied. Table 13.22 lists the parent-child relationships. Table 13.22 Parent-Child Relationships CAN BE A CHILD OF . . . CAN BE A PARENT OF . . . xsl:attribute xsl:comment xsl:copy xsl:element xsl:fallback xsl:for-each xsl:if 324 Chapter 13 Table 13.22 (Continued) CAN BE A CHILD OF . . . CAN BE A PARENT OF . . . xsl:if xsl:message xsl:otherwise xsl:param xsl:processing-instruction xsl:template xsl:variable xsl:when Here is a simple example that creates a new XML document containing only those employees who are clerks: <?xml version=”1.0”?> <xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:template match=”page”> <xsl:apply-templates select=”ROWSET/ROW”/> </xsl:template> <xsl:template match=”ROW”> <xsl:if test=”JOB=’CLERK’”> <xsl:copy-of select=”.”/> </xsl:if> </xsl:template> </xsl:stylesheet> xsl:namespace-alias Syntax The xsl:namespace-alias allows you translate one namespace in the input to another namespace in the output: <xsl:namespace-alias stylesheet-prefix = “prefix” | “#default” result-prefix = “prefix” | “#default” /> Table 13.23 lists the attributes. XSLT In-Depth 325 Table 13.23 xsl:namespace-alias Attributes ATTRIBUTE DESCRIPTION stylesheet-prefix The prefix of the namespace that should be translated in the output. result-prefix The prefix that should replace the prefix described by the stylesheet-prefix attribute. If the string “#default” is used for either attribute, it would represent the default namespace. This is either no namespace declared by xmlns or no namespace at all. The xsl:namespace-alias element must be the child of the root element and may not have child elements. Your stylesheets can have multiple xsl:namespace-alias elements. However, it is an error to have multiple xsl:namespace-alias elements that have the same stylesheet-prefix, and it may produce conflicts to have multiple xsl: namespace-alias elements that have the same result-prefix elements. xsl:processing-instruction Syntax The xsl:processing-instruction allows you to specify an xml processing instruction. It can’t be used to specify the xml declaration; for that, you can use the xsl:output element. <xsl:processing-instruction name = “processing_instruction_name”> xslt template elements and other text & xml </xsl:processing-instruction> The name attribute specifies the name of the processing instruction. Table 13.24 lists the parent-child relationships. Table 13.24 Parent-Child Relationships CAN BE A CHILD OF . . . CAN BE A PARENT OF . . . xsl:copy xsl:apply-imports xsl:element xsl:apply-templates xsl:fallback xsl:call-template xsl:for-each xsl:choose 326 Chapter 13 Table 13.24 (Continued) CAN BE A CHILD OF . . . CAN BE A PARENT OF . . . xsl:if xsl:copy xsl:message xsl:copy-of xsl:otherwise xsl:fallback xsl:param xsl:for-each xsl:template xsl:if xsl:variable xsl:message xsl:when xsl:text xsl:value-of xsl:variable The value of the xsl:processing-instruction becomes the string value included in the processing instruction. Any template elements between the start and end tags will be evaluated. Here is an example in which you specify that the out- put uses a particular stylesheet: <xsl:processing-instruction name=”xml-stylesheet”> type=”text/xml” href=”someStylesheet.xsl” </xsl:processing-instruction> This xsl:processing-instruction would produce the following processing instruction in the output: <?xml-stylesheet type=”text/xml” href=”someStylesheet.xsl”?> An xsl:processing-instruction element must evaluate to a valid XML processing instruction. Numbering Elements There are two XSLT elements that concern numbering. The first, xsl:number, is used to provide a formatted number that details the position of a node in a node list. The second, xsl:decimal-format, is used in conjunction with the format-number function that will be covered in the “XPath” section. XSLT In-Depth 327 xsl:number The xsl:number element inserts a formatted number into the output. Typically, this element is used to provide numbering for a set of XML elements in the source document. <xsl:number level = “single” | “multiple” | “any” count = “pattern” from = “pattern” value = “number_expression” format = “format_mask” lang = “nmtoken” letter-value = { “alphabetic” | “traditional” } grouping-separator = “char” grouping-size = “number” /> The attributes of elements control how the numbering is done. By default, the num- ber element produces the next number in sequence each time the XSLT processor encounters the particular element. The attributes enhance this basic behavior. Table 13.25 lists the attributes. Table 13.25 xsl:number Attributes ATTRIBUTE DESCRIPTION level If set to single, only the nodes in a node set will be numbered; if set to multiple, the counting crosses node sets will be numbered. Any level is used for multiple layered counting. count A pattern that describes which patterns should be counted. from A pattern that specifies where counting should start. value If present, the expression will be evaluated and the result placed into the output as the number. By default, the value that the xsl:number element outputs is determined by context. More information on this below. format Specifies the format mask for the outputted number. lang Specifies the language to use in determining the sequence when alphabetic characters are used in the numbering. 328 Chapter 13 Table 13.25 (Continued) ATTRIBUTE DESCRIPTION letter-value Used to remove ambiguities, in many languages, of alphabetic lettering. By default, traditional is specified. This means that a format of I starts the roman numeral sequence I, II, III, and so on. Specifying alphabetic starts the sequence I, J, K, and so on. grouping-separator Specifies the character that separates digits. In English this is customarily a comma, but in some other languages this is a space. The default is a comma. grouping-size Specifies the size of groups separated by the grouping separator. By default, this is 3. Table 13.26 lists the parent-child relationships. Table 13.26 Parent-Child Relationships CAN BE A CHILD OF . . . CAN BE A PARENT OF . . . xsl:attribute xsl:comment xsl:copy xsl:element xsl:fallback xsl:for-each xsl:if xsl:message xsl:otherwise xsl:param xsl:processing-instruction xsl:template xsl:variable xsl:when XSLT In-Depth 329