XSLT does not provide a ready-made split method to effectively separate the given query string. However, we can use the substring-before and substring-after functions to achieve the split effect. First look at an application requirement:
Content of the XML document to be formatted
<? XML version = "1.0" encoding = "UTF-8" ?>
< Items >
< Itema > A </ Itema >
< Itemb > B </ Itemb >
< Itemc > C </ Itemc >
< Itemd > D </ Itemd >
< Iteme > E </ Iteme >
</ Items >
XSLT document used to find node content
<? XML version = "1.0" encoding = "UTF-8" ?>
< XSL: stylesheet Version = "1.0" Xmlns: XSL = "Http://www.w3.org/1999/XSL/Transform"
Xmlns: msxsl = "Urn: Schemas-Microsoft-com: XSLT" Exclude-result-prefixes = "Msxsl"
>
< XSL: Output Method = "XML" Indent = "Yes" Omit-XML-Declaration = "Yes" />
<XSL: ParamName= "Tag"/>
< XSL: Template Match = "/" >
< XSL: value- Select = "// * [Name () = $ tag]" Disable-output-escaping = "Yes" />
</ XSL: Template >
</ XSL: stylesheet >
The node name to be searched is passed as the value of the parameter tag to the preceding XSLT document, which can output the content of the nodes in the XML document to the page, if the XML document node stores HTML markup content, the page displays hypertext markup with practical significance, this is determined by the disable-output-escaping = "yes" attribute. If the value of this attribute is false, the HTML Tag is output to the page as is.
The problem now is that I want the above XSLT document to output content from multiple matching XML nodes through the received parameters, for example, the received parameters are "itema, itemb, itemc ", the content of the three nodes is output in sequence on the page. How can this problem be achieved? The first thought was to implement a function similar to the split function in XSLT to break down the value of the parameter tag. It is best to store the decomposed results in an array, then, traverse the array and output the retrieved results in sequence. However, there is no split function in XSLT, and there is no "advanced" data structure like an array. XSLT itself is an XML document and can only implement some simple logic and operations. In fact, there are two functions in the XPath function of XSLT to meet our needs.
Substring-before (string1, string2) function: returns the substring of string2 in string1, such as substring-before ("itema, itemb, itemc ",",") the returned result is "itema ".
Substring-after (string1, string2) function: returns the substring after string2 appears in string1, such as substring-after ("itema, itemb, itemc ",",") the returned result is "itemb, itemc ".
With these two functions, we can implement the functions similar to the split function mentioned above. below is the modified XSLT documentCode:
<? XML version = "1.0" encoding = "UTF-8" ?>
< XSL: stylesheet Version = "1.0" Xmlns: XSL = "Http://www.w3.org/1999/XSL/Transform"
Xmlns: msxsl = "Urn: Schemas-Microsoft-com: XSLT" Exclude-result-prefixes = "Msxsl"
>
< XSL: Output Method = "XML" Indent = "Yes" Omit-XML-Declaration = "Yes" />
<XSL: ParamName= "Tag"/>
<XSL: TemplateMatch= "/">
< XSL: Call-template Name = "Output-tokens" >
< XSL: With-Param Name = "List" Select = "$ Tag" />
< XSL: With-Param Name = "Separator" > , </ XSL: With-Param >
</ XSL: Call-template >
</ XSL: Template >
< XSL: Template Name = "Output-tokens" >
< XSL: Param Name = "List" />
< XSL: Param Name = "Separator" />
< XSL: Variable Name = "Newlist" Select = "Concat (normalize-space ($ list), $ separator )" />
< XSL: Variable Name = "First" Select = "Substring-before ($ newlist, $ separator )" />
< XSL: Variable Name = "Remaining" Select = "Substring-after ($ newlist, $ separator )" />
<XSL: value-Select= "// * [Name () = $ first]"Disable-output-escaping= "Yes"/>
<BR/>
< XSL: If Test = "Substring-before ($ remaining, $ separator )! = ''" >
< XSL: Call-template Name = "Output-tokens" >
< XSL: With-Param Name = "List" Select = "$ Remaining" />
< XSL: With-Param Name = "Separator" Select = "$ Separator" />
</ XSL: Call-template >
</ XSL: If >
</ XSL: Template >
</XSL: stylesheet>
the output-tokens method is used to split strings and output query results in a loop, the basic idea is to constantly call the substring-before and substring-after functions to split the decomposed strings until the decomposition is completed.