使用 Xslt 将 Xml 中相似节点的数据导出到 Excel
我有一个 xml 文档,其中动态生成值,我需要使用 xslt 将这些数据导出到 Excel。为了测试目的,我创建了一个示例 xml:-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ucdetector>
<!--COPY_RIGHT-->
<statistics/>
<markers>
<!--Sample XML_INFO-->
<marker>
<description>Desc</description>
<classRef>Test1</classRef>
<classRef>Test2</classRef>
<classRef>Test3</classRef>
<markerType>Class PreferenceInitializer has 8 references</markerType>
</marker>
</markers>
<problems/>
</ucdetector>
xslt 如下:-
= tab = new line -->
<!-- First line: about -->
<xsl:value-of
select="concat('Report', '	', /ucdetector/statistics/abouts/about[@name='reportCreated']/value, '
', '
')" />
<!-- Second line: header -->
<xsl:value-of
select="concat('Class Name', '	', 'Referring class
name', '	', 'Class Details', '
')" />
<xsl:for-each select="/ucdetector/markers/marker">
<xsl:variable name="vars">
<xsl:apply-templates select="classRef" />
</xsl:variable>
<xsl:value-of
select="concat(description, '	', $vars, '	', markerType, '
')" />
</xsl:for-each>
我能够将所有数据正确导出到 Excel 中,但标签“classRef”中的数据一起显示为“Test1Test2Test3”,但我需要将它们分别放在 Excel 的不同列中。 有人可以提供一些线索吗?
I have an xml document where values are generated dynamically and i need to export these data to an excel using xslt. For testing purpose I have created a sample xml:-
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ucdetector>
<!--COPY_RIGHT-->
<statistics/>
<markers>
<!--Sample XML_INFO-->
<marker>
<description>Desc</description>
<classRef>Test1</classRef>
<classRef>Test2</classRef>
<classRef>Test3</classRef>
<markerType>Class PreferenceInitializer has 8 references</markerType>
</marker>
</markers>
<problems/>
</ucdetector>
The xslt is as follows:-
= tab = new line -->
<!-- First line: about -->
<xsl:value-of
select="concat('Report', ' ', /ucdetector/statistics/abouts/about[@name='reportCreated']/value, '
', '
')" />
<!-- Second line: header -->
<xsl:value-of
select="concat('Class Name', ' ', 'Referring class
name', ' ', 'Class Details', '
')" />
<xsl:for-each select="/ucdetector/markers/marker">
<xsl:variable name="vars">
<xsl:apply-templates select="classRef" />
</xsl:variable>
<xsl:value-of
select="concat(description, ' ', $vars, ' ', markerType, '
')" />
</xsl:for-each>
I was able to export all the data properly into excel except for data in tag "classRef" which appears together as "Test1Test2Test3" , but i need them separately in different columns of excel.
Can anybody provide some clue on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜您希望这三个值以制表符分隔?在这种情况下,我倾向于这样做(在 XSLT 2.0 中)
或在 1.0 中
I guess you want the three values to be tab-separated? In which case I would be inclined to do this (in XSLT 2.0)
or in 1.0
你的问题有点不清楚。我认为您想要做的是发出
marker
元素的所有子元素的文本,以制表符分隔,作为以换行符结尾的文本行。是这样吗?如果是这样,我认为创建一个包含所有连接在一起的
classRef
元素的变量没有任何好处 - 您对待它们与对待其他元素没有任何不同。您只需使用一个简单的模板即可将每个marker
元素转换为输出行:编辑:
好的,您发布的示例阐明了您要查找的内容。您想要的是每个
classRef
元素(而不是每个marker
元素)的输出中的一行。每行应该有相同数量的制表符,并以换行符终止。但任何给定marker
元素的第一行应包含非classRef
元素的数据,而其余行应仅包含classRef
元素。这应该可以解决问题:
Your question's a little unclear. I think what you're trying to do is emit the text of all of the child elements of the
marker
element, separated by tabs, as a line of text terminated with a newline. Is that right?If so, I don't see any benefit of creating a variable that contains the
classRef
elements all concatenated together - you're not treating them any differently than you are the other elements. You can just use a simple template to transform eachmarker
element into an output line:Edit:
Okay, the example you posted clarifies what you're looking for. What you want is one line in the output for each
classRef
element, not eachmarker
element. Every line should have the same number of tabs, and be terminated with a newline. But the first line for any givenmarker
element should contain data for the non-classRef
elements, while the remaining lines should only contain data for theclassRef
elements.This should do the trick: