Home > Backend Development > XML/RSS Tutorial > Detailed introduction to example code that uses xml, schema and xslt at the same time

Detailed introduction to example code that uses xml, schema and xslt at the same time

黄舟
Release: 2017-03-28 16:44:17
Original
2074 people have browsed it

I have never used schema before. This time I hope to add namespace and schema to my xml file, and then use xslt Unexpectedly, it took a lot of effort to convert xslt to html. Now I record the results of normal work, hoping to be of some help to everyone.

First Let's take a look at my xml file. It is intended to be used to define the menu of the web 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>
Copy after login

It is a very simple xml file. Put aside the detours, use the Generate Schema function of xmlspy to automatically generate the schema file, and then make some minor modifications. , the result is as follows:

<?xml version="1.0" encoding="GB2312"?>
<xs:schema xmlns="http://www.hz-sp.com/2005/XMLSchema-menu" xmlns:xs=" 
 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>
Copy after login

Among them, then use the Assign Schema function of xmlspy to specify this xsd in xml. The root node menu_items in xml 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">
Copy after login

Next, create the xslt file, the same Only the correct result is given:

<?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}">
    <img src="{@image}" width="113" height="57" border="0"/>
   </a>
   </xsl:for-each>
  </tr>
  </table>
</xsl:template>
</xsl:stylesheet>
Copy after login

What is annoying is that

xpath-default-namespace="http://www.hz-sp.com/2005/XMLSchema-menu"
Copy after login

has no effect on the match of xsl:template. It is estimated that the match is not xpath but this attribute##. #It also doesn't work for select in for-each, which is strange. I guess I haven't understood how to use this attribute.

The above is the detailed content of Detailed introduction to example code that uses xml, schema and xslt at the same time. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template