修改XSLT以将XML转换为制表符分隔的文本文件 [英] Modifying a XSLT for converting XML to tab delimited text file

查看:114
本文介绍了修改XSLT以将XML转换为制表符分隔的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是一个XML示例,其中导出了三个记录.选择四个数据字段与每个记录一起导出.

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>

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

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

我得到了一个XSLT文件来进行转换.但是,它只能处理简单,直接的XML结构.

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>&#09;</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

我对XML的了解有限.希望有人能帮忙.谢谢:-)

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

2011年4月25日编辑

在我在一些新领域进行测试之前,提供的XSLT一直运行良好.以下是从系统导出的XML.它包含两个记录.

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>

我要改进三个方面.

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

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

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

可以看到,有两个多级字段:测试表"字段和用户注释线程".希望改进的XSLT可以处理这种类型的文件

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

2011年4月26日修改

  1. 我已经成功解决了这一问题,方法是复制最后三个模板并将ns:CategoryValue重命名为ns:EnumValue

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

您的解决方案有效.谢谢!

Your solution works. Thanks!

对不起,我没有清楚解释.我只需要像所有其他字段一样转换所有多级字段和数据即可.即,一栏对应一栏.我不在乎他们的嵌套关系

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

推荐答案

尝试一下:

<?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="'&#09;'" />
  <xsl:param name="eol" select="'&#10;'" />
  <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"行更改为:

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="'&#13;&#10;'" />

ns:item模板有点棘手;让我知道是否需要解释.

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

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

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.

这篇关于修改XSLT以将XML转换为制表符分隔的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆