xml - 使用 XSL 从 XML 属性/值组合中获取值
问题描述
首先,据我所知,这不仅仅是使用 XSL 进行 xml 转换。这是关于 xml 呈现给我的方式。我已尽我所能搜索,但找不到与我所拥有的场景相似的场景。
XML 源:
<?xml version="1.0"?>
<RepData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<requestId>2313022420.01</requestId>
<requestTimeStamp>2018-JUN-30T06:13:40Z</requestTimeStamp>
</Header>
<responseProcessing>
<translatorContext/>
<styleSheet/>
</responseProcessing>
<serviceResponse>
<Report docType="ACCOUNT">
<dataSet name="ACCOUNT_DETS">
<fieldDefinition index="1" id="ACCOUNT_ID" label="Account Number" type="ALPHANUMERIC" length="19" repeatable="FALSE"/>
<fieldDefinition index="2" id="ACCOUNT_NAME" label="Account Name" type="ALPHANUMERIC" length="35" repeatable="FALSE"/>
<fieldDefinition index="3" id="CURRENCY" label="Currency" type="ALPHANUMERIC" length="25" repeatable="FALSE"/>
<fieldDefinition index="4" id="AMOUNT" label="Account Balance" type="ALPHANUMERIC" length="19" repeatable="FALSE"/>
<record>
<field index="1">100004087</field>
<field index="2">MY EURO ACCOUNT</field>
<field index="3">Euro</field>
<field index="4">530,000</field>
</record>
<record>
<field index="1">200008169</field>
<field index="2">USD CASH ACCOUNT</field>
<field index="3">US Dollar</field>
<field index="4">2,924.82</field>
</record>
</dataSet>
</Report>
</serviceResponse>
</RepData>
从源中,字段是在fieldDefinition
元素下定义的。并且值在field
元素中。
我似乎无法找到一种方法来遍历字段以选择字段值。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="RepData/serviceResponse/Report/dataSet/record" >
<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<th>Account</th>
<th>Balance</th>
</tr>
<xsl:for-each select="RepData/serviceResponse/Report/dataSet/record/field">
<p>
<xsl:value-of select="RepData/serviceResponse/Report/dataSet/record/field" />
</p>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
上面的 XSL 只返回空白,但如果我将 ' mode="toc"
' 添加到匹配所有数据中,我会得到 xml 中所有值的转储:
2313022420.01 2018-JUN-30T06:13:40Z 100004087 MY EURO ACCOUNT Euro 530,000 200008169 USD CASH ACCOUNT US Dollar 2,924.82
这就是我想要的(字段名称来自 中的label
属性fieldDefinition
):
Account Number|Account Name |Currency |Account Balance
100004087 |MY EURO ACCOUNT |Euro |530,000
200008169 |USD CASH ACCOUNT |US Dollar |2,924.82
解决方案
模板匹配和内for-each
循环不正确。您可以进行以下更改以获取 HTML 表。
假设其中的field
元素record
数量与元素数量(在本例中为 4)相匹配fieldDefinition
,您可以使用以下模板。可以有多个选项来获取输出,这里我使用了多个for-each
循环。您还可以使用模板方法来获取输出。
<!-- Template match with document root element -->
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr bgcolor="#9acd32">
<!-- Loop to generate the header row of the table -->
<xsl:for-each select="RepData/serviceResponse/Report/dataSet/fieldDefinition">
<th><xsl:value-of select="@label" /></th>
</xsl:for-each>
</tr>
<!-- Loop for each "record" -->
<xsl:for-each select="RepData/serviceResponse/Report/dataSet/record">
<tr>
<!-- Loop for each column below the header -->
<xsl:for-each select="field">
<td><xsl:value-of select="." /></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
输出
推荐阅读
- c# - 如何让物体自行移动?
- r - 使用 Photon 的 R 闪亮独立应用程序
- python - 单击空白区域时禁用选择/背景颜色
- python - 如何在服务器中托管的 mongoDB 数据库和远程设备中托管的 SQLite 数据库之间建立备份系统?
- groovy - 如何在响应断言失败时将 JMeter 变量写入文件
- python - Pandas 与许多条件和不一致的数据合并
- python - cpanel中的Python烧瓶应用程序路由:只能访问root url
- laravel - FIELD和FIELD_DEFINITION中指令的区别和示例
- php - Rest api:根路径目录有问题
- c++ - 通过 CMake 设置 Ninja 值(交叉编译)