xslt 压平任何 xml 文件
以以下 XML 为例:
<?xml version="1.0" encoding="UTF-8"?>
<Message>
<MessageID>1</MessageID>
<MessageType>0</MessageType>
<UniqueRef>12</UniqueRef>
<CreatedBy>fooo.bar</CreatedBy>
<Product>
<Name>Food Mixer</Name>
<Origin>London</Origin>
<CreatedBy>foo.bar</CreatedBy>
</Product>
<ProductExtendedProperties>
<CreateDate>23/10/2010</CreateDate>
<CreatedBy>foo.bar</CreatedBy>
</ProductExtendedProperties>
<Items>
<Item>
<Title>Food Mixer</Title>
<CreatedBy>my.customer</CreatedBy>
</Item>
</Items>
是否可以创建一个通用的 xslt,因为它将接受向其抛出的任何元素(无论是否嵌套)并提供格式良好的 html。
像这样的事情:
<html>
<body>
<fieldset>
<legend>Message</legend>
<div>
<p>MessageID: 1</p>
<p>MessageType: 0</p>
<p>UniqueRef: 0</p>
<p>CreatedBy: foo.bar</p>
<div>
<p>Product:</p>
<ul>
<li>Name: Food Mixer</li>
<li>Origin: London</li>
<li>CreatedBy: foo.bar</li>
</ul>
</div>
<div>
<p>ProductExtendedProperties:</p>
<ul>
<li>CreateDate: 23/10/2010</li>
<li>CreatedBy: foo.bar</li>
</ul>
</div>
<div>
<p>
Items
</p>
<div>
<p>Item 1:</p>
<ul>
<li>Title: Food Mixer</li>
<li>CreatedBy: my.customer</li>
</ul>
</div>
</div>
</div>
</fieldset>
</body>
它不必那么复杂,只需将元素的所有名称和值格式化为嵌套列表元素即可。
我不知道从哪里开始使用 xslt
非常感谢任何帮助
Given the following XML as an example:
<?xml version="1.0" encoding="UTF-8"?>
<Message>
<MessageID>1</MessageID>
<MessageType>0</MessageType>
<UniqueRef>12</UniqueRef>
<CreatedBy>fooo.bar</CreatedBy>
<Product>
<Name>Food Mixer</Name>
<Origin>London</Origin>
<CreatedBy>foo.bar</CreatedBy>
</Product>
<ProductExtendedProperties>
<CreateDate>23/10/2010</CreateDate>
<CreatedBy>foo.bar</CreatedBy>
</ProductExtendedProperties>
<Items>
<Item>
<Title>Food Mixer</Title>
<CreatedBy>my.customer</CreatedBy>
</Item>
</Items>
Is it possible to create an xslt which is generic in that it will take whatever element is thrown at it, nested or not and provide nicely formatted html.
Something like this:
<html>
<body>
<fieldset>
<legend>Message</legend>
<div>
<p>MessageID: 1</p>
<p>MessageType: 0</p>
<p>UniqueRef: 0</p>
<p>CreatedBy: foo.bar</p>
<div>
<p>Product:</p>
<ul>
<li>Name: Food Mixer</li>
<li>Origin: London</li>
<li>CreatedBy: foo.bar</li>
</ul>
</div>
<div>
<p>ProductExtendedProperties:</p>
<ul>
<li>CreateDate: 23/10/2010</li>
<li>CreatedBy: foo.bar</li>
</ul>
</div>
<div>
<p>
Items
</p>
<div>
<p>Item 1:</p>
<ul>
<li>Title: Food Mixer</li>
<li>CreatedBy: my.customer</li>
</ul>
</div>
</div>
</div>
</fieldset>
</body>
it doesnt have to be as complex as that, just something which formats into nested list elements all names and values of elements really.
I have no clue where to start with the xslt
Any help much appreciated thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为,如果您确实开始,然后具体解释您在哪里需要帮助,从您尝试的解决方案到您需要的内容,人们将更能够帮助您。
例如,您可以从一个将模板应用于
"*"
(任何子元素)的模板开始,以及另一个匹配"*"
并使用local-name 的模板()
输出当前元素的名称(以及所需的标记)。I think people will be a lot more able to help you if you do get a start, and then explain specifically where you need help getting from your attempted solution to what you need.
E.g. you could start with a template that applies templates to
"*"
(any child element), and another template that matches"*"
and useslocal-name()
to output the name of the current element (along with desired markup).一种方式:
输出:
One way:
Output: