How to use XSL and regular expressions to verify the validity of data (II.)

Source: Internet
Author: User
Tags filter expression regular expression xsl
The Data | is now continuing with the examples I have described in the previous series. We will implement a simple search mechanism,
Perhaps your interest is not in getting the title of the book but in getting the entire book node (note).
Then use the following function to meet your requirements.
Public Function getfilteredelements (activeelement as Variant, Regexpfilter as String, _
Optional querystring as String = "", Optional IsGlobal as Boolean=true, _
Optional IgnoreCase as Boolean = True) as IXMLDOMNodeList
Dim Re as RegExp
Dim Filterdoc as DOMDocument
Dim NodeList as IXMLDOMNodeList
Dim filterlist as IXMLDOMNodeList
Dim node as IXMLDOMElement
Dim Basenode as IXMLDOMElement
Set re = New RegExp
On Error GoTo ErrorHandler
Select case TypeName (activeelement)
Case "IXMLDOMElement"
Set Basenode = activeelement
Case "DOMDocument"
Set Basenode = activeelement.documentelement
Case Else
Error 1001
Set getfilteredelements = Nothing
End Select
Re. Pattern = Regexpfilter
Re. Global=isglobal
Re. Ignorecase=ignorecase
If querystring = "" Then
Set filterlist = Basenode.selectnodes (". [ Textnode ()]|. *[textnode ()] ")
Else
Set filterlist = Basenode.selectnodes (querystring)
End If
For each node in filterlist
If Re. Test (node. Text) Then
Node.setattribute "Filter:filteredelementfound", "true"
End If
Next
Set filterlist = Basenode.selectnodes (". [ @filter: filteredelementfound]|. *[@filter: Filteredelementfound] ")
For each node in filterlist
Node.removeattribute "Filter:filteredelementfound"
Next
Set getfilteredelements = filterlist
Exit Function
ErrorHandler:
If Err.Number = 1001 Then
MsgBox "Document must is an XML document, or a document element."
Else
Error Err.Number
End If
End Function

The following is a brief description of the method:
Getfilteredelements is primarily used to get an XML document or a node within a document, and
Convert it to our needs (including all of the node's child nodes) and place the converted node in a
List Type IXMLDOMNodeList.
This filter program is used in sequence for each node, and if the text of a node satisfies the expression,
Then the node is marked with a temporary attribute of Filter:filteredelementfound
(Of course, in order not to conflict with a flag that already exists in your own XML document, you can take this attribute
Change it to what you need).
Once all the nodes have been checked, a new list object (only those that satisfy the expression) is included
node) was created. These temporary properties are deleted and then returned to those nodes.

If you don't enter query parameters for this function,
Then you get all the leaf nodes of the entire document (that is, the nodes that don't have child elements)
If you enter query parameters for this function,
Then this function will return the node or child node that satisfies the condition
For example, the following code will return the title of the first book and the description of the node and the fourth book
Description node
Dim bookxml=new DOMDocument
Bookxml.load ("Bookcatalog.xml")
Set nodelist=getfilteredelements (bookxml, "xml")
And the following code returns the nodes of the first and fourth books, and then you can get them based on these nodes
The properties of the child node.
Set nodelist=getfilteredelements (bookxml, "xml", "//book")
In general, you should define an XSL query filter (filter) as much as possible
Because it will only return the nodes you need, it will greatly reduce the amount of data you need to process.

This function is an example of an application of the XML "database", because many SQL developers use the familiar
parameters, such as like, are not equivalent in XML, but as long as you are proficient in XML, you can use the
Regular expression, you will find that it can implement many functions like statements can implement.

Validating Data with XSL transformations
Regular expressions are used in transformations of XSL to be a powerful tool for validating data.
For example, you want to generate a table showing the title and description of a book based on the XML data
When you use the DOM version (the one that uses Microsoft's model Xdom), XSL has been able to implement very complex transformation XML to
HTML functionality.
<xsl:script&gt in this node of the XSL, allowing you to use the scripting language.
You can insert text into the output stream (but you are not able to output a DOM node to the output stream at this point).
The default scripting language in XSL is JavaScript,
In the process of using, you need to be aware that because "/" in JavaScript is a special character, you need to use
"//" Turn it.
The code is as follows:
<xsl:stylesheet xmlns:xsl= "Http://www.w3.org/TR/WD-xsl" >
<xsl:script language= "JavaScript" ><! [CDATA] [
isvalidbooktopic=/xml/
]]></xsl:script>
<xsl:template match= "/" >
<xsl:apply-templates select= "//book"/>
</xsl:template>
<xsl:template match= "book" >
<xsl:if expr= "Isvalidbooktopic.test (this.text)" >
<p><xsl:value-of select= "description"/></p>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Of course, you can also change the query parameters in your XSL (for example, the parameter used in the example above is XML)
A direct method is to generate a parameter entity "entities"
For example, the following code, you need to use the "%" character to indicate that a character is a parameter.
Function Setxslparameter (Xsldoc as DOMDocument, paramname as _
String,paramvalue as Variant) as DOMDocument
Dim Xsldoc as DOMDocument
Dim Scriptnode as IXMLDOMElement
Dim Re as RegExp

Set re=new REGEXP
Re.global=true
Re.ignorecase=true
re.pattern= "%" +paramname
For each scriptnode in Xsldoc.selectnodes ("Xsl:script")
Scriptnode.text=re.replace (Scriptnode.text,cstr (paramvalue))
Next
Return Xsldoc
End Function

Setxslparameter is used to set parameters for an XSL document.
Of course, you can not use the above function, directly in the XSL script



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.