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

如何用XSLT提取<Section name="Cechy">内容并转换为表格式<description>

解决方案

假设你的输入XML结构类似这样(如果实际结构不同,可根据说明调整):

<Root>
  <Section name="Cechy">
    <Attribute name="Rozmiar" value="XL"/>
    <Attribute name="Kolor" value="Niebieski"/>
    <Attribute name="Materiał" value="Bawełna"/>
  </Section>
</Root>

以下是完成需求的XSLT代码:

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

  <!-- 匹配根节点,触发目标Section的处理 -->
  <xsl:template match="/">
    <xsl:apply-templates select="//Section[@name='Cechy']"/>
  </xsl:template>

  <!-- 生成包含HTML表格的description元素 -->
  <xsl:template match="Section[@name='Cechy']">
    <description>
      <table border="1">
        <thead>
          <tr>
            <th>Nazwa właściwości</th>
            <th>Wartość</th>
          </tr>
        </thead>
        <tbody>
          <!-- 遍历Section下的所有子节点,替换*为实际节点名(比如Property)如果需要 -->
          <xsl:for-each select="*">
            <tr>
              <td><xsl:value-of select="@name"/></td>
              <td><xsl:value-of select="@value"/></td>
            </tr>
          </xsl:for-each>
        </tbody>
      </table>
    </description>
  </xsl:template>

  <!-- 忽略不需要处理的节点和文本 -->
  <xsl:template match="text()|@*"/>
</xsl:stylesheet>

关键说明:

  • 用//Section[@name='Cechy']精准定位到你需要处理的Section节点
  • 表格的表头文字可根据需求自行修改
  • 如果Section下的子节点不是<Attribute>,把select="*"换成实际的节点名称(比如select="Property")
  • 如果子节点的属性名不是name和value,调整xsl:value-of的路径即可(比如子元素是<Name>和<Value>,就改成select="Name"和select="Value")

另外,感谢之前帮你构建代码的伙伴!

内容的提问来源于stack exchange,提问作者user20508390

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 23:37:45