This article will introduce you to an example of using XSLT to recursively retrieve the list of Primary category names. I hope this tutorial will be helpful to you.
This article is about a small example of XSLT and XML.
Requirement: XML throws primary and subcategories of classes. subcategories of different primary classes are repeated. Each primary and subcategory is output in the form of a record set. Filter out the primary category.
1. XML data:
| The Code is as follows: |
Copy code |
|
<? Xml version = "1.0" encoding = "UTF-8"?>
<Rows>
<Row>
<Area> Carrier </area>
<Complainttype> Damage </complainttype>
</Row>
<Row>
<Area> Carrier </area>
<Complainttype> Delivery problem </complainttype>
</Row>
<Row>
<Area> Carrier </area>
<Complainttype> Documentation </complainttype>
</Row>
<Row>
<Area> Carrier </area>
<Complainttype> Excess </complainttype>
</Row>
<Row>
<Area> Consumer & amp; Office </area>
<Complainttype> Brochures/catalogs </complainttype>
</Row>
<Row>
<Area> Consumer & amp; Office </area>
<Complainttype> Commission/rebate </complainttype>
</Row>
<Row>
<Area> Customer </area>
<Complainttype> Commission/rebate </complainttype>
</Row>
<Row>
<Area> Customer </area>
<Complainttype> Credit/Overdues </complainttype>
</Row>
<Row>
<Area> Customer </area>
<Complainttype> Delivery problem </complainttype>
</Row>
<Row>
<Area> Customer </area>
<Complainttype> Documentation </complainttype>
</Row>
<Row>
<Area> Customer </area>
<Complainttype> Duty exemption </complainttype>
</Row>
<Row>
<Area> Customer </area>
<Complainttype> Excess </complainttype>
</Row>
<Row>
<Area> Customer </area>
<Complainttype> Imco </complainttype>
</Row>
<Row>
<Area> Customer </area>
<Complainttype> Order routing </complainttype>
</Row>
<Row>
<Area> DC </area>
<Complainttype> Excess </complainttype>
</Row>
</Rows> |
2. XSLT code:
| The Code is as follows: |
Copy code |
<? Xml version = "1.0" encoding = "UTF-8"?>
<Xsl: stylesheet xmlns: xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0">
<Xsl: output method = "html" version = "1.0" omit-xml-declaration = "yes"/>
<Xsl: template match = "/">
<Xsl: variable name = "optionsStr">
<Xsl: for-each select = "descendant: Row/area"> <xsl: value-of select = "."/>###</xsl: for>
</Xsl: variable>
<! -- OptionStr output:
Carrier ### Consumer & amp; Office ### Consumer & amp; office ### Customer #### Customer ### Customer #### DC ###
-->
<P>
<Select name = "area">
<Option value = ""> -- Select -- </option>
<Xsl: call-template name = "areaOptions">
<Xsl: with-param name = "optionsStr" select = "$ optionsStr"/>
</Xsl: call>
</Select>
</P>
</Xsl: template>
<! -- Remove repeat area -->
<Xsl: template name = "areaOptions">
<Xsl: param name = "optionsStr"/>
<! -- Normalize-space () for remove n -->
<! -- Option: the first option to be intercepted -->
<Xsl: variable name = "option"> <xsl: value-of select = "normalize-space (substring-before ($ optionsStr ,'###')) "/> </xsl: variable>
<! -- NewStr: extract the remaining string after the first option -->
<Xsl: variable name = "newStr"> <xsl: value-of select = "normalize-space (substring-after ($ optionsStr ,'###')) "/> </xsl: variable>
<! -- NewOption: The second screenshot option -->
<Xsl: variable name = "newOption"> <xsl: value-of select = "normalize-space (substring-before ($ newStr ,'###')) "/> </xsl: variable>
<! -- The option is not repeated only when the first option and the second option are different. -->
<Xsl: if test = "$ newOption! = $ Option ">
<Option>
<Xsl: attribute name = "value"> <xsl: value-of select = "/descendant: Row [area = $ option]/area"/> </xsl: attribute>
<Xsl: value-of select = "$ option"/>
</Option>
</Xsl: if>
<! -- If optionsStr and option both have values, copy newStr to optionsStr for recursive loop -->
<Xsl: if test = "$ optionsStr! = ''And $ option! = ''">
<Xsl: call-template name = "areaOptions">
<Xsl: with-param name = "optionsStr"> <xsl: value-of select = "$ newStr"/> </xsl: with>
</Xsl: call>
</Xsl: if>
</Xsl: template>
</Xsl: stylesheet>
|
The main idea is to combine the main classification loop into a string, and then recursively extract two options for the string for comparison. If the two options are different, the options are not repeated, otherwise, recursive loops are performed. The recursion ends when the total string is truncated and no more options are available. For details, see the comments section in the code.
Final effect:
Latest answer:
2012.04.23: the xsl: key and key () methods in XSLT are recently found, and the filtering results can be simply implemented:
| The Code is as follows: |
Copy code |
|
<? Xml version = "1.0" encoding = "UTF-8"?>
<Xsl: stylesheet version = "1.0" xmlns: xsl = "http://www.w3.org/1999/XSL/Transform">
<Xsl: key name = "mainArea" match = "rows/row" use = "area"/>
<Xsl: template match = "/">
<Xsl: for-each select = "rows/row [count (. | key ('mainare', area) [1]) = 1]">
<Xsl: value-of select = "area"/> <br/>
</Xsl: for>
</Xsl: template>
</Xsl: stylesheet> |