You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XSLT技术实现:基于不同元素的相同属性值填充表格

用XSLT基于XML相同属性值生成关联表格的解决方案

我明白你现在卡在怎么用XSLT基于XML里的相同属性值来生成关联表格了,咱们一步步来解决这个问题。首先我先补全一个贴近你需求的完整XML示例(因为你提供的XML内容被截断了,假设每个GPSSetup下有通过setupRef属性关联的观测点数据):

<?xml version="1.0" encoding="UTF-8"?>
<LandXML>
  <Survey>
    <GPSSetup id="Setup1" stationName="Ref1">
      <LinkedPoint setupRef="Setup1" pointId="P1" elevation="100.5"/>
      <LinkedPoint setupRef="Setup1" pointId="P2" elevation="101.2"/>
    </GPSSetup>
    <GPSSetup id="Setup2" stationName="Ref2">
      <LinkedPoint setupRef="Setup2" pointId="P3" elevation="99.8"/>
    </GPSSetup>
    <GPSSetup id="Setup3" stationName="Ref3">
      <LinkedPoint setupRef="Setup3" pointId="P4" elevation="102.1"/>
      <LinkedPoint setupRef="Setup3" pointId="P5" elevation="100.9"/>
    </GPSSetup>
  </Survey>
</LandXML>

接下来是实现需求的XSLT代码,核心是用<xsl:key>来关联具有相同属性值的元素,然后循环遍历生成表格:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes"/>

  <!-- 定义key:用setupRef属性值关联LinkedPoint元素 -->
  <xsl:key name="points-by-setup" match="LinkedPoint" use="@setupRef"/>

  <xsl:template match="/">
    <html>
      <body>
        <h2>GPS Setup 关联观测点表格</h2>
        <table border="1">
          <tr>
            <th>Setup ID</th>
            <th>站点名称</th>
            <th>观测点ID</th>
            <th>海拔高度</th>
          </tr>
          <!-- 遍历所有GPSSetup元素 -->
          <xsl:for-each select="LandXML/Survey/GPSSetup">
            <xsl:variable name="current-setup-id" select="@id"/>
            <!-- 通过key获取当前Setup关联的所有LinkedPoint -->
            <xsl:for-each select="key('points-by-setup', $current-setup-id)">
              <tr>
                <!-- 只在第一个关联点显示Setup信息,避免重复 -->
                <xsl:if test="position() = 1">
                  <td rowspan="{count(key('points-by-setup', $current-setup-id))}">
                    <xsl:value-of select="$current-setup-id"/>
                  </td>
                  <td rowspan="{count(key('points-by-setup', $current-setup-id))}">
                    <xsl:value-of select="../@stationName"/>
                  </td>
                </xsl:if>
                <td><xsl:value-of select="@pointId"/></td>
                <td><xsl:value-of select="@elevation"/></td>
              </tr>
            </xsl:for-each>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

关键步骤解释:

  • 定义<xsl:key>:这是关联相同属性元素的核心,它把所有LinkedPoint元素按照setupRef属性值分组,方便后续快速查找。
  • 遍历GPSSetup:先循环每个GPS站点,保存当前站点的id到变量current-setup-id。
  • 关联对应元素:用key('points-by-setup', $current-setup-id)获取当前站点关联的所有观测点。
  • 生成表格布局:使用rowspan让站点信息跨多行显示,避免重复,同时遍历每个观测点填充对应的列数据。

如果你的XML结构和我假设的不同(比如关联属性不是setupRef,或者元素名称不一样),只需要调整xsl:key的match和use属性,以及遍历的元素路径即可。

内容的提问来源于stack exchange,提问作者Arturs.E

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:17:17