如何使用 XSLT 将特定类的未知 XML 元素格式化为 HTML?
所以我有一个由我的应用程序生成的 XML 文档,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE AddressBook>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>
<AddressBook>
<Item>
<UserGeneratedElementName1 class="info">Whatever blah blah</UserGeneratedElementName1>
<UserGeneratedElementName2 class="info">Whatever blah blah</UserGeneratedElementName2>
</Item>
<Item>
<UserGeneratedElementName3 class="info">Whatever blah blah</UserGeneratedElementName3>
</Item>
...
...
Other Items with user-generated elements with user-generated content...
</AddressBook>
我想将其转换为类似于此的 HTML 文档:
<html>
<head>
<title>AddressBook</title>
</head>
<body>
<div class="root">
<div class="item">
<b>UserGeneratedElementName1:</b> Whatever blah blah
<b>UserGeneratedElementName2:</b> Whatever blah blah
</div>
<div class="item">
<b>UserGeneratedElementName3:</b> Whatever blah blah
</div>
...
...
Other transformed items...
</div>
</body>
</html>
我试图掌握 XSLT 语法,但所有指南都太模糊,无法帮助我跟这个还是太深了。另外,XSLT 语法似乎很混乱。 提前致谢。
So I have a XML document generated by my application like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE AddressBook>
<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>
<AddressBook>
<Item>
<UserGeneratedElementName1 class="info">Whatever blah blah</UserGeneratedElementName1>
<UserGeneratedElementName2 class="info">Whatever blah blah</UserGeneratedElementName2>
</Item>
<Item>
<UserGeneratedElementName3 class="info">Whatever blah blah</UserGeneratedElementName3>
</Item>
...
...
Other Items with user-generated elements with user-generated content...
</AddressBook>
And I want to turn it into a HTML document similar to this:
<html>
<head>
<title>AddressBook</title>
</head>
<body>
<div class="root">
<div class="item">
<b>UserGeneratedElementName1:</b> Whatever blah blah
<b>UserGeneratedElementName2:</b> Whatever blah blah
</div>
<div class="item">
<b>UserGeneratedElementName3:</b> Whatever blah blah
</div>
...
...
Other transformed items...
</div>
</body>
</html>
I have tried to get a grasp of the XSLT syntax, but all the guides were either too vague to help me with this or too deep. Also XSLT syntax seems quite confusing.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里看看这个问题
是否有 XSLT name-of 元素?
您可以使用
或
来获取节点的名称,具体取决于您是要包含完整的前缀名称还是仅包含本地部分。
您应该能够使用 xsl:for-each 块将它们组合在一起,以迭代前 3 个级别的项目并生成您要查找的 HTML。
像这样的东西适用于固定数量的级别。
更通用的方法看起来更像是:
Take a look at this question here
Is there an XSLT name-of element?
You can use
or
to get the name of a node, depending on if you want to include the full prefixed name, or just the local portion.
You should be able to piece those together with xsl:for-each blocks to iterate through the first 3 levels of items and generate the HTML you're looking for.
Something like this would work for a fixed number of levels.
A more generic approach would look something more like:
此完整的 XSLT 转换:
应用于提供的 XML 文档时:
产生所需的正确结果:
说明:
模板匹配任何
Item
元素以及Item
元素的任何子元素。使用标准 XPath
name()
函数。This complete XSLT transformation:
when applied on the provided XML document:
produces the wanted, correct result:
Explanation:
Templates matching any
Item
element and any element child of anItem
element.Use of the standard XPath
name()
function.