【问题标题】:How to transform an XML file using XSLT如何使用 XSLT 转换 XML 文件
【发布时间】:2014-01-07 10:54:01
【问题描述】:

我是编码新手,在使用 XSLT 解析 XML 文件和提供 HTML 输出方面需要帮助。 我有下面的代码。 我需要具有 result="INFORMATION"

的脚本名称和类型
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="class.xsl"?>
<TestLog>
 <Event dpIter="0">
  <Event Timestamp="27-Dec-2012 04:16:26.830 PM" Type="Script Start" Headline="Script       start [DSE.DSEBalanceInquiry_FC.DseBalanceInquiry_FC_Test_003]" Result="INFORMATION">
    <Property line_number="1"/>
    <Property script_iter_count="0"/>
    <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:26.830 PM" Type="General" Headline="D:\TestAutomation\LogLevels.txt (The system cannot find the file specified.)" Result="INFORMATION">
    <Property script_name="DSE.DSEBalanceInquiry_FC.DseBalanceInquiry_FC_Test_003"/>
    <Property line_number="35"/>
    <Property script_id="DSE.DSEBalanceInquiry_FC.DseBalanceInquiry_FC_Test_003.java"/>
  </Event>
 </Event>
</TestLog>

XSLT 代码:

  <?xml version="1.0" encoding="ISO-8859-1"?> 
  <xsl:stylesheet version="1.0" xmlns:xsl="w3.org/1999/XSL/Transform">; 
  <xsl:template match="/">
  <html> 
  <body> 
  <h2></h2> 
  <table border="1">
  <tr> 
  <th>Type</th>
  <th>Result</th> 
 </tr> 
 <xsl:for-each select="TestLog/Event"> 
 <tr> 
  <td><xsl:value-of select="Type"/></td> 
  <td><xsl:value-of select="Result"/></td> 
 </tr> 
 </xsl:for-each> 
   </table> 
  </body> 
  </html> 
  </xsl:template> 
 </xsl:stylesheet>

【问题讨论】:

  • 请将您的 XSLT 样式表(到目前为止您尝试过的)添加到帖子中。
  • 我试图从 event 获取类型,但没有得到任何输出 w3.org/1999/XSL/Transform" >
  • 您是否看过 XSLT 工作原理的最基本示例?
  • 是的,我曾尝试从 xml 获取事件或类型,但无法获取。

标签: java html xml xslt


【解决方案1】:

据我了解,如果 Event 节点的 Result 属性等于“INFORMATION”,则您希望访问它们。

注意:这会告诉您 XSLT 的一般原则,因为您的问题很宽泛且不具体。只有问题更准确,才能给出更准确的答案。

在 XSLT 中,您可以在方括号中指定此类限制:

<xsl:template match="Event[parent::Event and @Result='INFORMATION']">

如果Event 元素是上级Event 元素的子元素并且它们的@Result 是正确的,则上面的代码行匹配。

这是一个完整的样式表,作为查找特定节点的示例:

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="TestLog|Event[parent::Testlog]">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="Event[parent::Event and @Result='INFORMATION']">
  <xsl:text>Type: </xsl:text>
  <xsl:value-of select="@Type"/>
  <xsl:text>&#10;</xsl:text>
  <xsl:text>Script name: </xsl:text>
  <xsl:value-of select="Property/@script_name"/>
  <xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template match="Property"/>

</xsl:stylesheet>

如果应用于您的输入 XML,结果如下:

Type: Script Start
Script name: DSE.DSEBalanceInquiry_FC.DseBalanceInquiry_FC_Test_003
Type: General
Script name: DSE.DSEBalanceInquiry_FC.DseBalanceInquiry_FC_Test_003

它找到所需的Event 节点并输出它们的Type 属性,以及它们的一个子元素的script_name 属性。


编辑(回复您的评论):

下面的样式表做你想做的事,我修改了几处:

  • XSL 的命名空间必须为“http://www.w3.org/1999/XSL/Transform
  • 您正在匹配“/”(文档节点)。因此,您必须在树中导航以找到 for-each 的节点,如下所示:TestLog/Event/Event
  • 您的代码包含分号“;”应该删除的
  • 属性必须以“@”(或属性::)为前缀

所有这些都让我相信你从来没有尝试过自己运行这个。

完整的样式表:

<?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></h2>
     <table border="1">
        <tr>
           <th>Type</th>
           <th>Result</th>
        </tr>
           <xsl:for-each select="TestLog/Event/Event[@Result='INFORMATION']">
              <tr>
                 <td>
                    <xsl:value-of select="@Type"/>
                 </td>
                 <td>
                    <xsl:value-of select="@Result"/>
                 </td>
              </tr>
           </xsl:for-each>
     </table>
   </body>
  </html>
</xsl:template>

</xsl:stylesheet>

【讨论】:

  • 我已经编写了这段代码,但我没有得到任何值,我只得到了 hedders。我错过了什么吗? w3.org/1999/XSL/Transform">
  • 编辑您的帖子,而不是发表评论。
  • 感谢 Mathias Muller 提供的信息。我很抱歉没有为您提供适当的数据。我是编码新手,对我必须放置哪些细节有点困惑
  • 不客气。说“谢谢”的 StackOverflow 是通过接受有帮助的答案(标记左侧的勾号)。
  • 一个问题,你能推荐一个工具,当我提供 xml 和 xslt 输入时,它会给我输出吗?
猜你喜欢
  • 2019-04-11
  • 1970-01-01
  • 2013-05-17
  • 2019-05-30
  • 2021-12-22
  • 1970-01-01
  • 2012-10-14
  • 2014-11-15
  • 1970-01-01
相关资源
最近更新 更多