This issue learns the XSL style method and can be used to filter the range of elements in the select attributes of XSL elements <xsl:for-each>, <xsl:value-of>, <xsl:template>, match attributes of <xsl:apply-templates>, <xsl:if>, and <xsl:when>, to provide greater flexibility.
XML is like DHTML (dynamic HTML). These nodes are objects, and these objects are all hierarchical, forming a tree-like structure with clear hierarchy from the root node, which forms a document object model DOM (Document Object Model), which uses the object properties and methods to achieve the purpose of accessing and controlling XML nodes.
We do not intend to elaborate on the DOM of XML here, because this can be written into a tutorial with a lot of space. Let's first discuss some common methods in order to have a general understanding of the object methods of DOM.
Note: Starting from this issue, all examples no longer provide complete source code. If you have any questions, please read the previous seven issues carefully and practice.
1. end()
Meaning: Returns the last element in the collection.
Example: Output the last resume
Assume that the XML file format is:
……<resume>…</resume>……<resume>…</resume>……
The corresponding XSL file content is:
<xsl:for-each select="resume[end()]">……</xsl:for-each>
or:
<xsl:templates match="resume[end()]">……</xsl:templates>
or:
<xsl:apply-template select="resume[end()]">……</xsl:apply-template>
2. index()
Meaning: Returns the position of the element in the set, the return value is an integer, where the first element returns 0.
Example: Return to the previous three resumes.
resume[index()$le$3]
Note: index() is related to the parent element, please see the following example:
<x>
<y/>
<y/>
</x>
<x>
<y/>
<y/>
</x>
Returns the first <y> of all <x>
x/y[index()=0] or x/y[0]
3. nodeName()
Meaning: Returns the name of the element, that is, the tag name.
Example: Select any element, if its name (i.e., tag name) is equal to "name":
*[nodeName()='name'] or *[name]
4. number()
Meaning: Convert the value to a numeric form, return empty if it is not a numeric value, and require parameters.
Example: resume of a person under 30 years old:
resume[number(age)$lt$30] or resume[age$lt$30]
5. nodeType()
Meaning: Returns the node type, and the result is a numeric value. Here is a list of return values:
Node type | Node type value | Character form description of nodes |
Element | 1 | 'element' |
Element Attribute | 2 | 'attribute' |
Markup-Delimited Region of Text | 3 | 'text' |
Processing Instruction | 7 | 'processing_instruction' |
Comment | 8 | 'comment' |
Document Entity | 9 | 'document' |
6. value()
Meaning: Returns the value of an element or attribute.
Example: value() is the default method for an element or attribute, and the following representation is equivalent:
name!value()="NAME" and name="NAME"
@attr="attribute_value" and @attr="attribute_value"
Note: @ is the attribute prefix, @attr means it is the attribute attr
7. Attribute()
Meaning: Returns a collection of all attribute nodes, equivalent to "@*".
Example: Looking for all resume elements, satisfying the condition that at least one attribute has the value of "ABC":
resume[$any$attribute()='ABC'] or resume[$any$@*='ABC']
Looking for all resume elements, satisfying the condition that at least one child element has one attribute with the value of "ABC":
resume[$any$*/attribute()='ABC'] or resume[$any$*/@*='ABC']
8. Comment()
Meaning: Returns all comment nodes.
Example:
resume[$any$comment()='Yu Xichu's resume']
Indicates looking for <resume> elements containing comment statements: <!--Yu Xichu's resume-->.
9. cdata()
Meaning: Returns a collection of all CDATA types nodes.
Example:
resume[$any$cdata()='Yu Xichu's resume']
It means to find the <resume> element containing the following statement (must be a direct child node).
10. node()
Meaning: Returns a collection of all nodes except root nodes and attribute nodes in the current context environment, equivalent to:
"*|pi()|comment()|text()"
Example: Find all elements resume, whose last node has the name "skill":
resume[node()[end()]!nodeName()='skill']
Find the first node of all resume elements: resume/node()[0].
11. textnode()
Meaning: Returns a collection of nodes of all text types.
Example: Find the second text node of each p element:
p/textnode(1) or p!textnode(1)
12. text()
Meaning: Returns a collection of all nodes representing text strings, equivalent to "cdata()|textnode()".
This is the introduction to the content of this issue. Another function date() error occurred on my machine and caused the browser to automatically close. There is also a function pi() that I have not found the appropriate application method, so I will not introduce it. The next issue will tell you how to use scripts in XSL.