Main XSLT element 2

Source: Internet
Author: User
Tags xsl xslt

1. <XSL: namespace-alias> element

Definition and usage

The <XSL: namespace-alias> element is used to replace the namespaces in the style sheet with different namespaces in the output. In other words, replace the prefixes associated with the given namespace with other prefixes.

Note: <XSL: namespace-alias> is a top-level element and must be a child element of <XSL: stylesheet> or <XSL: Transform>.

Example: Enter the XML document

<Soapenv: Envelope

Xmlns: soapenv = "http://schemas.xmlsoap.org/soap/envelope"

Xmlns: q0 = "http: // tobacco/ECS/NAT/MED/receivebusinessreq"

>

<Soapenv: Body>

<Q0: receiverequest>

<Q0: comcode> comcode </q0: comcode>

<Q0: indcode> indcode </q0: indcode>

<Q0: requestnum> requestnum </q0: requestnum>

<Q0: Request> request-XML content </q0: Request>

</Q0: receiverequest>

</Soapenv: Body>

</Soapenv: envelope>

XSLT document for conversion:

<? XML version = "1.0" encoding = "UTF-8"?>

<XSL: stylesheetversion = "1.0"

Xmlns: q0 = "http: // tobacco/ECS/NAT/MED/receivebusinessreq"

Xmlns: Out = "http: // tobacco/ECS/COM/MED/transrequestco"

Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">

<XSL: namespace-alias stylesheet-Prefix = "q0" result-Prefix = "out"/>

<XSL: template match = "q0: receiverequest">

<Soapenv: Envelope

Xmlns: soapenv = "http://schemas.xmlsoap.org/soap/envelope/">

<Soapenv: Body>

<Out: transrequest>

<Q0: comcode> This is <XSL: value-of select = "q0: comcode"/> </q0: comcode>

<Q0: indcode> This is <XSL: value-of select = "q0: indcode"/> </q0: indcode>

<Q0: requestnum> This is <XSL: value-of select = "q0: requestnum"/> </q0: requestnum>

<Q0: requestxml> This is <XSL: value-of select = "q0: request"/> </q0: requestxml>

</Out: transrequest>

</Soapenv: Body>

</Soapenv: envelope>

</XSL: Template>

</XSL: stylesheet>

Output Conversion Result:

<Soapenv: envelope xmlns: soapenv = "http://schemas.xmlsoap.org/soap/envelope"

Xmlns: q0 = "http: // tobacco/ECS/NAT/MED/receivebusinessreq"

Xmlns: Out = "http: // tobacco/ECS/COM/MED/transrequestco">

<Soapenv: Body>

<Out: transrequest>

<Out: comcode> This is comcode </out: comcode>

<Out: indcode> This is indcode </out: indcode>

<Out: requestnum> This is requestnum </out: requestnum>

<Out: requestxml> This is request-XML content </out: requestxml>

</Out: transrequest>

</Soapenv: Body>

</Soapenv: envelope>

The results show that the namespace has changed;

The prefix from q0 to QNAME from out.

2. Blank processing strip-space and preserve-space: Processing blank content, not space processing !!!!

Blank space is reserved by default.

<XSL: preserve-space> elements are used to define elements that retain white space.

<XSL: strip-space> elements are used to define elements that delete white spaces.

<XSL: preserve-space elements = "list-of-element-names"/>
<XSL: strip-space elements = "list-of-element-names"/>

Note: elements is required. A list of elements separated by spaces, specifying that elements with blank spaces are retained/deleted.

Note: The list can contain "*" and "Prefix: *", so that you can add all elements or all elements from a specified namespace.

<XSL: stylesheetversion = "1.0"
Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<! -- Country company price year: if there is only space in the text, the blank space is deleted. -->
<XSL: strip-space elements = "country company price year"/>
<XSL: preserve-space elements = "title artist"/>
<XSL: template match = "/">
<HTML>
<Body>
<XSL: For-each select = "catalog/CD">
<P>
<XSL: value-of select = "title"/> <br/>
<XSL: value-of select = "artist"/> <br/>
<XSL: value-of select = "country"/> <br/>
<XSL: value-of select = "company"/> <br/>
<XSL: value-of select = "price"/> <br/>
<XSL: value-of select = "year"/>
</P>
</XSL: For-each>
</Body>
</Html>
</XSL: Template>
</XSL: stylesheet>

3. <XSL: Variable> Elements

<XSL: Variable> elements are used to declare local or global variables.

NOTE: If declared as a top-level element, the variable is global, and if declared in the template, the variable is local.

Note: once you set the variable value, it cannot be changed or modified!

Tip: You can use the content of the <XSL: Variable> element or the select attribute to add a value to the variable!

<XSL: Variable
Name = "name"
Select = "expression"> Use select to define the value of a variable.
<! -- Content: Template -->
</XSL: Variable>

Note:

If the select attribute is set, the <XSL: Variable> element cannot contain any content. If the select attribute contains a text string, quotation marks must be placed on the string.

<XSL: variable name = "color" select = "'red'"/>

<XSL: variable name = "Header">
<Tr>
<TH> element </Th>
<TH> description </Th>
</Tr>
</XSL: Variable>

The parent element can basically be any other output element, which is different from <XSL: param>, and its value cannot be modified once set.

4. Copy and copy-

1) The <XSL: Copy> element can create a copy of the current node (copy ).

Note: The namespace node of the current node will be automatically copied, but the subnodes and attributes of the current node will not be automatically copied!

<XSL: Copy use-Attribute-sets = "name-list">
<! -- Content: Template -->
</XSL: Copy>

Example: copy the message node to the output document:

<? XML version = "1.0" encoding = "ISO-8859-1"?>

<XSL: stylesheetversion = "1.0"

Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">

<XSL: template match = "message">

<XSL: Copy>

<XSL: Apply-templates/>

</XSL: Copy>

</XSL: Template>

</XSL: stylesheet>

2) The <XSL: copy-of> element can create a copy of the current node.

Note: The namespace nodes, subnodes, and attributes of the current node are automatically copied!

Tip: This element can be used to insert multiple copies of the same node into different output locations.

<XSL: copy-of select = "expression"/>

Example:

<XSL: variable name = "Header">

<Tr>

<TH> element </Th>

<TH> description </Th>

</Tr>

</XSL: Variable>

<XSL: template match = "/">

<HTML>

<Body>

<Table>

<XSL: copy-of select = "$ Header"/>

5. Import

<XSL: Import> a top-level element is used to add the content of a style sheet to another style sheet.

Note: The imported style has a lower priority than the exported style table.

Note: This element must be the first subnode of <XSL: stylesheet> or <XSL: Transform>.

Syntax: <XSL: Import href = "Uri"/>

6. <XSL: Include> Elements

Is a top-level element that includes the content of a style sheet in a style sheet to another style sheet.

Note: The included style sheet has the same priority as the included style sheet.

Note: This element must be a subnode of <XSL: stylesheet> or <XSL: Transform>.

The difference between include and inmport is that the former is imported and has a priority, while the latter is included and does not have a priority.

7. <XSL: Apply-imports> element

The <XSL: Apply-imports> element can apply template rules from the imported style sheet.

The priority of the template rules in the imported style sheet is lower than that in the main style sheet. If you want to use a template rule in the imported style sheet instead of an equivalent rule in the main style sheet, the <XSL: Apply-imports> element is used.

8. The <XSL: output> element defines the format of the output document.

Note: <XSL: output> is a top-level element and must be a subnode of <XSL: stylesheet> or <XSL: Transform>.

<XSL: Output

Method = "XML | HTML | text | Name"

Version = "string"

Encoding = "string"

Omit-XML-declaration = "Yes | no"

Standalone = "Yes | no"

Doctype-Public = "string"

Doctype-system = "string"

CDATA-section-elements = "namelist"

Indent = "Yes | no"

Media-type = "string"/>

9. The <XSL: Text> element is used to write text to the output, that is, a text node is generated through the style sheet.

Tip: This element can contain text, object reference, and # pcdata.

<XSL: text = "Yes | no">

<! -- Content: # pcdata -->

</XSL: Text>

Disable-output-escaping: Optional. The default value is "no ". If the value is "yes", text nodes generated by instantiating the <XSL: Text> element will not be escaped during output. For example, if it is set to "yes", "<" will not be converted. If it is set to "no", it is output as "& lt ;". Netscape 6 does not support this attribute.

<XSL: template match = "/">
<HTML>
<Body>
<H2> my CD collection </H2>
<P> titles:
<XSL: For-each select = "catalog/CD">
<XSL: value-of select = "title"/>
<XSL: If test = "position () <last ()-1">
<XSL: Text>, </XSL: Text>
</XSL: If>
<XSL: If test = "position () = last ()-1">
<XSL: Text>, and </XSL: Text>
</XSL: If>
<XSL: If test = "position () = last ()">
<XSL: Text>! </XSL: Text>
</XSL: If>
</XSL: For-each>
</P>
</Body>
</Html>
</XSL: Template>

10. <XSL: Key> element

<XSL: Key> an element is a top-level element. It can declare a named key (that is, the name and Value Pair allocated to the element specified in the XML document ).

This key is used in the style sheet through the key () function to help you effectively access allocated elements in complex XML documents ..

Note: keys do not have to be unique!

Syntax

<XSL: Key
Name = "name" key name
Match = "pattern" matched element
Use = "expression" key value

/>

When processing An XSLT style sheet for the first time, keys are stored internally to simplify access. The key can simplify access to nodes in the XML document, but it may not be faster than retrieving the same node using XPath.

<? XML version = '1. 0'?>
<XSL: stylesheetversion = "1.0"
Xmlns: XSL = "http://www.w3.org/1999/XSL/Transform">
<XSL: key name = "title-search" match = "book" use = "@ Author"/>
<XSL: template match = "/">
<HTML>
<Body>
<XSL: For-each select = "Key ('title-search', 'David perry')">
<Div>
<XSL: value-of select = "@ title"/>
</Div>
</XSL: For-each>
</Body>
</Html>
</XSL: Template>
</XSL: stylesheet>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.