所以我目前有一个文档,里面有一堆XML。如下面列出
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="datalog.xsl"?>
<ROOT>
<Vehicle>
<ADGCode>44095650</ADGCode>
<Brand>Mercedes Benz</Brand>
<Model>ML 63 AMG (375 kW)</Model>
<NewUsed>Used</NewUsed>
<Year>2007</Year>
<Mileage>166000</Mileage>
<Price>289990</Price>
<Colour>Black</Colour>
<StockNo/>
<Extras>Airbag - Driver, Pass & Sides
Air Conditioner
ABS
Central Locking Key
Radio/CD
Cruise Control
Electric Mirrors
Electric Windows - Front & Back
Heated Front Seats
Multi-function Steering Wheel
Navigation System
Power Steering
Sunroof - Electric
Xenon Headlights
</Extras>
</Vehicle>
它在XSL中翻译就像这样
<?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>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Image</th>
<th>ADG Code</th>
<th>Brand</th>
<th>Model</th>
<th>New / Used</th>
<th>Year</th>
<th>Mileage</th>
<th>Price</th>
<th>Colour</th>
</tr>
<xsl:for-each select="ROOT/Vehicle">
<tr>
<td>
<img width="200px">
<xsl:attribute name="src">
<xsl:value-of select="Images/Image"/>
</xsl:attribute>
</img>
</td>
<td><xsl:value-of select="ADGCode"/></td>
<td><xsl:value-of select="Brand"/></td>
<td><xsl:value-of select="Model"/></td>
<td><xsl:value-of select="NewUsed"/></td>
<td><xsl:value-of select="Year"/></td>
<td><xsl:value-of select="Mileage"/></td>
<td><xsl:value-of select="Price"/></td>
<td><xsl:value-of select="Colour"/></td>
</tr>
<tr>
<td colspan="9"><xsl:value-of select="Extras"/></td>
</tr>
<br />
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
但我想知道的是,我如何修改XML文档,以便从供应商允许我从其数据库中显示二手车的网站上托管的实时XML表中读取链接?
我不确定它是否简单,而不是放在XML文档的链接上,还是我必须做其他事情?我无法在网络上找到其他任何答案。
非常感谢您的帮助和指导。
答案 0 :(得分:0)
您可以使用xi:include
来引用必须包含在XML输入中的外部xml内容:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="datalog.xsl"?>
<ROOT>
<!-- external xml content -->
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
href="http://url.of.your/external/xml/content" parse="xml"/>
<!-- your local xml content -->
<Vehicle>
<ADGCode>44095650</ADGCode>
<Brand>Mercedes Benz</Brand>
...
</Vehicle>
</ROOT>
请注意,要使此解决方案有效:
&
实体,网址的查询字符串中的&
字符必须正确escaped,才能使XML格式正确-xi
命令行选项)例如,如果外部xml具有与本地xml相同的结构(ROOT
文档元素与Vehicle
子元素),则组合的xml将如下所示:
<ROOT>
<!-- external xml content -->
<ROOT>
<Vehicle>
<ADGCode>65897159</ADGCode>
...
</Vehicle>
</ROOT>
<!-- your local xml content -->
<Vehicle>
<ADGCode>44095650</ADGCode>
<Brand>Mercedes Benz</Brand>
...
</Vehicle>
</ROOT>
所以你的xsl:for-each
循环应该是:
<xsl:for-each select="//Vehicle">
...
</xsl:for-each>
以便在任何深度匹配Vehicle
元素。
修改强>
评论澄清了问题是关于如何将本地XSLT应用于远程XML (我最初误解了输入是本地XML +远程XML)。
因此,此问题与How to apply XSLT on XML with just XInclude非常相似,the answer to a different question的回答是Sean B. Durkin同一用户{{3}}。
使用这种方法,您的输入文件只是:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="transform.xsl"?>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude"
href="http://www.carfind.co.za/xmlfeeds/pickup.aspx?feedid=352&templatetype=DealershipWebsite&accesskey=9A6A5A7C"
parse="xml"/>
并且需要稍微修改XSLT以匹配xi:include
元素:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xi="http://www.w3.org/2001/XInclude"
exclude-result-prefixes="xi">
<xsl:output method="html"/>
<xsl:template match="xi:include[@href][@parse='xml' or not(@parse)]">
<xsl:apply-templates select="document(@href)" />
</xsl:template>
<xsl:template match="ROOT">
<html>
...
<xsl:for-each select="Vehicle">
...
</xsl:for-each>
...
</html>
</xsl:template>
</xsl:stylesheet>
现在,您可以在浏览器中打开最小的XML文件,结果应该是预期的(它可能取决于浏览器;在我的Mac上,它适用于Safari但不适用于Firefox,可能是因为安全设置)。