Reading a XML text and placing it in row using XSLT

人走茶凉 提交于 2019-12-11 19:44:49

问题


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 &apos;&apos; ; 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>

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>

回答1:


The following stylesheet does what you want. This line:

<xsl:value-of select="substring-before(substring-after(@Headline,'Step: '),';')"/>

extracts the part of the Headline attribute that is the step number, using a combination of substring-after and substring-before.

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?

Stylesheet

<?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>

Output

<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>


来源:https://stackoverflow.com/questions/20991331/reading-a-xml-text-and-placing-it-in-row-using-xslt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!