我使用XSD从XML文件定义架构。
Customer.xsd:
用于描述客户详细信息的XML Schema文档
<xs:import namespace="http://Address" schemaLocation="file:///C:/Users/SaithKumar/Documents/Altova/XMLSpy2017/Examples/AddressDetails.xsd "/>
<xs:element name="Persons">
<xs:complexType>
<xs:sequence>
<xs:element name="Customer" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="nickname" type="xs:string"/>
<xs:element name="phone" type="xs:float"/>
<xs:element ref="add:Address"/>
<xs:element name="Shipping_info">
<xs:complexType>
<xs:sequence>
<xs:element name="tracking_no" type="xs:string"/>
<xs:element name="delivery_no" type="xs:string" minOccurs="0"/>
<xs:element name="type" type="xs:string"/>
<xs:element name="delivery_date" type="xs:string"/>
<xs:element ref="add:Address"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Payment">
<xs:complexType>
<xs:sequence>
<xs:element name="cardno" type="xs:string"/>
<xs:element name="cvv" type="xs:int"/>
<xs:element name="date" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" type="xs:int"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
从此文件导入上述XSD文件的地址架构 Address.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:add="http://Address" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" targetNamespace="http://Address" elementFormDefault="qualified" attributeFormDefault="unqualified" vc:minVersion="1.1">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:element name="Address">
<xs:complexType>
<xs:sequence>
<xs:element name="addr1" type="xs:string"/>
<xs:element name="addr2" type="xs:string" minOccurs="0"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="zipcode" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
此处显示XSL文件。
CustomersXSL
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match = "/">
<html>
<body>
<h3>Details of each Students. Xpath expression = "/Persons"</h3>
<h3>Details of each Students. Xpath expression = "/Persons/*"</h3>
<table border = "1">
<tr bgcolor = "#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Phone</th>
</tr>
<xsl:for-each select = "/Persons/*">
<tr>
<td><xsl:value-of select = "@id"/></td>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "nickname"/></td>
<td><xsl:value-of select = "phone"/></td>
</tr>
</xsl:for-each>
</table>
<h3>Details of each Students. Xpath expression = "/Persons/Customer"</h3>
<table border = "1">
<tr bgcolor = "#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Phone</th>
</tr>
<xsl:for-each select = "/Persons/Customer">
<tr>
<td><xsl:value-of select = "@id"/></td>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "nickname"/></td>
<td><xsl:value-of select = "phone"/></td>
</tr>
</xsl:for-each>
</table>
<h3>Details of each Students. Xpath expression = "//Customer"</h3>
<table border = "1">
<tr bgcolor = "#9acd32">
<th>Roll No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Nick Name</th>
<th>Phone</th>
</tr>
<xsl:for-each select = "//Customer">
<tr>
<td><xsl:value-of select = "@id"/></td>
<td><xsl:value-of select = "firstname"/></td>
<td><xsl:value-of select = "lastname"/></td>
<td><xsl:value-of select = "nickname"/></td>
<td><xsl:value-of select = "phone"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
最后显示所有此ID的XML文件。 CustomersXML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type = "text/xsl" href = "CustomersXSL2.xsl"?>
<Persons xmlns="http://customers" xmlns:add="http://Address" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://customers file:///C:/Users/SaithKumar/Documents/Altova/XMLSpy2017/Examples/CustomersXSD2.xsd">
<Customer id="561">
<firstname>Saith Kumar</firstname>
<lastname>Gundu</lastname>
<nickname>sgundu</nickname>
<phone>9805621459</phone>
<add:Address>
<add:addr1>9303 Kittansett Drive</add:addr1>
<add:city>Charlotte</add:city>
<add:state>NC</add:state>
<add:zipcode>28262</add:zipcode>
</add:Address>
<Shipping_info>
<tracking_no>800910872</tracking_no>
<type>UPS</type>
<delivery_date>02/10/2017</delivery_date>
<add:Address>
<add:addr1>9303 Kittansett Drive</add:addr1>
<add:city>Charlotte</add:city>
<add:state>NC</add:state>
<add:zipcode>28262</add:zipcode>
</add:Address>
</Shipping_info>
<Payment>
<cardno>4400556688997564</cardno>
<cvv>869</cvv>
<date>04/19</date>
</Payment>
</Customer>
<Customer id="562">
<firstname>Mano Vikas</firstname>
<lastname>Banavathu</lastname>
<nickname>mbanavat</nickname>
<phone>7596241235</phone>
<add:Address>
<add:addr1>9303 Kittansett Drive</add:addr1>
<add:city>Charlotte</add:city>
<add:state>NC</add:state>
<add:zipcode>28262</add:zipcode>
</add:Address>
<Shipping_info>
<tracking_no>800942689</tracking_no>
<type>FedEx</type>
<delivery_date>29/01/2017</delivery_date>
<add:Address>
<add:addr1>9306 Kittansett Drive</add:addr1>
<add:city>Charlotte</add:city>
<add:state>NC</add:state>
<add:zipcode>28262</add:zipcode>
</add:Address>
</Shipping_info>
<Payment>
<cardno>6897562456895625</cardno>
<cvv>568</cvv>
<date>04/59</date>
</Payment>
</Customer>
<Customer id="563">
<firstname>Himanshu</firstname>
<lastname>Agarwal</lastname>
<nickname>hagarw</nickname>
<phone>9804455010</phone>
<add:Address>
<add:addr1>9303 Kittansett Drive</add:addr1>
<add:city>Charlotte</add:city>
<add:state>NC</add:state>
<add:zipcode>28262</add:zipcode>
</add:Address>
<Shipping_info>
<tracking_no>800910845</tracking_no>
<type>USPS</type>
<delivery_date>5/25/2016</delivery_date>
<add:Address>
<add:addr1>9303 Kittansett Drive</add:addr1>
<add:city>Charlotte</add:city>
<add:state>NC</add:state>
<add:zipcode>28262</add:zipcode>
</add:Address>
</Shipping_info>
<Payment>
<cardno>8654896589638523</cardno>
<cvv>741</cvv>
<date>05/21</date>
</Payment>
</Customer>
上面显示的所有代码都是用Altova XMLSPY编写的,它验证了所有文件。然而,当我在浏览器中运行XML文件时,样式表中的表格正在显示,但只显示标题行。数据未显示。你能告诉我我做错了吗? 感谢。