XML 2 使用 XSLT 的 XML
我在 w3schools 学习了 XSLT 和 XPath,但这并不完全是我想要的。 只有有关转换 XSLT 2 HTML 的示例,这非常简单,但我需要 XML 2 XML 转换,并且找不到带有示例的好教程...我下载了 MSXSL.exe,但找不到有关使用它的示例转换 XML... 有人可以写一个样本吗? 我有像这样的customers.xml:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Customers table from Northwind database-->
<Customers>
<Customer CustomerID="ALFKI">
<CompanyName>Alfreds Futterkiste</CompanyName>
<Country>Germany</Country>
<Orders>
<Order OrderID="10643">
<ShipCity>Berlin</ShipCity>
</Order>
<Order OrderID="10692">
<ShipCity>Berlin</ShipCity>
</Order>
<Order OrderID="10702">
<ShipCity>Berlin</ShipCity>
</Order>
<Order OrderID="10835">
<ShipCity>Berlin</ShipCity>
</Order>
<Order OrderID="10952">
<ShipCity>Berlin</ShipCity>
</Order>
<Order OrderID="11011">
<ShipCity>Berlin</ShipCity>
</Order>
</Orders>
</Customer>
<Customer CustomerID="ANATR">
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
<Country>Mexico</Country>
<Orders>
<Order OrderID="10308">
<ShipCity>México D.F.</ShipCity>
</Order>
<Order OrderID="10625">
<ShipCity>México D.F.</ShipCity>
</Order>
<Order OrderID="10759">
<ShipCity>México D.F.</ShipCity>
</Order>
<Order OrderID="10926">
<ShipCity>México D.F.</ShipCity>
</Order>
</Orders>
</Customer>
</Customers>
并尝试生成另一个仅包含“国家/地区”节点的XML。 怎么写呢?
I learned about XSLT and XPath at w3schools,but it's not exactly what I wanted..
There are only examples about transforming XSLT 2 HTML, this is pretty easy,but I need a XML 2 XML transformation and can't find a good tutorial with examples...I downloaded MSXSL.exe but can't find exempls about using it to transform XML...
Can anyone write a sample?
I have customers.xml like :
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Customers table from Northwind database-->
<Customers>
<Customer CustomerID="ALFKI">
<CompanyName>Alfreds Futterkiste</CompanyName>
<Country>Germany</Country>
<Orders>
<Order OrderID="10643">
<ShipCity>Berlin</ShipCity>
</Order>
<Order OrderID="10692">
<ShipCity>Berlin</ShipCity>
</Order>
<Order OrderID="10702">
<ShipCity>Berlin</ShipCity>
</Order>
<Order OrderID="10835">
<ShipCity>Berlin</ShipCity>
</Order>
<Order OrderID="10952">
<ShipCity>Berlin</ShipCity>
</Order>
<Order OrderID="11011">
<ShipCity>Berlin</ShipCity>
</Order>
</Orders>
</Customer>
<Customer CustomerID="ANATR">
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
<Country>Mexico</Country>
<Orders>
<Order OrderID="10308">
<ShipCity>México D.F.</ShipCity>
</Order>
<Order OrderID="10625">
<ShipCity>México D.F.</ShipCity>
</Order>
<Order OrderID="10759">
<ShipCity>México D.F.</ShipCity>
</Order>
<Order OrderID="10926">
<ShipCity>México D.F.</ShipCity>
</Order>
</Orders>
</Customer>
</Customers>
And trying to generate another XML that contains only "country" nodes..
How to write it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
完全相同,您只需要相应地使用
output
元素的method
属性即可。It's exactly the same, you just need to use the
method
attribute of theoutput
element accordingly.像这样的事情应该有效......
这是结果......
Something about like this should work...
... Here is the result ...
这里有两种不同的解决方案:一种是简单的解决方案,另一种是仅生成独特国家/地区的更复杂的解决方案:
.1。获取所有
Country
元素:当此转换应用于提供的 XML 文档时,将生成所需的正确结果:
.2。 查找所有唯一的国家/地区:
当此转换应用于具有所提供文档结构的任何 XML 文档时,其中某些
Country
元素可能具有相同的值,只有一个生成每个不同值的Country
元素。使用提供的 XML 文档,我们仍然得到相同的结果:
Here are two different solutions: a simple one and a more complicated one that produces only the unique countries:
.1. Get all
Country
elements:when this transformation is applied on the provided XML document, the wanted, correct result is produced:
.2. Find all unique countries:
when this transformation is applied on any XML document with the structure of the provided document where some
Country
elements may have the same value, only oneCountry
element for each different value is produced.With the provided XML document we still get the same result:
它非常简单,您只需在 xslt 模板中编写 xml 而不是 html 即可。
以下内容(未经测试)应该可以帮助您入门:
另外,对于 XSLT 处理器,我衷心推荐 Visual Studio - 它有一个出色的 XSLT 调试器,它将帮助您无休止地理解样式表。 (我不确定此功能是否包含在 Express 版本中......)
Its pretty simple, you just need to write xml instead of html inside your xslt templates.
The following (untested) should get you started:
Also, for a XSLT processor I heartily recommend Visual Studio - it has an excellent XSLT debugger which will help you out no end in understanding your stylesheets. (I'm not sure if this functionality is included in the express editions...)
在拉式样式中,这应该是表达此转换的较短样式表:
输出:
In pull style, this should be the shorter stylesheet expressing this transformation:
Output: