Examples of simultaneous use of XML, schema, and XSLT

Source: Internet
Author: User
Tags xmlns xpath xsl xslt

I've never used a schema before, this time I wanted to add namespace to my XML file, schema, and then transform it into HTML with XSLT, but I didn't think it would take much effort. Now I have a good record of the results of the work, I hope to help some people.

Let's take a look at my XML file first. The menu that you intend to use to define the page.

<?xml version="1.0" encoding="GB2312"?>
<menu_items>
<menu_item href="index.html" image="images/A1.gif" name="首页"/>
<menu_item href="ep.html" image="images/A2.gif" name="新闻">
    <menu_item href="ep.html" image="images/A2.gif" name="国内新闻"/>
</menu_item>
</menu_items>

Very simple XML file, put aside detours, with XMLSpy Generate schema function automatically generate schema files, and then make minor changes, the results are as follows:

<?xml version= "1.0" encoding= "GB2312"
<xs:schema xmlns= "http://www.hz-sp.com/2005/" Xmlschema-menu "xmlns:xs=" Http://www.w3.org/2001/XMLSchema "targetnamespace=" http://www.hz-sp.com/2005/   Xmlschema-menu ""
<xs:element name= "Menu_item"
<xs:complextype>
<xs:sequence>
<xs:element ref= "Menu_item" minoccurs= "0"/>
</xs:sequence>
<xs:attribute name= "name" Type= "XS : string "use=" required/>
<xs:attribute name= "href" type= "Xs:anyuri" use= "optional"/>
<xs: Attribute name= "image" Type= "Xs:anyuri" use= "optional"/>
</xs:complextype>
</xs:element>
<xs:element name= "Menu_items"
<xs:complextype>
<xs:sequence>
<xs:element ref= " Menu_item "maxoccurs=" unbounded "/>
</xs:sequence>
</xs:complextype>
</xs:element>
</xs:schema>

Among them, Http://www.hz-sp.com/2005/XMLSchema-menu is my namespace. This XSD is then specified in XML with the XMLSpy assign Schema feature, and the root node in the XML Menu_items is:

<menu_items xmlns="http://www.hz-sp.com/2005/XMLSchema-menu" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hz-sp.com/2005/XMLSchema-menu
menu.xsd">

The next step is to create the XSLT file, which also gives the correct results:

<?xml version= "1.0" encoding= "GB2312"
<xsl:stylesheet xpath-default-namespace= "http://" Www.hz-sp.com/2005/XMLSchema-menu "version=" 2.0 "xmlns:xsl=" http://www.w3.org/1999/XSL/Transform "xmlns:xs=" http ://www.w3.org/2001/XMLSchema "xmlns:fn=" http://www.w3.org/2004/07/xpath-functions "xmlns:xdt=" http://www.w3.org/ 2004/07/xpath-datatypes "xmlns=" http://www.w3.org/1999/xhtml >>
<xsl:output encoding= "GB2312" indent= "Yes" method= "HTML" version= "4.0"/>
<xsl:template match= "Mm:menu_items" xmlns:mm= "http://www.hz-sp.com/"   2005/xmlschema-menu ""
<table width= "900" border= "0" cellspacing= "0" cellpadding= "0"
<tr>
<xsl:for-each select= "Mm:menu_item"
<a href= "{@href}"

</a>
</xsl:for-each>
</tr>
</table>
</xsl:templ Ate>
</xsl:stylesheet>

It is annoying that xpath-default-namespace= "http://www.hz-sp.com/2005/XMLSchema-menu" has no effect on the match of Xsl:template, and it is estimated that match is not Xpath. But this attribute has no effect on the select in For-each, which is odd, and I don't think I understand how this property should be used.

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.