First of all, to the best of my knowledge, this is not about just xml transformation using XSL. It's about the way the xml is being presented to me. I've searched all I can but I can't find a scenario similar to the one I have.
首先,就我所知,这不仅仅是使用XSL进行xml转换。它是关于xml呈现给我的方式。我已经搜索了所有我能找到的,但是我找不到一个类似的场景。
XML source:
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>
From the source, the fields are defined under the fieldDefinition
element. And the values are in the field
element.
从源文件中,字段在fieldDefinition元素下定义。值在field元素中。
I can't seem to get a way to iterate through fields to pick the field values.
我似乎没有办法遍历字段来选择字段值。
<?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>
The XSL above only returns blank but if I add 'mode="toc"
' to the match all data I get a dump of all values in the xml:
上面的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
This is what I'm going for (the field names are from the label
attribute in fieldDefinition
):
这就是我要找的(字段名来自fieldDefinition中的label属性):
Account Number|Account Name |Currency |Account Balance
100004087 |MY EURO ACCOUNT |Euro |530,000
200008169 |USD CASH ACCOUNT |US Dollar |2,924.82
2 个解决方案
#1
1
<xsl:template match="RepData">
<xsl:for-each select="serviceResponse/Report/dataSet">
<table>
<thead>
<tr bgcolor="green">
<xsl:for-each select="fieldDefinition">
<th>
<xsl:value-of select="@label"/>
</th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:for-each select="record">
<tr bgcolor="blue">
<xsl:for-each select="field">
<td style="color:white;">
<xsl:value-of select="."/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:for-each>
</xsl:template>
#2
3
The template match and the inner for-each
loop are incorrect. You can make the following changes to get the HTML table.
模板匹配和内部for-each循环不正确。您可以进行以下更改以获取HTML表。
Assuming the number of field
elements within record
matches with the number of fieldDefinition
elements (4 in this case), you can use the below template. There can be multiple options to get the output and here I have used multiple for-each
loops. You can also use a template approach to get the output.
假设记录匹配的字段元素数量与fieldDefinition元素的数量(在本例中为4),您可以使用以下模板。可以有多个选项来获得输出,这里我使用了多个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>
Output
输出
#1
1
<xsl:template match="RepData">
<xsl:for-each select="serviceResponse/Report/dataSet">
<table>
<thead>
<tr bgcolor="green">
<xsl:for-each select="fieldDefinition">
<th>
<xsl:value-of select="@label"/>
</th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:for-each select="record">
<tr bgcolor="blue">
<xsl:for-each select="field">
<td style="color:white;">
<xsl:value-of select="."/>
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</xsl:for-each>
</xsl:template>
#2
3
The template match and the inner for-each
loop are incorrect. You can make the following changes to get the HTML table.
模板匹配和内部for-each循环不正确。您可以进行以下更改以获取HTML表。
Assuming the number of field
elements within record
matches with the number of fieldDefinition
elements (4 in this case), you can use the below template. There can be multiple options to get the output and here I have used multiple for-each
loops. You can also use a template approach to get the output.
假设记录匹配的字段元素数量与fieldDefinition元素的数量(在本例中为4),您可以使用以下模板。可以有多个选项来获得输出,这里我使用了多个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>
Output
输出