gpt4 book ai didi

xml - 慕尼黑? XSLT 去规范化/透视/展平 xml 文件?

转载 作者:数据小太阳 更新时间:2023-10-29 02:59:52 37 4
gpt4 key购买 nike

给定一个具有以下结构的输入 xml 文件:

<root>
<record row="1" col="1" val="1" />
<record row="1" col="2" val="2" />
<record row="1" col="3" val="3" />
<record row="1" col="n" val="4" />
<record row="2" col="1" val="5" />
<record row="2" col="3" val="6" />
<record row="2" col="n" val="7" />
<record row="n" col="2" val="8" />
<record row="n" col="3" val="9" />
<record row="n" col="n" val="10" />
</root>

如何使用 XSLT 输出以下结构?

<root>
<row id="1">
<col id="1">1</col>
<col id="2">2</col>
<col id="3">3</col>
<col id="n">4</col>
</row>
<row id="2">
<col id="1">5</col>
<col id="2"></col>
<col id="3">6</col>
<col id="n">7</col>
</row>
<row id="n">
<col id="1"></col>
<col id="2">8</col>
<col id="3">9</col>
<col id="n">10</col>
</row>
</root>

[注意即使输入中没有相关元素,所有列也是如何输出的]

编辑:我可能在示例中使用数字和字母造成了混淆。我正在寻找的解决方案需要处理非数字的行和列属性。

最佳答案

这个问题的答案显示了解决问题的可能方法:

xslt: How could I use xslt to create a table with multiple columns and rows?


编辑:下面是一个结合了链接问题中看到的技术的解决方案。

我假设:

  • 你的 @row@col属性是定义记录在表中位置的递增数字,它们不能真正包含字符串 "n" .因此它们在整个文档中不是唯一的,这使得它们不适合作为 HTML @id属性。我用 @title 代替了它们我的输出中的属性。
  • 没有隐含的空行(@row 连续性中的间隙不会产生空行),只有隐含的空单元格。
  • 每个 @row@col组合是独一无二的。

这个 XSLT 1.0 转换:

<xsl:stylesheet 
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>

<!-- prepare some keys for later use -->
<xsl:key name="kRecordsByRow" match="record" use="@row" />
<xsl:key name="kRecordsByPos" match="record" use="concat(@row, ',', @col)" />

<!-- find out the highest @col number -->
<xsl:variable name="vMaxCol">
<xsl:for-each select="/root/record">
<xsl:sort select="@col" data-type="number" order="descending" />
<xsl:if test="position() = 1">
<xsl:value-of select="@col" />
</xsl:if>
</xsl:for-each>
</xsl:variable>

<!-- select the <record>s that are the first in their rows -->
<xsl:variable name="vRows" select="
/root/record[
generate-id()
=
generate-id(key('kRecordsByRow', @row)[1])
]
" />

<!-- output basic table structure -->
<xsl:template match="/root">
<table>
<xsl:for-each select="$vRows">
<xsl:sort select="@row" data-type="number" />
<tr title="{@row}">
<xsl:call-template name="td" />
</tr>
</xsl:for-each>
</table>
</xsl:template>

<!-- output the right number of <td>s in each row, empty or not -->
<xsl:template name="td">
<xsl:param name="col" select="1" />

<td title="{$col}">
<xsl:value-of select="key('kRecordsByPos', concat(@row, ',', $col))/@val" />
</td>

<xsl:if test="$col &lt; $vMaxCol">
<xsl:call-template name="td">
<xsl:with-param name="col" select="$col + 1" />
</xsl:call-template>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

…当应用到这个(稍微修改过的)输入时:

<root>
<record row="1" col="1" val="1" />
<record row="1" col="2" val="2" />
<record row="1" col="3" val="3" />
<record row="1" col="4" val="4" />
<record row="2" col="1" val="5" />
<record row="2" col="3" val="6" />
<record row="2" col="4" val="7" />
<record row="3" col="2" val="8" />
<record row="3" col="3" val="9" />
<record row="3" col="4" val="10" />
</root>

...产生:

<table>
<tr title="1">
<td title="1">1</td>
<td title="2">2</td>
<td title="3">3</td>
<td title="4">4</td>
</tr>
<tr title="2">
<td title="1">5</td>
<td title="2"></td>
<td title="3">6</td>
<td title="4">7</td>
</tr>
<tr title="3">
<td title="1"></td>
<td title="2">8</td>
<td title="3">9</td>
<td title="4">10</td>
</tr>
</table>
  • Muenchian 分组用于选择第一个 <record>每个@row小组
  • 一个<xsl:key>用于通过其位置查明记录
  • 递归用于生成一组一致的 <td> s,独立于 <record> 的实际存在在指定位置

关于xml - 慕尼黑? XSLT 去规范化/透视/展平 xml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/771369/

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