修改 XSLT 以将 XML 转换为制表符分隔的文本文件

发布于 2024-10-16 05:55:12 字数 20011 浏览 1 评论 0 原文

我当前使用的系统允许我以 XML 格式导出选定的记录数据。该系统还提供了使用 XSLT 转换 XML 的选项。我喜欢使用它将 XML 转换为制表符分隔的文本,以便我可以将数据导入到另一个应用程序。

以下是导出了三个记录的 XML 示例。选择与每条记录一起导出的四个数据字段。

<?xml version="1.1" encoding="UTF-8"?>
<!DOCTYPE Export>
<ns:Export xmlns:ns="http://www.canto.com/ns/Export/1.0">
<ns:Layout tableName="AssetRecords">
    <ns:Fields>
        <ns:Field uid="{5bbb974b-d50a-45de-bb40-f6d7fb36bfd8}" type="2" valueInterpretation="5">
            <ns:Name>Date Taken / Scanned</ns:Name>
        </ns:Field>
        <ns:Field uid="{af4b2e43-5f6a-11d2-8f20-0000c0e166dc}" type="0" valueInterpretation="0">
            <ns:Name>Title</ns:Name>
        </ns:Field>
        <ns:Field uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}" type="2" valueInterpretation="0">
            <ns:Name>ID</ns:Name>
        </ns:Field>
        <ns:Field uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}" type="0" valueInterpretation="0">
            <ns:Name>Categories</ns:Name>
        </ns:Field>
    </ns:Fields>
</ns:Layout>
<ns:Items>
    <ns:Item catalogid="4" id="1">
        <ns:FieldValue uid="{5bbb974b-d50a-45de-bb40-f6d7fb36bfd8}" displayValue="Oct 10, 2010">2049029450</ns:FieldValue>
        <ns:FieldValue uid="{af4b2e43-5f6a-11d2-8f20-0000c0e166dc}">teapot</ns:FieldValue>
        <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">1</ns:FieldValue>
        <ns:FieldValue uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}">
            <ns:CategoryValue>Artifacts:Daily Life Item</ns:CategoryValue>
            <ns:CategoryValue>Images:Objects</ns:CategoryValue>
        </ns:FieldValue>
    </ns:Item>
    <ns:Item catalogid="4" id="2">
        <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">2</ns:FieldValue>
        <ns:FieldValue uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}">
            <ns:CategoryValue>Artifacts:Personal Artefact</ns:CategoryValue>
            <ns:CategoryValue>Artifacts:Daily Life Item</ns:CategoryValue>
            <ns:CategoryValue>Images:Objects</ns:CategoryValue>
        </ns:FieldValue>
    </ns:Item>
    <ns:Item catalogid="4" id="10">
        <ns:FieldValue uid="{af4b2e43-5f6a-11d2-8f20-0000c0e166dc}">Product Overview</ns:FieldValue>
        <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">10</ns:FieldValue>
        <ns:FieldValue uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}">
            <ns:CategoryValue>Documents:Paper Records</ns:CategoryValue>
        </ns:FieldValue>
    </ns:Item>
</ns:Items>
</ns:Export>

在 Excel 中打开时,我所需的制表符分隔输出文本如下表所示

https://lh5.googleusercontent.com/_Wsib_NwEZrA/TU6vzqbEGDI/AAAAAAAAAU0/V1SfGJyXJhQ/s800/2011-02-06_185321.png

我获得了一个 XSLT 文件来进行转换。然而,它只能处理简单、直接的 XML 结构。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns="http://www.canto.com/ns/Export/1.0"
    xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="text" indent="yes" encoding="utf-8" standalone="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <Root>
        <xsl:apply-templates select="/ns:Export/ns:Items/ns:Item"/>
    </Root>
</xsl:template>

<xsl:template match="ns:Item">
    <xsl:for-each select="ns:FieldValue">
        <xsl:value-of select="normalize-space(text())"/>
        <xsl:text>&#09;</xsl:text>
    </xsl:for-each>
    <xsl:text>
</xsl:text>
</xsl:template>

</xsl:stylesheet>

我希望有一个改进的版本,以便

  • 字段名称显示为列 日期标题
  • :使用显示值, 不是类别的数字
  • :所有类别 空数据显示在单独的列中
  • :显示空单元格

我的 XML 知识有限。希望有人可以帮忙。谢谢:-)

编辑于 2011 年 4 月 25 日

在我对一些新字段进行测试之前,提供的 XSLT 一直运行良好。下面是从系统导出的XML。它由两个记录组成。

<?xml version="1.1" encoding="UTF-8"?>
<!DOCTYPE Export>
<ns:Export xmlns:ns="http://www.canto.com/ns/Export/1.0">
<ns:Layout tableName="AssetRecords">
    <ns:Fields>
        <ns:Field uid="{00a3b951-4c7b-4751-90c0-c88cf0eb4983}" type="10" valueInterpretation="0">
            <ns:Name>Test Table Field</ns:Name>
            <ns:Layout tableName="AssetRecords/{00a3b951-4c7b-4751-90c0-c88cf0eb4983}">
                <ns:Fields>
                    <ns:Field uid="{86b3bf92-e7cc-4150-bd74-191ce4bf9374}" type="0" valueInterpretation="0">
                        <ns:Name>Accession Number</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}" type="2" valueInterpretation="0">
                        <ns:Name>Host Item ID</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}" type="2" valueInterpretation="0">
                        <ns:Name>ID</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{8322d5e0-edc9-4c2a-9991-702c376d9edc}" type="0" valueInterpretation="0">
                        <ns:Name>Description / Synopsis</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{5bbb974b-d50a-45de-bb40-f6d7fb36bfd8}" type="2" valueInterpretation="5">
                        <ns:Name>Date Taken / Scanned</ns:Name>
                    </ns:Field>
                </ns:Fields>
            </ns:Layout>
        </ns:Field>
        <ns:Field uid="{04a4080f-fec0-4e01-822f-b9125c0b5ac9}" type="10" valueInterpretation="0">
            <ns:Name>User Comment Thread</ns:Name>
            <ns:Layout tableName="AssetRecords/{04a4080f-fec0-4e01-822f-b9125c0b5ac9}">
                <ns:Fields>
                    <ns:Field uid="{dd48a259-8a55-46a6-b7cd-95a0335197e3}" type="5" valueInterpretation="0">
                        <ns:Name>User Comment Coordinates</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{03504c8d-c809-461c-a62e-55a4bd6c1adf}" type="10" valueInterpretation="0">
                        <ns:Name>User Comment</ns:Name>
                        <ns:Layout tableName="AssetRecords/{04a4080f-fec0-4e01-822f-b9125c0b5ac9}/{03504c8d-c809-461c-a62e-55a4bd6c1adf}">
                            <ns:Fields>
                                <ns:Field uid="{af4b2e46-5f6a-11d2-8f20-0000c0e166dc}" type="0" valueInterpretation="0">
                                    <ns:Name>Comments</ns:Name>
                                </ns:Field>
                                <ns:Field uid="{340f81ff-bef4-4967-b5eb-da1db00b73c6}" type="4" valueInterpretation="0">
                                    <ns:Name>Modification Date</ns:Name>
                                </ns:Field>
                                <ns:Field uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}" type="2" valueInterpretation="0">
                                    <ns:Name>Host Item ID</ns:Name>
                                </ns:Field>
                                <ns:Field uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}" type="2" valueInterpretation="0">
                                    <ns:Name>ID</ns:Name>
                                </ns:Field>
                                <ns:Field uid="{d332e5f9-980a-4685-bdd1-235635ac7f2f}" type="2" valueInterpretation="0">
                                    <ns:Name>Replied On</ns:Name>
                                </ns:Field>
                                <ns:Field uid="{da7fd65c-ea26-49f8-b7b8-a6871727d0bb}" type="0" valueInterpretation="10">
                                    <ns:Name>User</ns:Name>
                                </ns:Field>
                            </ns:Fields>
                        </ns:Layout>
                    </ns:Field>
                    <ns:Field uid="{228051ee-4983-462b-84e8-3644a14c0d52}" type="2" valueInterpretation="0">
                        <ns:Name>Page Number</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{d9bbc910-bae3-4862-97aa-b3ddb8bebb06}" type="4" valueInterpretation="0">
                        <ns:Name>Creation Date</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}" type="2" valueInterpretation="0">
                        <ns:Name>ID</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}" type="2" valueInterpretation="0">
                        <ns:Name>Host Item ID</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{da7fd65c-ea26-49f8-b7b8-a6871727d0bb}" type="0" valueInterpretation="10">
                        <ns:Name>User</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{cf84d248-60b6-4fe1-a8f1-a0d91c307b0a}" type="7" valueInterpretation="0">
                        <ns:Name>User Comment Type</ns:Name>
                    </ns:Field>
                </ns:Fields>
            </ns:Layout>
        </ns:Field>
        <ns:Field uid="{af4b2e00-5f6a-11d2-8f20-0000c0e166dc}" type="0" valueInterpretation="0">
            <ns:Name>Record Name</ns:Name>
        </ns:Field>
        <ns:Field uid="{6060d669-a2ff-4284-af2e-4c6762139ea1}" type="7" valueInterpretation="7">
            <ns:Name>Test multi-select string list</ns:Name>
        </ns:Field>
        <ns:Field uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}" type="0" valueInterpretation="0">
            <ns:Name>Categories</ns:Name>
        </ns:Field>
    </ns:Fields>
</ns:Layout>
<ns:Items>
    <ns:Item catalogid="3" id="1">
        <ns:FieldValue uid="{00a3b951-4c7b-4751-90c0-c88cf0eb4983}">
            <ns:Items>
                <ns:Item catalogid="3" id="1">
                    <ns:FieldValue uid="{86b3bf92-e7cc-4150-bd74-191ce4bf9374}">table access number test</ns:FieldValue>
                    <ns:FieldValue uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}">1</ns:FieldValue>
                    <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">1</ns:FieldValue>
                    <ns:FieldValue uid="{8322d5e0-edc9-4c2a-9991-702c376d9edc}">table decription test</ns:FieldValue>
                    <ns:FieldValue uid="{5bbb974b-d50a-45de-bb40-f6d7fb36bfd8}" displayValue="12-Jan-1967">2049007148</ns:FieldValue>
                </ns:Item>
                <ns:Item catalogid="3" id="12">
                    <ns:FieldValue uid="{86b3bf92-e7cc-4150-bd74-191ce4bf9374}">789-123</ns:FieldValue>
                    <ns:FieldValue uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}">1</ns:FieldValue>
                    <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">12</ns:FieldValue>
                    <ns:FieldValue uid="{8322d5e0-edc9-4c2a-9991-702c376d9edc}">data entered using Web Client in IE7</ns:FieldValue>
                    <ns:FieldValue uid="{5bbb974b-d50a-45de-bb40-f6d7fb36bfd8}" displayValue="31-Oct-1977">2049012575</ns:FieldValue>
                </ns:Item>
            </ns:Items>
        </ns:FieldValue>
        <ns:FieldValue uid="{04a4080f-fec0-4e01-822f-b9125c0b5ac9}">
            <ns:Items>
                <ns:Item catalogid="3" id="6">
                    <ns:FieldValue uid="{dd48a259-8a55-46a6-b7cd-95a0335197e3}">7265636f00000008000000084154505944626c653fc6d5ef2c783013000000084154505844626c653fb3d4bb7e327a97000000084142505844626c653fd33ae45b57bcb2000000084142505944626c653fcd12073615a24000000004455450584c6f6e670000002800000004455450594c6f6e670000012600000004454250584c6f6e67000001ee00000004454250594c6f6e6700000122</ns:FieldValue>
                    <ns:FieldValue uid="{03504c8d-c809-461c-a62e-55a4bd6c1adf}">
                        <ns:Items>
                            <ns:Item catalogid="3" id="6">
                                <ns:FieldValue uid="{af4b2e46-5f6a-11d2-8f20-0000c0e166dc}">Crop this part</ns:FieldValue>
                                <ns:FieldValue uid="{340f81ff-bef4-4967-b5eb-da1db00b73c6}" displayValue="2011-04-25T20:03:51+08:00">1303733031000</ns:FieldValue>
                                <ns:FieldValue uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}">6</ns:FieldValue>
                                <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">6</ns:FieldValue>
                                <ns:FieldValue uid="{da7fd65c-ea26-49f8-b7b8-a6871727d0bb}">U:{98ca1422-0171-4d1d-8b84-8cdda4ab70ae}:testsuperadmin</ns:FieldValue>
                            </ns:Item>
                        </ns:Items>
                    </ns:FieldValue>
                    <ns:FieldValue uid="{d9bbc910-bae3-4862-97aa-b3ddb8bebb06}" displayValue="2011-04-25T20:03:40+08:00">1303733020000</ns:FieldValue>
                    <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">6</ns:FieldValue>
                    <ns:FieldValue uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}">1</ns:FieldValue>
                    <ns:FieldValue uid="{da7fd65c-ea26-49f8-b7b8-a6871727d0bb}">U:{98ca1422-0171-4d1d-8b84-8cdda4ab70ae}:testsuperadmin</ns:FieldValue>
                    <ns:FieldValue uid="{cf84d248-60b6-4fe1-a8f1-a0d91c307b0a}">
                        <ns:EnumValue id="1">Rectangle</ns:EnumValue>
                    </ns:FieldValue>
                </ns:Item>
                <ns:Item catalogid="3" id="7">
                    <ns:FieldValue uid="{dd48a259-8a55-46a6-b7cd-95a0335197e3}">7265636f00000006000000084154505944626c653fe1a70f9fd98a37000000084154505844626c653fdf6fc64f52edf900000004455450584c6f6e67000000be00000004455450594c6f6e670000024d00000004454250584c6f6e67000002f800000004454250594c6f6e67000000fc</ns:FieldValue>
                    <ns:FieldValue uid="{03504c8d-c809-461c-a62e-55a4bd6c1adf}">
                        <ns:Items>
                            <ns:Item catalogid="3" id="7">
                                <ns:FieldValue uid="{af4b2e46-5f6a-11d2-8f20-0000c0e166dc}">Remove this spot</ns:FieldValue>
                                <ns:FieldValue uid="{340f81ff-bef4-4967-b5eb-da1db00b73c6}" displayValue="2011-04-25T20:04:34+08:00">1303733074000</ns:FieldValue>
                                <ns:FieldValue uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}">7</ns:FieldValue>
                                <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">7</ns:FieldValue>
                                <ns:FieldValue uid="{da7fd65c-ea26-49f8-b7b8-a6871727d0bb}">U:{98ca1422-0171-4d1d-8b84-8cdda4ab70ae}:testsuperadmin</ns:FieldValue>
                            </ns:Item>
                        </ns:Items>
                    </ns:FieldValue>
                    <ns:FieldValue uid="{d9bbc910-bae3-4862-97aa-b3ddb8bebb06}" displayValue="2011-04-25T20:04:09+08:00">1303733049000</ns:FieldValue>
                    <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">7</ns:FieldValue>
                    <ns:FieldValue uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}">1</ns:FieldValue>
                    <ns:FieldValue uid="{da7fd65c-ea26-49f8-b7b8-a6871727d0bb}">U:{98ca1422-0171-4d1d-8b84-8cdda4ab70ae}:testsuperadmin</ns:FieldValue>
                    <ns:FieldValue uid="{cf84d248-60b6-4fe1-a8f1-a0d91c307b0a}">
                        <ns:EnumValue id="0">Icon</ns:EnumValue>
                    </ns:FieldValue>
                </ns:Item>
            </ns:Items>
        </ns:FieldValue>
        <ns:FieldValue uid="{af4b2e00-5f6a-11d2-8f20-0000c0e166dc}">890-ppm.jpg</ns:FieldValue>
        <ns:FieldValue uid="{6060d669-a2ff-4284-af2e-4c6762139ea1}">
            <ns:EnumValue id="0">Item A</ns:EnumValue>
            <ns:EnumValue id="1">Item B</ns:EnumValue>
            <ns:EnumValue id="3">Item D</ns:EnumValue>
        </ns:FieldValue>
        <ns:FieldValue uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}">
            <ns:CategoryValue>$Categories:Artifacts:Daily Life Item</ns:CategoryValue>
            <ns:CategoryValue>$Categories:Images:Objects</ns:CategoryValue>
        </ns:FieldValue>
    </ns:Item>
    <ns:Item catalogid="3" id="2">
        <ns:FieldValue uid="{00a3b951-4c7b-4751-90c0-c88cf0eb4983}">
            <ns:Items>
                <ns:Item catalogid="3" id="13">
                    <ns:FieldValue uid="{86b3bf92-e7cc-4150-bd74-191ce4bf9374}">12345-6789</ns:FieldValue>
                    <ns:FieldValue uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}">2</ns:FieldValue>
                    <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">13</ns:FieldValue>
                    <ns:FieldValue uid="{8322d5e0-edc9-4c2a-9991-702c376d9edc}">Test 2nd level description field</ns:FieldValue>
                    <ns:FieldValue uid="{5bbb974b-d50a-45de-bb40-f6d7fb36bfd8}" displayValue="31-Mar-2011">2049029759</ns:FieldValue>
                </ns:Item>
            </ns:Items>
        </ns:FieldValue>
        <ns:FieldValue uid="{af4b2e00-5f6a-11d2-8f20-0000c0e166dc}">2002-4-scrimshaw2001-128-10.jpg</ns:FieldValue>
        <ns:FieldValue uid="{6060d669-a2ff-4284-af2e-4c6762139ea1}">
            <ns:EnumValue id="4">Item E</ns:EnumValue>
            <ns:EnumValue id="0">Item A</ns:EnumValue>
            <ns:EnumValue id="2">Item C</ns:EnumValue>
        </ns:FieldValue>
        <ns:FieldValue uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}">
            <ns:CategoryValue>$Categories:Artifacts:Personal Artefact</ns:CategoryValue>
            <ns:CategoryValue>$Categories:Artifacts:Daily Life Item</ns:CategoryValue>
            <ns:CategoryValue>$Categories:Images:Objects</ns:CategoryValue>
        </ns:FieldValue>
    </ns:Item>
</ns:Items>
</ns:Export>

我希望改进三个方面。

  1. 以前,只有“类别”字段具有多个值。现在,其他字段也可以有多个值。在本示例中,字段名称为测试多选字符串列表

  2. 不要将多个值分成不同的列,而是在值之间插入分隔符,例如使用|或我们可以定义的一些其他字符

  3. 可以看到,有两个多层字段:测试表字段和用户评论线程。希望改进的XSLT能够处理这种类型的字段

2011 年 4 月 26 日编辑

  1. 我已经通过复制最后 3 个模板并将 ns:CategoryValue 重命名为 ns:EnumValue 成功解决了这个问题

  2. 您的解决方案有效,成功解决了这个问题。谢谢!

  3. 抱歉我没有解释清楚。我只需要像其他字段一样转换所有多级字段和数据。即一栏对应一个字段。我不关心他们的嵌套关系

A system that I am using currently allows me to export selected record data in XML format. The system also provides an option to use XSLT to transform the XML. I like to use it to convert the XML to tab delimited text so that I can import the data to another application.

Here is a XML sample with three records exported. Four data fields are selected to be exported with each record.

<?xml version="1.1" encoding="UTF-8"?>
<!DOCTYPE Export>
<ns:Export xmlns:ns="http://www.canto.com/ns/Export/1.0">
<ns:Layout tableName="AssetRecords">
    <ns:Fields>
        <ns:Field uid="{5bbb974b-d50a-45de-bb40-f6d7fb36bfd8}" type="2" valueInterpretation="5">
            <ns:Name>Date Taken / Scanned</ns:Name>
        </ns:Field>
        <ns:Field uid="{af4b2e43-5f6a-11d2-8f20-0000c0e166dc}" type="0" valueInterpretation="0">
            <ns:Name>Title</ns:Name>
        </ns:Field>
        <ns:Field uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}" type="2" valueInterpretation="0">
            <ns:Name>ID</ns:Name>
        </ns:Field>
        <ns:Field uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}" type="0" valueInterpretation="0">
            <ns:Name>Categories</ns:Name>
        </ns:Field>
    </ns:Fields>
</ns:Layout>
<ns:Items>
    <ns:Item catalogid="4" id="1">
        <ns:FieldValue uid="{5bbb974b-d50a-45de-bb40-f6d7fb36bfd8}" displayValue="Oct 10, 2010">2049029450</ns:FieldValue>
        <ns:FieldValue uid="{af4b2e43-5f6a-11d2-8f20-0000c0e166dc}">teapot</ns:FieldValue>
        <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">1</ns:FieldValue>
        <ns:FieldValue uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}">
            <ns:CategoryValue>Artifacts:Daily Life Item</ns:CategoryValue>
            <ns:CategoryValue>Images:Objects</ns:CategoryValue>
        </ns:FieldValue>
    </ns:Item>
    <ns:Item catalogid="4" id="2">
        <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">2</ns:FieldValue>
        <ns:FieldValue uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}">
            <ns:CategoryValue>Artifacts:Personal Artefact</ns:CategoryValue>
            <ns:CategoryValue>Artifacts:Daily Life Item</ns:CategoryValue>
            <ns:CategoryValue>Images:Objects</ns:CategoryValue>
        </ns:FieldValue>
    </ns:Item>
    <ns:Item catalogid="4" id="10">
        <ns:FieldValue uid="{af4b2e43-5f6a-11d2-8f20-0000c0e166dc}">Product Overview</ns:FieldValue>
        <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">10</ns:FieldValue>
        <ns:FieldValue uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}">
            <ns:CategoryValue>Documents:Paper Records</ns:CategoryValue>
        </ns:FieldValue>
    </ns:Item>
</ns:Items>
</ns:Export>

My desired tab delimited output text is like the following table when open in Excel

https://lh5.googleusercontent.com/_Wsib_NwEZrA/TU6vzqbEGDI/AAAAAAAAAU0/V1SfGJyXJhQ/s800/2011-02-06_185321.png

I was given a XSLT file to do the conversion. However, it can only handle simple, straight-forward XML structure.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns="http://www.canto.com/ns/Export/1.0"
    xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output method="text" indent="yes" encoding="utf-8" standalone="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <Root>
        <xsl:apply-templates select="/ns:Export/ns:Items/ns:Item"/>
    </Root>
</xsl:template>

<xsl:template match="ns:Item">
    <xsl:for-each select="ns:FieldValue">
        <xsl:value-of select="normalize-space(text())"/>
        <xsl:text>	</xsl:text>
    </xsl:for-each>
    <xsl:text>
</xsl:text>
</xsl:template>

</xsl:stylesheet>

I like to have an improved version so that

  • field names are shown as column
    headers
  • for date : the display value is used,
    not the numbers
  • for categories: all the categories
    displayed in separate columns
  • for empty data: empty cell is shown

I have limited XML knowledge. Hope someone can assist. Thanks :-)

Edited on 25 April 2011

The XSLT provided has worked very well until I tested on some new fields. Below is the XML exported from the system. It consists of two records.

<?xml version="1.1" encoding="UTF-8"?>
<!DOCTYPE Export>
<ns:Export xmlns:ns="http://www.canto.com/ns/Export/1.0">
<ns:Layout tableName="AssetRecords">
    <ns:Fields>
        <ns:Field uid="{00a3b951-4c7b-4751-90c0-c88cf0eb4983}" type="10" valueInterpretation="0">
            <ns:Name>Test Table Field</ns:Name>
            <ns:Layout tableName="AssetRecords/{00a3b951-4c7b-4751-90c0-c88cf0eb4983}">
                <ns:Fields>
                    <ns:Field uid="{86b3bf92-e7cc-4150-bd74-191ce4bf9374}" type="0" valueInterpretation="0">
                        <ns:Name>Accession Number</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}" type="2" valueInterpretation="0">
                        <ns:Name>Host Item ID</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}" type="2" valueInterpretation="0">
                        <ns:Name>ID</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{8322d5e0-edc9-4c2a-9991-702c376d9edc}" type="0" valueInterpretation="0">
                        <ns:Name>Description / Synopsis</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{5bbb974b-d50a-45de-bb40-f6d7fb36bfd8}" type="2" valueInterpretation="5">
                        <ns:Name>Date Taken / Scanned</ns:Name>
                    </ns:Field>
                </ns:Fields>
            </ns:Layout>
        </ns:Field>
        <ns:Field uid="{04a4080f-fec0-4e01-822f-b9125c0b5ac9}" type="10" valueInterpretation="0">
            <ns:Name>User Comment Thread</ns:Name>
            <ns:Layout tableName="AssetRecords/{04a4080f-fec0-4e01-822f-b9125c0b5ac9}">
                <ns:Fields>
                    <ns:Field uid="{dd48a259-8a55-46a6-b7cd-95a0335197e3}" type="5" valueInterpretation="0">
                        <ns:Name>User Comment Coordinates</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{03504c8d-c809-461c-a62e-55a4bd6c1adf}" type="10" valueInterpretation="0">
                        <ns:Name>User Comment</ns:Name>
                        <ns:Layout tableName="AssetRecords/{04a4080f-fec0-4e01-822f-b9125c0b5ac9}/{03504c8d-c809-461c-a62e-55a4bd6c1adf}">
                            <ns:Fields>
                                <ns:Field uid="{af4b2e46-5f6a-11d2-8f20-0000c0e166dc}" type="0" valueInterpretation="0">
                                    <ns:Name>Comments</ns:Name>
                                </ns:Field>
                                <ns:Field uid="{340f81ff-bef4-4967-b5eb-da1db00b73c6}" type="4" valueInterpretation="0">
                                    <ns:Name>Modification Date</ns:Name>
                                </ns:Field>
                                <ns:Field uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}" type="2" valueInterpretation="0">
                                    <ns:Name>Host Item ID</ns:Name>
                                </ns:Field>
                                <ns:Field uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}" type="2" valueInterpretation="0">
                                    <ns:Name>ID</ns:Name>
                                </ns:Field>
                                <ns:Field uid="{d332e5f9-980a-4685-bdd1-235635ac7f2f}" type="2" valueInterpretation="0">
                                    <ns:Name>Replied On</ns:Name>
                                </ns:Field>
                                <ns:Field uid="{da7fd65c-ea26-49f8-b7b8-a6871727d0bb}" type="0" valueInterpretation="10">
                                    <ns:Name>User</ns:Name>
                                </ns:Field>
                            </ns:Fields>
                        </ns:Layout>
                    </ns:Field>
                    <ns:Field uid="{228051ee-4983-462b-84e8-3644a14c0d52}" type="2" valueInterpretation="0">
                        <ns:Name>Page Number</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{d9bbc910-bae3-4862-97aa-b3ddb8bebb06}" type="4" valueInterpretation="0">
                        <ns:Name>Creation Date</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}" type="2" valueInterpretation="0">
                        <ns:Name>ID</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}" type="2" valueInterpretation="0">
                        <ns:Name>Host Item ID</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{da7fd65c-ea26-49f8-b7b8-a6871727d0bb}" type="0" valueInterpretation="10">
                        <ns:Name>User</ns:Name>
                    </ns:Field>
                    <ns:Field uid="{cf84d248-60b6-4fe1-a8f1-a0d91c307b0a}" type="7" valueInterpretation="0">
                        <ns:Name>User Comment Type</ns:Name>
                    </ns:Field>
                </ns:Fields>
            </ns:Layout>
        </ns:Field>
        <ns:Field uid="{af4b2e00-5f6a-11d2-8f20-0000c0e166dc}" type="0" valueInterpretation="0">
            <ns:Name>Record Name</ns:Name>
        </ns:Field>
        <ns:Field uid="{6060d669-a2ff-4284-af2e-4c6762139ea1}" type="7" valueInterpretation="7">
            <ns:Name>Test multi-select string list</ns:Name>
        </ns:Field>
        <ns:Field uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}" type="0" valueInterpretation="0">
            <ns:Name>Categories</ns:Name>
        </ns:Field>
    </ns:Fields>
</ns:Layout>
<ns:Items>
    <ns:Item catalogid="3" id="1">
        <ns:FieldValue uid="{00a3b951-4c7b-4751-90c0-c88cf0eb4983}">
            <ns:Items>
                <ns:Item catalogid="3" id="1">
                    <ns:FieldValue uid="{86b3bf92-e7cc-4150-bd74-191ce4bf9374}">table access number test</ns:FieldValue>
                    <ns:FieldValue uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}">1</ns:FieldValue>
                    <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">1</ns:FieldValue>
                    <ns:FieldValue uid="{8322d5e0-edc9-4c2a-9991-702c376d9edc}">table decription test</ns:FieldValue>
                    <ns:FieldValue uid="{5bbb974b-d50a-45de-bb40-f6d7fb36bfd8}" displayValue="12-Jan-1967">2049007148</ns:FieldValue>
                </ns:Item>
                <ns:Item catalogid="3" id="12">
                    <ns:FieldValue uid="{86b3bf92-e7cc-4150-bd74-191ce4bf9374}">789-123</ns:FieldValue>
                    <ns:FieldValue uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}">1</ns:FieldValue>
                    <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">12</ns:FieldValue>
                    <ns:FieldValue uid="{8322d5e0-edc9-4c2a-9991-702c376d9edc}">data entered using Web Client in IE7</ns:FieldValue>
                    <ns:FieldValue uid="{5bbb974b-d50a-45de-bb40-f6d7fb36bfd8}" displayValue="31-Oct-1977">2049012575</ns:FieldValue>
                </ns:Item>
            </ns:Items>
        </ns:FieldValue>
        <ns:FieldValue uid="{04a4080f-fec0-4e01-822f-b9125c0b5ac9}">
            <ns:Items>
                <ns:Item catalogid="3" id="6">
                    <ns:FieldValue uid="{dd48a259-8a55-46a6-b7cd-95a0335197e3}">7265636f00000008000000084154505944626c653fc6d5ef2c783013000000084154505844626c653fb3d4bb7e327a97000000084142505844626c653fd33ae45b57bcb2000000084142505944626c653fcd12073615a24000000004455450584c6f6e670000002800000004455450594c6f6e670000012600000004454250584c6f6e67000001ee00000004454250594c6f6e6700000122</ns:FieldValue>
                    <ns:FieldValue uid="{03504c8d-c809-461c-a62e-55a4bd6c1adf}">
                        <ns:Items>
                            <ns:Item catalogid="3" id="6">
                                <ns:FieldValue uid="{af4b2e46-5f6a-11d2-8f20-0000c0e166dc}">Crop this part</ns:FieldValue>
                                <ns:FieldValue uid="{340f81ff-bef4-4967-b5eb-da1db00b73c6}" displayValue="2011-04-25T20:03:51+08:00">1303733031000</ns:FieldValue>
                                <ns:FieldValue uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}">6</ns:FieldValue>
                                <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">6</ns:FieldValue>
                                <ns:FieldValue uid="{da7fd65c-ea26-49f8-b7b8-a6871727d0bb}">U:{98ca1422-0171-4d1d-8b84-8cdda4ab70ae}:testsuperadmin</ns:FieldValue>
                            </ns:Item>
                        </ns:Items>
                    </ns:FieldValue>
                    <ns:FieldValue uid="{d9bbc910-bae3-4862-97aa-b3ddb8bebb06}" displayValue="2011-04-25T20:03:40+08:00">1303733020000</ns:FieldValue>
                    <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">6</ns:FieldValue>
                    <ns:FieldValue uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}">1</ns:FieldValue>
                    <ns:FieldValue uid="{da7fd65c-ea26-49f8-b7b8-a6871727d0bb}">U:{98ca1422-0171-4d1d-8b84-8cdda4ab70ae}:testsuperadmin</ns:FieldValue>
                    <ns:FieldValue uid="{cf84d248-60b6-4fe1-a8f1-a0d91c307b0a}">
                        <ns:EnumValue id="1">Rectangle</ns:EnumValue>
                    </ns:FieldValue>
                </ns:Item>
                <ns:Item catalogid="3" id="7">
                    <ns:FieldValue uid="{dd48a259-8a55-46a6-b7cd-95a0335197e3}">7265636f00000006000000084154505944626c653fe1a70f9fd98a37000000084154505844626c653fdf6fc64f52edf900000004455450584c6f6e67000000be00000004455450594c6f6e670000024d00000004454250584c6f6e67000002f800000004454250594c6f6e67000000fc</ns:FieldValue>
                    <ns:FieldValue uid="{03504c8d-c809-461c-a62e-55a4bd6c1adf}">
                        <ns:Items>
                            <ns:Item catalogid="3" id="7">
                                <ns:FieldValue uid="{af4b2e46-5f6a-11d2-8f20-0000c0e166dc}">Remove this spot</ns:FieldValue>
                                <ns:FieldValue uid="{340f81ff-bef4-4967-b5eb-da1db00b73c6}" displayValue="2011-04-25T20:04:34+08:00">1303733074000</ns:FieldValue>
                                <ns:FieldValue uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}">7</ns:FieldValue>
                                <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">7</ns:FieldValue>
                                <ns:FieldValue uid="{da7fd65c-ea26-49f8-b7b8-a6871727d0bb}">U:{98ca1422-0171-4d1d-8b84-8cdda4ab70ae}:testsuperadmin</ns:FieldValue>
                            </ns:Item>
                        </ns:Items>
                    </ns:FieldValue>
                    <ns:FieldValue uid="{d9bbc910-bae3-4862-97aa-b3ddb8bebb06}" displayValue="2011-04-25T20:04:09+08:00">1303733049000</ns:FieldValue>
                    <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">7</ns:FieldValue>
                    <ns:FieldValue uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}">1</ns:FieldValue>
                    <ns:FieldValue uid="{da7fd65c-ea26-49f8-b7b8-a6871727d0bb}">U:{98ca1422-0171-4d1d-8b84-8cdda4ab70ae}:testsuperadmin</ns:FieldValue>
                    <ns:FieldValue uid="{cf84d248-60b6-4fe1-a8f1-a0d91c307b0a}">
                        <ns:EnumValue id="0">Icon</ns:EnumValue>
                    </ns:FieldValue>
                </ns:Item>
            </ns:Items>
        </ns:FieldValue>
        <ns:FieldValue uid="{af4b2e00-5f6a-11d2-8f20-0000c0e166dc}">890-ppm.jpg</ns:FieldValue>
        <ns:FieldValue uid="{6060d669-a2ff-4284-af2e-4c6762139ea1}">
            <ns:EnumValue id="0">Item A</ns:EnumValue>
            <ns:EnumValue id="1">Item B</ns:EnumValue>
            <ns:EnumValue id="3">Item D</ns:EnumValue>
        </ns:FieldValue>
        <ns:FieldValue uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}">
            <ns:CategoryValue>$Categories:Artifacts:Daily Life Item</ns:CategoryValue>
            <ns:CategoryValue>$Categories:Images:Objects</ns:CategoryValue>
        </ns:FieldValue>
    </ns:Item>
    <ns:Item catalogid="3" id="2">
        <ns:FieldValue uid="{00a3b951-4c7b-4751-90c0-c88cf0eb4983}">
            <ns:Items>
                <ns:Item catalogid="3" id="13">
                    <ns:FieldValue uid="{86b3bf92-e7cc-4150-bd74-191ce4bf9374}">12345-6789</ns:FieldValue>
                    <ns:FieldValue uid="{e709c047-7fca-4081-9c4b-41fb70bf31f4}">2</ns:FieldValue>
                    <ns:FieldValue uid="{c02adb31-5c2c-4014-b86a-a53cf83f7e6c}">13</ns:FieldValue>
                    <ns:FieldValue uid="{8322d5e0-edc9-4c2a-9991-702c376d9edc}">Test 2nd level description field</ns:FieldValue>
                    <ns:FieldValue uid="{5bbb974b-d50a-45de-bb40-f6d7fb36bfd8}" displayValue="31-Mar-2011">2049029759</ns:FieldValue>
                </ns:Item>
            </ns:Items>
        </ns:FieldValue>
        <ns:FieldValue uid="{af4b2e00-5f6a-11d2-8f20-0000c0e166dc}">2002-4-scrimshaw2001-128-10.jpg</ns:FieldValue>
        <ns:FieldValue uid="{6060d669-a2ff-4284-af2e-4c6762139ea1}">
            <ns:EnumValue id="4">Item E</ns:EnumValue>
            <ns:EnumValue id="0">Item A</ns:EnumValue>
            <ns:EnumValue id="2">Item C</ns:EnumValue>
        </ns:FieldValue>
        <ns:FieldValue uid="{af4b2e0c-5f6a-11d2-8f20-0000c0e166dc}">
            <ns:CategoryValue>$Categories:Artifacts:Personal Artefact</ns:CategoryValue>
            <ns:CategoryValue>$Categories:Artifacts:Daily Life Item</ns:CategoryValue>
            <ns:CategoryValue>$Categories:Images:Objects</ns:CategoryValue>
        </ns:FieldValue>
    </ns:Item>
</ns:Items>
</ns:Export>

There are three areas that I like to improve.

  1. Previously, only the Category field has multi values. Now, other field can have multi values too. In this example, the field name is Test multi-select string list

  2. Instead of breaking the multi values into different columns, insert separators between the values e.g. use | or some other characters that we can define

  3. As can be seen, there are two multi level fields: Test Table Field and User Comment Thread. Hope that the improved XSLT can handle this type of filed

Edit on 26 Apr 2011

  1. I have solved this successfully by duplicating the last 3 templates and renaming ns:CategoryValue to ns:EnumValue

  2. Your solution works. Thanks!

  3. Sorry that I did not explain clearly. I just need all the multi-level fields and data to be transformed like the rest of the fields. That is, one column for one field. I do not care about their nested relationship

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枉心 2024-10-23 05:55:12

试试这个:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns="http://www.canto.com/ns/Export/1.0"
    xmlns:fn="http://www.w3.org/2005/xpath-functions">
  <xsl:output method="text" indent="yes" encoding="utf-8" standalone="yes"/>
  <xsl:strip-space elements="*"/>

  <!-- Parameterized separator/end of line characters for flexibility -->
  <xsl:param name="sep" select="'	'" />
  <xsl:param name="eol" select="'
'" />
  <xsl:param name="listsep" select="'|'" />

  <!-- On matching the root node, output a list of field names, followed by the items -->
  <xsl:template match="/ns:Export">
    <xsl:apply-templates select="ns:Layout/ns:Fields/ns:Field" />
    <xsl:apply-templates select="ns:Items/ns:Item"/>
  </xsl:template>

  <!-- On matching all but the last field name, output the name followed by separator -->
  <xsl:template match="ns:Field[position()!=last()]">
    <xsl:value-of select="concat(normalize-space(ns:Name),$sep)" />
  </xsl:template>

  <!-- On matching the last field name, output the name followed by a newline -->
  <xsl:template match="ns:Field[position()=last()]">
    <xsl:value-of select="concat(normalize-space(ns:Name),$eol)" />
  </xsl:template>

  <!-- On matching an item, iterate through each field, applying templates to any 'ns:FieldValue' nodes that share the same value of @uid -->
  <xsl:template match="ns:Item">
    <xsl:variable name="item" select="." />
    <xsl:for-each select="/ns:Export/ns:Layout/ns:Fields/ns:Field/@uid">
      <xsl:apply-templates select="$item/ns:FieldValue[@uid=current()]" />
      <xsl:if test="position()!=last()">
        <xsl:value-of select="$sep" />
      </xsl:if>
    </xsl:for-each>
    <xsl:value-of select="$eol" />
  </xsl:template>

  <!-- On matching a field value, output the content. -->
  <xsl:template match="ns:FieldValue">
    <xsl:value-of select="normalize-space(.)" />
  </xsl:template>

  <!-- on matching a field value with a @displayValue attribute, output the value of that attribute -->
  <xsl:template match="ns:FieldValue[@displayValue]">
    <xsl:value-of select="normalize-space(@displayValue)" />
  </xsl:template>

  <!-- On matching a field value with ns:CategoryValue children, apply templates on those children. -->
  <xsl:template match="ns:FieldValue[ns:CategoryValue]">
    <xsl:apply-templates select="ns:CategoryValue" />
  </xsl:template>

  <!-- On matching a category value, output it's content, plus a separator. -->
  <xsl:template match="ns:CategoryValue">
    <xsl:value-of select="concat(normalize-space(.),$listsep)" />
  </xsl:template>

  <!-- On matching the last category value, output it's content without a separator. -->
  <xsl:template match="ns:CategoryValue[position()=last()]">
    <xsl:value-of select="normalize-space(.)" />
  </xsl:template>
</xsl:stylesheet>

这可能不是使用 XSLT 的最有效方法,但它相对简单。正如您可能看到的,我在注释中嵌入了模板的基本描述。我不确定您使用什么作为换行符(CR+LF 还是只是 LF?),但我已经对其进行了参数化,目前使用 LF。如果需要使用 CR+LF,请将 行更改为:

<xsl:param name="eol" select="'
'" />

ns:item 模板有点棘手;如果您需要解释,请告诉我。

编辑:我对其进行了轻微修改,以允许对单个字段中的值列表使用不同的分隔符,只需添加第三个参数,并在倒数第二个模板中使用它来代替现有的 $sep参数。

Try this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns="http://www.canto.com/ns/Export/1.0"
    xmlns:fn="http://www.w3.org/2005/xpath-functions">
  <xsl:output method="text" indent="yes" encoding="utf-8" standalone="yes"/>
  <xsl:strip-space elements="*"/>

  <!-- Parameterized separator/end of line characters for flexibility -->
  <xsl:param name="sep" select="'	'" />
  <xsl:param name="eol" select="'
'" />
  <xsl:param name="listsep" select="'|'" />

  <!-- On matching the root node, output a list of field names, followed by the items -->
  <xsl:template match="/ns:Export">
    <xsl:apply-templates select="ns:Layout/ns:Fields/ns:Field" />
    <xsl:apply-templates select="ns:Items/ns:Item"/>
  </xsl:template>

  <!-- On matching all but the last field name, output the name followed by separator -->
  <xsl:template match="ns:Field[position()!=last()]">
    <xsl:value-of select="concat(normalize-space(ns:Name),$sep)" />
  </xsl:template>

  <!-- On matching the last field name, output the name followed by a newline -->
  <xsl:template match="ns:Field[position()=last()]">
    <xsl:value-of select="concat(normalize-space(ns:Name),$eol)" />
  </xsl:template>

  <!-- On matching an item, iterate through each field, applying templates to any 'ns:FieldValue' nodes that share the same value of @uid -->
  <xsl:template match="ns:Item">
    <xsl:variable name="item" select="." />
    <xsl:for-each select="/ns:Export/ns:Layout/ns:Fields/ns:Field/@uid">
      <xsl:apply-templates select="$item/ns:FieldValue[@uid=current()]" />
      <xsl:if test="position()!=last()">
        <xsl:value-of select="$sep" />
      </xsl:if>
    </xsl:for-each>
    <xsl:value-of select="$eol" />
  </xsl:template>

  <!-- On matching a field value, output the content. -->
  <xsl:template match="ns:FieldValue">
    <xsl:value-of select="normalize-space(.)" />
  </xsl:template>

  <!-- on matching a field value with a @displayValue attribute, output the value of that attribute -->
  <xsl:template match="ns:FieldValue[@displayValue]">
    <xsl:value-of select="normalize-space(@displayValue)" />
  </xsl:template>

  <!-- On matching a field value with ns:CategoryValue children, apply templates on those children. -->
  <xsl:template match="ns:FieldValue[ns:CategoryValue]">
    <xsl:apply-templates select="ns:CategoryValue" />
  </xsl:template>

  <!-- On matching a category value, output it's content, plus a separator. -->
  <xsl:template match="ns:CategoryValue">
    <xsl:value-of select="concat(normalize-space(.),$listsep)" />
  </xsl:template>

  <!-- On matching the last category value, output it's content without a separator. -->
  <xsl:template match="ns:CategoryValue[position()=last()]">
    <xsl:value-of select="normalize-space(.)" />
  </xsl:template>
</xsl:stylesheet>

This probably isn't the most efficient way of using XSLT, but it's relatively simple. As you can probably see, I've embedded basic descriptions of the templates in comments. I'm not sure what you're using as a newline character (CR+LF or just LF?), but I've parameterised it, using LF at the moment. If you need to use CR+LF, change the <xsl:param name="eol" line to:

<xsl:param name="eol" select="'
'" />

The ns:item template's a little tricky; let me know if you need an explanation of it.

EDIT: I've amended it very slightly to allow a different separator for a list of values in a single field, just adding a third parameter, and using it in the second last template in place of the existing $sep parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文