A xslt sample
WANG Hailong
In the Chinese website, I saw a lot of examples of XSLT, but most of them were entry nature. An example of an XSLT is given below, indicating some of the typical usage of XSLT.
The XSLT1.0 specification defines the Document () function, providing the initial handling of multiple XML input files. We use this feature to achieve the new old XML file comparison.
For example, we have an XML format product list listing some of the Open Source software for XSLT processing.
Every time, we update a list of products.
The following product.1.xml is the first product list.
Product.1.xml
XML Version = "1.0"?>
product>
product>
product-root>
After a while, the product list is updated to product.2.xml. Among them, the product list previous product list --Product.1.xml.
Product.2.xml
XML Version = "1.0"?>
product>
product>
product>
product>
product-root>
We handle product.2.xml with the XSL file below to find new adds.
Product.diff.xsl
XML Version = "1.0"?>
DIFMERENCE>
xsl: template>
xsl: transform>
Although this XSL file is short, it includes a very important feature of XSLT and XPath's typical usage. Because the content of Product-History is Product.1.xml, Document (Product-History) gets the root element of the last product list product.1.xml.
Document (Product-History) // Product obtains all Product elements below the root element of Product.1.xml. We can also write document / product, which is more confirmed, specifying only the Product-root elements below the product.
Note that Product [NOT (Product-ID = History / Product-id)] indicates "Product-id and History Product-id, which is different"; Product [Product-ID! = History / Product-ID] "Product-id and History Product-ID have at least one different product".
The processed XML results are as follows.
XML Version = "1.0" encoding = "UTF-8"?>
product>
product>
DIFMERENCE>
About how to run this example or other XSLT example. First, you need an XSLT conversion tool. Where do I get XSLT conversion tools? See the product list product list Product.2.xml, including a good XSLT conversion tool. Access the URL inside. :-)