关于编写xslt文件的建议

时间:2011-10-03 16:42:41

标签: c# .net xml xslt xpath

我有xml文件如下。

<?xml-stylesheet type='text/xsl' href='AdditionalLogInfo.xsl'?>
<Logs>
  <Log TestName="EwireDepositTests" Date="Oct 3 11">
    <Item>
      <Message>Name: blabla</Message>
    </Item>
    <Item>
      <Message>Test Status: Failed</Message>
    </Item>
    <Item>
      <Message>
      </Message>
    </Item>
    <Item>
      <Message>[[Logs]]</Message>
    </Item>
    <Item>
      <Message>[ccpayment]</Message>
    </Item>
    <Item>
       <Exception>blabla couldn't be found in the database</Exception>
    </Item>
    <Item>
      <Message>
      </Message>
    </Item>
    <Item>
      <Message>[logging]</Message>
    </Item>
    <Item>
       <Exception>couldn't be found in the database</Exception>
    </Item>
  </Log>
</Logs>

我遇到的问题是写一个xslt文件,这样如果Logs / Log / Item中有Exception节点,我可以用红线查看好看的html页面,如果有Message节点,则可以查看绿线。

1 个答案:

答案 0 :(得分:2)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"
        doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
        doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"/>

  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <body>
        <ul>
          <xsl:apply-templates select="//Item/*[normalize-space(.)]"/>
        </ul>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="Message">
    <li style="color:green" xmlns="http://www.w3.org/1999/xhtml">
      <xsl:value-of select="."/>
    </li>
  </xsl:template>

  <xsl:template match="Exception">
    <li style="color:red" xmlns="http://www.w3.org/1999/xhtml">
      <xsl:value-of select="."/>
    </li>
  </xsl:template>

</xsl:stylesheet>

输出:

enter image description here