使用 XSLT/XSL 解析具有相同名称的子元素的 XML
我想知道是否有一种方法可以使用 XSLT 传输父元素及其所有具有相同元素名称的子元素。
例如,如果原始 xml 文件是这样的:
<parent>
<child>1</child>
<child>2</child>
<child>3</child>
</parent>
我尝试使用 xsl 解析它:
<xsl:for-each select="parent">
<print><xsl:value-of select="child"></print>
想要这样的东西:
<print>1</print>
<print>2</print>
<print>3</print>
但我得到这个:
<print>1</print>
因为 for-each 更适合这种格式:
<parent>
<child>1</child>
<parent>
</parent
<child>2</child>
<parent>
</parent
<child>3</child>
</parent
有没有办法获得想要的打印输出没有像上面那样格式化,而是第一种方式?
谢谢
I am wondering if there is a way to transfer over a parent element with all of its children elements that have the same element name using XSLT.
For example, if the original xml file is like this:
<parent>
<child>1</child>
<child>2</child>
<child>3</child>
</parent>
And I try to parse it with xsl using:
<xsl:for-each select="parent">
<print><xsl:value-of select="child"></print>
wanting something like this:
<print>1</print>
<print>2</print>
<print>3</print>
but I get this:
<print>1</print>
because the for-each is more designed for this format:
<parent>
<child>1</child>
<parent>
</parent
<child>2</child>
<parent>
</parent
<child>3</child>
</parent
Is there anyway to get the desired printout without formatting it like it is above, but rather the first way?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您正在父级而不是子级上执行
xsl:for-each
。如果将其更改为以下内容,您将获得所需的结果(假设当前上下文为/
):但是...通常不需要使用
xsl:for-each
。您应该让重写模板为您处理工作,而不是尝试从单个模板/上下文(如/
)获取所有子级,这是一个完整的样式表示例:
此样式表的输出将是:
It's because you're doing the
xsl:for-each
on the parent instead of the child. You would get the results you're looking for if you changed it to this (assuming the current context is/
):However... using
xsl:for-each
is usually not necessary. You should let overriding templates handle the work for you instead of trying to get to all of the children from a single template/context (like/
)Here's a complete stylesheet for an example:
the output of this stylesheet would be: