使用 XSLT/XSL 解析具有相同名称的子元素的 XML

发布于 2024-11-18 17:06:55 字数 862 浏览 5 评论 0原文

我想知道是否有一种方法可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流星番茄 2024-11-25 17:06:55

这是因为您正在父级而不是子级上执行 xsl:for-each 。如果将其更改为以下内容,您将获得所需的结果(假设当前上下文为 /):

<xsl:for-each select="parent/child">
  <print><xsl:value-of select="."/></print>
</xsl:for-each>

但是...通常不需要使用xsl:for-each。您应该让重写模板为您处理工作,而不是尝试从单个模板/上下文(如 /)获取所有子级,

这是一个完整的样式表示例:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="parent">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="child">
      <print><xsl:apply-templates/></print>
  </xsl:template>

</xsl:stylesheet>

此样式表的输出将是:

<print>1</print>
<print>2</print>
<print>3</print>

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 /):

<xsl:for-each select="parent/child">
  <print><xsl:value-of select="."/></print>
</xsl:for-each>

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:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="parent">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="child">
      <print><xsl:apply-templates/></print>
  </xsl:template>

</xsl:stylesheet>

the output of this stylesheet would be:

<print>1</print>
<print>2</print>
<print>3</print>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文