- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
给定一个具有以下结构的输入 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 < $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>
<record>
每个@row
小组<xsl:key>
用于通过其位置查明记录<td>
s,独立于 <record>
的实际存在在指定位置关于xml - 慕尼黑? XSLT 去规范化/透视/展平 xml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/771369/
您好,我有一个使用 JSON.Stringify 输出到此的对象 {"0":["test1","ttttt","","","","","","","",""],"1":["test2","ghjgjh
我有以下数据框,它是执行 groupby + 聚合总和的结果: df.groupby(['id', 'category']).agg([pd.Series.sum])
我有一个 3D 三角形带(见插图)。三角形不在一个平面内。 我想展平三角形带,使所有三角形都位于第一个三角形的平面内。 计划是围绕与第一个三角形的连接边旋转第二个三角形,使其与第一个三角形在同一平面内
简单地说,我正在寻找可在 iOS 上使用的与 NSBezierPath 的 -bezierPathByFlatteningPath 等效的方法。这对我来说是直接处理 CGPath 的函数还是 UIBe
假设我有以下 JToken: @"{ ""data"": [ { ""company"": { ""ID"": ""12
如果我在多个分支中处理单个功能,我会使用 git pull branch1 branch2 branch3 将所有更改 pull 入我的主分支。但是,每个分支的所有提交日志也会被复制。如何将提交日志扁
这个问题在这里已经有了答案: How do I make a flat list out of a list of lists? (33 个答案) 关闭6年前。 假设我们有一个返回列表(或有限迭代器)
给定如下模式: root |-- first_name: string |-- last_name: string |-- degrees: array | |-- element: struc
我有一个包含多个列的表,其中一些列是相同长度的数组。我想解除它们的嵌套,以获得包含来自不同行中的数组的值的结果。 所以有这样一张 table : 我想去: 这是其中一个数组列的工作方式: WITH d
我最近买了一台 RICOH THETA S,用于在 360 vr 中录制足球比赛。 我想使用 ffmpeg 将我用我的相机录制的鱼眼电影展平,这可能吗? enter image description
这是我的 question 的后续.是否可以将表格展平为如下所示,而不是数据透视表: data = {'year': ['2016', '2016', '2015', '2014', '2013'],
我目前正在将我的 jruby/java2d 图形绘制/布局应用程序移植到 macruby/cocoa。因此我需要获取开放的 NSBezierPath 与封闭的 NSBezierPath 的交点。 在
是否有一种简单的方法来展平一组 try 以给出尝试值的成功或失败? 例如: def map(l:List[Int]) = l map { case 4 => Failure(new Excepti
我有一个包含数百万行的“服务”表。每行对应于工作人员在给定日期和时间间隔内提供的服务(每行都有一个唯一的 ID)。在某些情况下,工作人员可能会在重叠的时间范围内提供服务。我需要编写一个查询来合并重叠的
我在使用Elastic Search(ES)检索JSON对象时遇到问题。现在,当我尝试使用下面的请求正文从ES查询一些数据时, "_source": [ "data.id", "dat
我有一个订单流(来源是订单列表)。每个订单都有一个 Customer 和一个 OrderLine 列表。 我想要实现的是拥有一个以客户为键的 map ,以及属于该客户的所有订单行,在一个简单的列表中作
给定一个如下所示的复杂对象: case class Complex ( id: Long, name: String, nested: Seq[Complex] ) 实际上,这可能会变成这
我很好奇你如何将数组 Promise 映射的结果展平。我有一个函数 Promise.maps 一组值,它们本身就是 promise (需要解析)并返回一个数组。所以,我得到类似的结果: [ [1, 2
我是 CouchDB 的新手,我只是想评估它在常见任务中的实用性。其中一项任务是生成报告。我的问题是:如果我有这样的文档结构: { "_id": "29763f342ab34fd7b579fd4
假设我们有这样的 map : %{"a": %{"b": 2, "c":5}, "d": 1} 有没有类似this function的东西(js回答同一问题)内置elixr? 最终结果应该是: %{"
我是一名优秀的程序员,十分优秀!