XSLT-从具有相同属性名称的元素创建属性值数组

时间:2019-12-12 13:19:02

标签: xml xslt

我有一个XML文档,例如:

[<Envelope xmlns="http://www.gesmes.org/xml/2002-08-01">
    <Cube xmlns="http://www.bankofengland.co.uk/boeapps/iadb/agg_series" SCODE="RPMB3GLOTHE" DESC="Monthly amounts outstanding of other-owned banks' sterling sight deposits from monetary financial institutions (in sterling millions) not seasonally adjusted" COUNTRY="" CONCAT="Amounts outstanding # Other-owned banks # Sterling # Deposits - sight # Monetary financial institutions # Sterling # Not seasonally adjusted ">
        <Cube FREQ_NAME="Monthly" FREQ_CODE="M" FREQ_ORD="7"/>
        <Cube SERIES_DEF="banking list" DEF_LOC="http://www.bankofengland.co.uk/statistics/ms/current/Bklist.pdf"/>
        <Cube SERIES_DEF="revisions policy" DEF_LOC="http://www.bankofengland.co.uk/statistics/pages/iadb/notesiadb/Revisions.aspx"/>
        <Cube CAT_NAME="SEASONAL ADJUSTMENT" CAT_VAL="U" VAL_DESC="Not seasonally adjusted" CAT_ORD="1"/>
        <Cube CAT_NAME="TYPE" CAT_VAL="L" VAL_DESC="Amounts outstanding" CAT_ORD="2"/>
        <Cube CAT_NAME="OUTPUT IN" CAT_VAL="£" VAL_DESC="Sterling" CAT_ORD="3"/>
        <Cube CAT_NAME="SECTOR" CAT_VAL="OTHE" VAL_DESC="Other-owned banks" CAT_ORD="4"/>
        <Cube CAT_NAME="OF SECTOR" CAT_VAL="MF" VAL_DESC="Monetary financial institutions" CAT_ORD="5"/>
        <Cube CAT_NAME="INSTRUMENT CURRENCY" CAT_VAL="£" VAL_DESC="Sterling" CAT_ORD="6"/>
        <Cube CAT_NAME="INSTRUMENTS" CAT_VAL="LDPS" VAL_DESC="Deposits - sight" CAT_ORD="7"/>
        <Cube FIRST_OBS="2010-01-31" LAST_OBS="2019-10-31"/>
        <Cube TIME="2010-01-31" OBS_VALUE="131" OBS_CONF="N" LAST_UPDATED="2010-05-04 09:30:00">...</Cube>
        <Cube TIME="2010-02-28" OBS_VALUE="144" OBS_CONF="N" LAST_UPDATED="2010-05-04 09:30:00">...</Cube>
        <Cube TIME="2010-03-31" OBS_VALUE="152" OBS_CONF="N" LAST_UPDATED="2010-05-04 09:30:00"> </Cube>
        <Cube TIME="2010-04-30" OBS_VALUE="159" OBS_CONF="N" LAST_UPDATED="2010-07-29 09:30:00">
            <Revision UPDATE_TIMESTAMP="2010-06-02 09:30:00" OBS_VALUE="145"/>
            <Revision UPDATE_TIMESTAMP="2010-07-29 09:30:00" OBS_VALUE="159"/>
        </Cube>
</Envelope>][1]

我想用1行创建一个表,其中包含每个元素的所有属性值。我的问题是某些元素具有完全相同的属性,例如“ CAT_NAME”。我想创建一个数组或一个字典来将这些值存储在表的1个单元格中。

例如上述示例:

我有一个列:“ CAT_NAME”,值将是“ SEASONAL ADJUSTMENT,TYPE,OUTPUT IN ...等” 我不太在意格式,我只想在一行中输入。

我当前的XSLT模板:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:c="http://www.bankofengland.co.uk/boeapps/iadb/agg_series" xmlns:e="http://www.gesmes.org/xml/2002-08-01">
    <xsl:template match="/">
        <table>
            <thead>
                <tr>
                    <th>SCODE</th>
                    <th>DESC</th>
                    <th>COUNTRY</th>
                    <th>CONCAT</th>
                    <th>FREQ_NAME</th>
                    <th>FREQ_CODE</th>
                    <th>FREQ_ORD</th>
                    <th>SERIES_DEF</th>
                    <th>DEF_LOC</th>
                    <th>CAT_NAME</th>
                    <th>CAT_VAL</th>
                    <th>VAL_DESC</th>
                    <th>CAT_ORD</th>
                    <th>FIRST_OBS</th>
                    <th>LAST_OBS</th>
                </tr>
            </thead>
            <tbody>
                <xsl:for-each select="e:Envelope/c:Cube/c:Cube[not(@TIME)]">
                    <tr>
                        <td><xsl:value-of select="../@SCODE" /></td>
                        <td><xsl:value-of select="../@DESC" /></td>
                        <td><xsl:value-of select="../@COUNTRY" /></td>
                        <td><xsl:value-of select="../@CONCAT" /></td>
                        <td><xsl:value-of select="@FREQ_NAME" /></td>
                        <td><xsl:value-of select="@FREQ_CODE" /></td>
                        <td><xsl:value-of select="@FREQ_ORD" /></td>
                        <td><xsl:value-of select="@SERIES_DEF" /></td>
                        <td><xsl:value-of select="@DEF_LOC" /></td>
                        <td><xsl:value-of select="@CAT_NAME" /></td>
                        <td><xsl:value-of select="@CAT_VA" /></td>
                        <td><xsl:value-of select="@VAL_DESC" /></td>
                        <td><xsl:value-of select="@CAT_ORD" /></td>
                        <td><xsl:value-of select="@FIRST_OBS" /></td>
                        <td><xsl:value-of select="@LAST_OBS" /></td>
                    </tr>
                </xsl:for-each>
            </xsl:for-each>
        </tbody>
    </table>
</xsl:template>
</xsl:stylesheet>

给出以下输出:

Output

如果有人能为我指出我所需要的逻辑的正确方向,那就太好了!

0 个答案:

没有答案