使用 XSLT 读取 XML 文本并将其排成一行 [英] Reading a XML text and placing it in row using XSLT
问题描述
我有下面的 XML 文件.我需要输出 Event
元素的 Type
和 Headline
,这些元素在它们的 Headline
和步骤号中包含step".
I have the XML file below. I need to output Type
and Headline
of Event
elements that contain 'step' in their Headline
and the step number.
<?xml version="1.0" encoding="ISO-8859-1"?>
<Testlog>
<Event Timestamp="27-Dec-2012 04:25:12.247 PM" Type="Script End" Headline="Script end [DSE.DSEBalanceInquiry_FC.DseBalanceInquiry_FC_Test_003]" Result="WARNING">
<Property script_name="DSE.DSEBalanceInquiry_FC.DseBalanceInquiry_FC_Test_003"/>
<Property script_id="DSE.DSEBalanceInquiry_FC.DseBalanceInquiry_FC_Test_003.java"/>
</Event>
<Event Timestamp="27-Dec-2012 04:16:33.335 PM" Type="General" Headline="_FRMWK.SystemLibrary.Sys_TmxProcesses logStepBegin: Step: 2.001; Action: ImportACU; Narrative: Import ACU settings based on '' ; TestName: DseBalanceInquiry_FC_Test_003" Result="INFORMATION">
<Property script_name="DSE.DSEBalanceInquiry_FC.DseBalanceInquiry_FC_Test_003"/>
<Property line_number="64"/>
<Property script_id="DSE.DSEBalanceInquiry_FC.DseBalanceInquiry_FC_Test_003.java"/>
</Event>
</Testlog>
我的 XSLT 代码给了我一个包含step"的标题列表.如果我想在单独的行中获取步骤编号,我该如何实现?
My XSLT code gives me a list of headlines that contain "step". If I want to get the step number in a separate row, how I can achieve it?
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Report</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
<th style="text-align:left">Step</th>
<th style="text-align:left">Headline</th>
</tr>
<xsl:for-each select="Testlog/Event[contains(@Headline, 'Step:')]">
<tr>
<td><xsl:value-of select="@Type"/></td>
<td><xsl:value-of select="@Result"/></td>
<td><xsl:value-of select="@Step"/></td>
<td><xsl:value-of select="@Headline"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
推荐答案
以下样式表满足您的需求.这一行:
The following stylesheet does what you want. This line:
<xsl:value-of select="substring-before(substring-after(@Headline,'Step: '),';')"/>
使用 substring-after 和 substring-before 的组合提取 Headline
属性中的步骤编号部分.
extracts the part of the Headline
attribute that is the step number, using a combination of substring-after and substring-before.
此外,我已将您的列标题更改为更有意义的内容.显然,您是从 W3Schools 中获取的.在描述您的意图时请小心:我相信您不希望单独一行中的步骤编号".您想要在同一行中的步骤编号,并表示在单独的列中",对吗?
Also, I have changed your column headers to something more meaningful. Obviously, you have taken them from W3Schools. Please be careful when describing what your intent is: You do not want the "step number in a separate row" I believe. You want the step number in the same row and meant to say "in a separate column", right?
样式表
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Report</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Type</th>
<th style="text-align:left">Result</th>
<th style="text-align:left">Step Number</th>
<th style="text-align:left">Headline</th>
</tr>
<xsl:for-each select="Testlog/Event[contains(@Headline, 'Step:')]">
<tr>
<td><xsl:value-of select="@Type"/></td>
<td><xsl:value-of select="@Result"/></td>
<td><xsl:value-of select="substring-before(substring-after(@Headline,'Step: '),';')"/></td>
<td><xsl:value-of select="@Headline"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
输出
<html>
<body>
<h2>Report</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Type</th>
<th style="text-align:left">Result</th>
<th style="text-align:left">Step Number</th>
<th style="text-align:left">Headline</th>
</tr>
<tr>
<td>General</td>
<td>INFORMATION</td>
<td>2.001</td>
<td>_FRMWK.SystemLibrary.Sys_TmxProcesses logStepBegin: Step: 2.001; Action: ImportACU; Narrative: Import ACU settings
based on '' ; TestName: DseBalanceInquiry_FC_Test_003
</td>
</tr>
</table>
</body>
</html>
这篇关于使用 XSLT 读取 XML 文本并将其排成一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!