[英]How to process a namespaced XML document using XSLT?
I am trying to process an XML file with XSLT to produce a list or table or (eventually) an SQL INSERT command with some of the values. 我正在尝试使用XSLT处理XML文件以生成列表或表,或者最终生成带有某些值的SQL INSERT命令。 I have been using the example from w3schools http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog .
我一直在使用来自w3schools http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog的示例。
My XML is very simple and I need to extract just the name and the rate of the hotel: 我的XML非常简单,我只需要提取酒店的名称和价格即可:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<getHotelsResponse xmlns="http://hotel.booking.vbooking.com">
<getHotelsReturn>
<address>
<number>589-591</number>
<postcode>08014</postcode>
<region>Catalonia</region>
<street>Carrer Vermell</street>
<town>Barcelona</town>
</address>
<name>Downtown Hotel</name>
<rate>235.0</rate>
</getHotelsReturn>
</getHotelsResponse>
</soapenv:Body>
</soapenv:Envelope>
The best XSLT I could build up from the w3schools was this one: 我可以从w3schools建立的最好的XSLT是:
<?xml version="1.0" encoding="utf-8"?>
<!-- Edited by XMLSpyΠ-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/soapenv:Envelope/soapenv:Body/getHotelsResponse">
<html>
<body>
<h2>Hotels in Barcelona</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Hotel name</th>
<th>Rate ($/night)</th>
</tr>
<xsl:for-each select="getHotelsReturn">
<tr>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="rate"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Which should produce a similar result to what I had in the w3schools example, but it comes back as blank. 它将产生与w3schools示例中类似的结果,但返回空白。
Can anyone please explain this? 谁能解释一下? Thanks.
谢谢。
As pointed out by @keshlam, if elements in your input XML have namespaces, you must declare them in your XSLT stylesheet as well. 正如@keshlam指出的那样,如果输入XML中的元素具有名称空间,则还必须在XSLT样式表中声明它们。
Find more detail on namespaces in a previous answer of mine . 在我的以前的答案中找到有关名称空间的更多详细信息。
The gist of the information you find there is: as far as the XSLT processor is concerned, a getHotelsReturn
element is entirely different from a vb:getHotelsReturn
element. 您所找到的信息的依据是:就XSLT处理器而言,
getHotelsReturn
元素与vb:getHotelsReturn
元素完全不同。
Stylesheet 样式表
<?xml version="1.0" encoding="utf-8"?>
<!-- Edited by XMLSpy?-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:vb="http://hotel.booking.vbooking.com"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
exclude-result-prefixes="soapenv vb">
<xsl:template match="/soapenv:Envelope/soapenv:Body/vb:getHotelsResponse">
<html>
<body>
<h2>Hotels in Barcelona</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Hotel name</th>
<th>Rate ($/night)</th>
</tr>
<xsl:for-each select="vb:getHotelsReturn">
<tr>
<td><xsl:value-of select="vb:name"/></td>
<td><xsl:value-of select="vb:rate"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output 产量
<html>
<body>
<h2>Hotels in Barcelona</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Hotel name</th>
<th>Rate ($/night)</th>
</tr>
<tr>
<td>Downtown Hotel</td>
<td>235.0</td>
</tr>
</table>
</body>
</html>
Rendered HTML 呈现的HTML
You can use XPaths like this: 您可以像这样使用XPath:
/Product1/Product2/Product3[@ValidYN = 'Y' and @ProductType = 'ABC']
/Product1/Product2/Product3[@ValidYN = 'Y' and @ProductType = 'DEF']
/Product1/Product2/Product3[@ValidYN = 'Y' and @ProductType = 'GHI']
Check this other question How to parse XML using XSLT? 另一个问题请问如何使用XSLT解析XML?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.