gpt4 book ai didi

xml - 如何仅使用 XSLT 提取 "for eached"元素的子元素

转载 作者:行者123 更新时间:2023-12-03 16:56:39 25 4
gpt4 key购买 nike

我想从定义如下的 XML 文件中显示一组表:

<reportStructure>
<table>
<headers>
<tableHeader>Header 1.1</tableHeader>
<tableHeader>Header 1.2</tableHeader>
</headers>
<tuples>
<tuple>
<tableCell>1.1.1</tableCell>
<tableCell>1.2.1</tableCell>
</tuple>
<tuple>
<tableCell>1.1.2</tableCell>
<tableCell>1.2.2</tableCell>
</tuple>
</tuples>
</table>
<table>
...

我正在使用 XSLT 和 XPath 来转换数据,但是 foreach 并没有按照我期望的方式工作:
      <xsl:template match="reportStructure">
<xsl:for-each select="table">
<table>
<tr>
<xsl:apply-templates select="/reportStructure/table/headers"/>
</tr>
<xsl:apply-templates select="/reportStructure/table/tuples/tuple"/>
</table>
</xsl:for-each>
</xsl:template>

<xsl:template match="headers">
<xsl:for-each select="tableHeader">
<th>
<xsl:value-of select="." />
</th>
</xsl:for-each>
</xsl:template

<xsl:template match="tuple">
<tr>
<xsl:for-each select="tableCell">
<td>
<xsl:value-of select="." />
</td>
</xsl:for-each>
</tr>
</xsl:template>

虽然我希望这会为每个表格标签输出一个表格,但它会为每个表格标签输出所有表格标题和单元格。

最佳答案

您正在选择 apply-templates 中的所有标题和元组.

仅选择相关的:

  <xsl:template match="reportStructure">
<xsl:for-each select="table">
<table>
<tr>
<xsl:apply-templates select="headers"/>
</tr>
<xsl:apply-templates select="tuples/tuple"/>
</table>
</xsl:for-each>
</xsl:template>

您实际上也应该简单地将上述内容作为一个 table模板,没有 xsl:for-each :
  <xsl:template match="table">
<table>
<tr>
<xsl:apply-templates select="headers"/>
</tr>
<xsl:apply-templates select="tuples/tuple"/>
</table>
</xsl:template>

关于xml - 如何仅使用 XSLT 提取 "for eached"元素的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4158357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com