XSLT:将分组 html 元素移动到节级别
我正在尝试编写一个 XSLT,根据标头级别将 HTML 文件组织为不同的部分级别。这是我的输入:
<html>
<head>
<title></title>
</head>
<body>
<h1>HEADER 1 CONTENT</h1>
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
<h2>Header 2 CONTENT</h2>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
</body>
</html>
我目前正在使用一个相当简单的结构,因此这种模式暂时保持不变。我需要这样的输出...
<document>
<section level="1">
<header1>Header 1 CONTENT</header1>
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
<section level="2">
<header2>Header 2 CONTENT</header2>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
</section>
</section>
</document>
我一直在使用这个示例: Stackoverflow 答案
但是,我无法让它完全按照我的需要进行操作。
我正在使用 Saxon 9 在 Oxygen for dev 中运行 xslt。我将在生产中使用 cmd/bat 文件。仍然是 Saxon 9。如果可能的话,我想处理最多 4 个嵌套节级别。
非常感谢任何帮助!
我需要附加到此,因为我遇到了另一个规定。我可能早就该想到这一点了。
我遇到以下代码示例
<html>
<head>
<title></title>
</head>
<body>
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
<h1>Header 2 CONTENT</h1>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
</body>
</html>
如您所见,
是
的子级,而在我的第一个代码段
< ;p>
始终是标头级别的子级。我想要的结果与上面相同,只是当我遇到
作为 的子级时,它应该包裹在
<部分级别=“1”>
。
<document>
<section level="1">
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
</section>
<section level="1">
<header1>Header 2 CONTENT</header1>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
</section>
</document>
I'm trying to write an XSLT that organizes an HTML file into different section levels depending on the header level. Here is my input:
<html>
<head>
<title></title>
</head>
<body>
<h1>HEADER 1 CONTENT</h1>
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
<h2>Header 2 CONTENT</h2>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
</body>
</html>
I'm working with a fairly simple structure at the moment so this pattern will be constant for the time-being. I need an output like this...
<document>
<section level="1">
<header1>Header 1 CONTENT</header1>
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
<section level="2">
<header2>Header 2 CONTENT</header2>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
</section>
</section>
</document>
I had been working with this example: Stackoverflow Answer
However, I cannot get it to do exactly what I need.
I'm using Saxon 9 to run the xslt within Oxygen for dev. I'll be using a cmd/bat file in production. Still Saxon 9. I'd like to handle up to 4 nested section levels if possible.
Any help is much appreciated!
I need to append onto this as I've encountered another stipulation. I probably should have thought of this before.
I'm encountering the following code sample
<html>
<head>
<title></title>
</head>
<body>
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
<h1>Header 2 CONTENT</h1>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
</body>
</html>
As you can see, the <p>
is a child of <body>
while in my first snippet, <p>
was always a child of a header level. My desired result is the same as above except that when I encounter <p>
as a child of <body>
, it should be wrapped in <section level="1">
.
<document>
<section level="1">
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
<p>Level 1 para</p>
</section>
<section level="1">
<header1>Header 2 CONTENT</header1>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
<p>Level 2 para</p>
</section>
</document>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个 XSLT 2.0 样式表:
它应该执行您所要求的操作,尽管它不会停止在四个嵌套级别,而是只要找到
h[n]
元素就进行分组。Here is an XSLT 2.0 stylesheet:
It should do what you asked for, although it does not stop at four nested levels but rather groups as long as it finds
h[n]
elements.XSLT 1.0 解决方案(基本上是 Jenni Tennison 借用的):
当此转换应用于以下 XML 文档时:
生成所需的结果:
An XSLT 1.0 solution (essentially borrowed by Jenni Tennison):
when this transformation is applied on the following XML document:
the wanted result is produced:
XSLT 1.0 输出中更通用的分组
:
以及更复杂的输入示例,例如:
输出:
A more general grouping in XSLT 1.0
Output:
And with a more complex input sample like:
Output:
我能够为上面的附录找到一些有用的东西。我在正文模板中添加了逻辑来测试标头标签。它可能不适用于所有情况,但它对于我的任务来说效果很好。
I was able to get something working for my addendum above. I added logic into the body template to test for header tags. It may not work for every situation, but it is doing well for my task.