首页 > 解决方案 > 使用 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

标签: xmlxslt

解决方案


模板匹配和内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>

输出

HTML 表格


推荐阅读