修改 XSLT 以将 XML 转换为制表符分隔的文本文件
我当前使用的系统允许我以 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 中打开时,我所需的制表符分隔输出文本如下表所示
我获得了一个 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>	</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>
我希望改进三个方面。
-
以前,只有“类别”字段具有多个值。现在,其他字段也可以有多个值。在本示例中,字段名称为测试多选字符串列表
-
不要将多个值分成不同的列,而是在值之间插入分隔符,例如使用|或我们可以定义的一些其他字符
可以看到,有两个多层字段:测试表字段和用户评论线程。希望改进的XSLT能够处理这种类型的字段
2011 年 4 月 26 日编辑
-
我已经通过复制最后 3 个模板并将 ns:CategoryValue 重命名为 ns:EnumValue 成功解决了这个问题
-
您的解决方案有效,成功解决了这个问题。谢谢!
-
抱歉我没有解释清楚。我只需要像其他字段一样转换所有多级字段和数据。即一栏对应一个字段。我不关心他们的嵌套关系
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
这可能不是使用 XSLT 的最有效方法,但它相对简单。正如您可能看到的,我在注释中嵌入了模板的基本描述。我不确定您使用什么作为换行符(CR+LF 还是只是 LF?),但我已经对其进行了参数化,目前使用 LF。如果需要使用 CR+LF,请将 行更改为:
ns:item
模板有点棘手;如果您需要解释,请告诉我。编辑:我对其进行了轻微修改,以允许对单个字段中的值列表使用不同的分隔符,只需添加第三个参数,并在倒数第二个模板中使用它来代替现有的
$sep参数。
Try this:
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: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.