2011-07-14 34 views
2

我从我写的程序中获得XML中的以下调试转储。我想把这个格式化为一个表格,并且嵌套了子例外(我还没有得到这个)。 我是XSL(T)的新手,所以请耐心等待。 我有三个文件debug.xml,debug.xsl和table.css。 underneat用于显示没有父级的子节点的XML

XML文件

<?xml version="1.0" encoding="ISO8859-1" ?> 
<?xml-stylesheet type="text/xsl" href="debug.xsl"?> 
<Root> 
<Exception> 
    <Type>DuplicateKeyPairException1</Type> 
    <Message>Duplicate in the key/section pair found in the file. - [THRUSTERS/NRTHRUSTERS] already has a value.</Message> 
    <Source>WindowsFormsApplication1</Source> 
    <StackTrace> at IniParser..ctor(String iniPath) in C:\Programming\CSharp\VisualSourceSafeInterface\Test2\WindowsFormsApplication1\WindowsFormsApplication1\IniParser.cs:line 110 
    at CIniFile.Open(String sFileName) in C:\Programming\CSharp\VisualSourceSafeInterface\Test2\WindowsFormsApplication1\WindowsFormsApplication1\FileParser.cs:line 115 
    at CIniFile..ctor(String sFilename) in C:\Programming\CSharp\VisualSourceSafeInterface\Test2\WindowsFormsApplication1\WindowsFormsApplication1\FileParser.cs:line 109 
    at ScriptHost.RequestDocument(String qualifiedPath, enumFileType filetype) in C:\Programming\CSharp\VisualSourceSafeInterface\Test2\WindowsFormsApplication1\WindowsFormsApplication1\ScriptHost.cs:line 55</StackTrace> 
    <TargetSite>Void .ctor(System.String)</TargetSite> 
    <Data> 
    <VSSFullPath>c:\vss\ERROR\vss\SRCSAFE.INI</VSSFullPath> 
    <VSSFilename>xxx.ini</VSSFilename> 
    </Data> 
<Exception> 
    <Type>System.ArgumentException</Type> 
    <Message>Item has already been added. Key in dictionary: 'IniParser+SectionPair' Key being added: 'IniParser+SectionPair'</Message> 
    <Source>mscorlib</Source> 
    <StackTrace> at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add) 
    at System.Collections.Hashtable.Add(Object key, Object value) 
    at IniParser..ctor(String iniPath) in C:\Programming\CSharp\VisualSourceSafeInterface\Test2\WindowsFormsApplication1\WindowsFormsApplication1\IniParser.cs:line 95</StackTrace> 
    <TargetSite>Void Insert(System.Object, System.Object, Boolean)</TargetSite> 
</Exception> 
</Exception> 
<Exception> 
    <Type>DuplicateKeyPairException2</Type> 
    <Message>Duplicate in the key/section pair found in the file. - [THRUSTERS/NRTHRUSTERS] already has a value.</Message> 
    <Source>WindowsFormsApplication1</Source> 
    <StackTrace> at IniParser..ctor(String iniPath) in C:\Programming\CSharp\VisualSourceSafeInterface\Test2\WindowsFormsApplication1\WindowsFormsApplication1\IniParser.cs:line 110 
    at CIniFile.Open(String sFileName) in C:\Programming\CSharp\VisualSourceSafeInterface\Test2\WindowsFormsApplication1\WindowsFormsApplication1\FileParser.cs:line 115 
    at CIniFile..ctor(String sFilename) in C:\Programming\CSharp\VisualSourceSafeInterface\Test2\WindowsFormsApplication1\WindowsFormsApplication1\FileParser.cs:line 109 
    at ScriptHost.RequestDocument(String qualifiedPath, enumFileType filetype) in C:\Programming\CSharp\VisualSourceSafeInterface\Test2\WindowsFormsApplication1\WindowsFormsApplication1\ScriptHost.cs:line 55</StackTrace> 
    <TargetSite>Void .ctor(System.String)</TargetSite> 
    <Data> 
    <VSSFullPath>c:\vss\ERROR\vss\SRCSAFE.INI</VSSFullPath> 
    <VSSFilename>xxx.ini</VSSFilename> 
    </Data> 
</Exception> 
</Root> 

XSL文件

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


    <xsl:template match="/"> 
    <html> 
    <head> 
     <title>Cascading Style Sheet</title> 
    <link rel="stylesheet" type="text/css" href="table.css" title="Style"/> 
    </head> 
    <body> 
      <xsl:for-each select="Root"> 
       <xsl:element name="table"> 
        <xsl:attribute name="border">1</xsl:attribute> 
         <xsl:for-each select="*"> 
          <xsl:apply-templates select="."/> 
         </xsl:for-each> 
       </xsl:element> 
      </xsl:for-each> 
    </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="Root/Exception"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
      <xsl:attribute name="colspan">2</xsl:attribute> 
      <xsl:attribute name="class">Exception</xsl:attribute> 
      Exception 
      </xsl:element> 
     </xsl:element> 
     <xsl:apply-templates select="Type"/> 
     <xsl:apply-templates select="Message"/> 
     <xsl:apply-templates select="VSSFullPath"/> 
     <xsl:apply-templates select="VSSFilename"/> 
     <xsl:apply-templates select="Source"/> 
     <xsl:apply-templates select="TargetSite"/> 
     <xsl:apply-templates select="StackTrace"/> 
     <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="Exception/Exception"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
      <xsl:attribute name="colspan">2</xsl:attribute> 
      <xsl:attribute name="class">Exception</xsl:attribute> 
      SUB Exception 
      </xsl:element> 
     </xsl:element> 
     <xsl:apply-templates select="Type"/> 
     <xsl:apply-templates select="Message"/> 
     <xsl:apply-templates select="Data"/> 
     <xsl:apply-templates select="Source"/> 
     <xsl:apply-templates select="TargetSite"/> 
     <xsl:apply-templates select="StackTrace"/> 
     <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="Type"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       Type 
      </xsl:element> 
      <xsl:element name="td"> 
       <xsl:value-of select="." /> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="Message"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       Message 
      </xsl:element> 
      <xsl:element name="td"> 
       <xsl:value-of select="." /> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="Source"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       Source 
      </xsl:element> 
      <xsl:element name="td"> 
       <xsl:value-of select="." /> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="TargetSite"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       TargetSite 
      </xsl:element> 
      <xsl:element name="td"> 
       <xsl:value-of select="." /> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="StackTrace"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       StackTrace 
      </xsl:element> 
      <xsl:element name="td"> 
       <xsl:value-of select="." /> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 

    <!--Print data in a new table inside the cell--> 
    <xsl:template match="Data"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       Data 
      </xsl:element> 
      <xsl:element name="td"> 
       <xsl:element name="table"> 
       <xsl:attribute name="border">1</xsl:attribute> 
        <xsl:for-each select="*"> 
         <xsl:element name="tr"> 
          <xsl:element name="th"> 
           <xsl:value-of select="local-name()"/> 
          </xsl:element> 
          <xsl:element name="td"> 
            <xsl:value-of select="."/> 
          </xsl:element> 
         </xsl:element> 
        </xsl:for-each> 
       </xsl:element> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="VSSFilename"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       VSS File 
      </xsl:element> 
      <xsl:element name="td"> 
       <xsl:value-of select="." /> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 

       <!--Sub Exceptions 
       <xsl:for-each select="child::Exception"> 
        <xsl:element name="tr"> 
         <xsl:element name="th"> 
          Nested Exception 
         </xsl:element> 
         <xsl:element name="td"> 
          <xsl:value-of select="." /> 
         </xsl:element> 
        </xsl:element> 
       </xsl:for-each>--> 
</xsl:stylesheet> 

CSS-文件显示

table 
{text-align: left; 
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif ; 
font-weight: normal; 
font-size: 11px; 
color: #fff; 
width: 420px; 
background-color: #666; 
border: 0px; 
border-collapse: collapse; 
border-spacing: 0px;} 

th { 
    font: bold 11px "Trebuchet MS", Verdana, Arial, Helvetica, 
    sans-serif; 
    color: #6D929B; 
    border-right: 1px solid #C1DAD7; 
    border-bottom: 1px solid #C1DAD7; 
    border-top: 1px solid #C1DAD7; 
    letter-spacing: 2px; 
    text-transform: uppercase; 
    text-align: left; 
    padding: 6px 6px 6px 12px; 
    background: #CAE8EA url(images/bg_header.jpg) no-repeat; 
} 


th.Exception 
{ 
    border-right: 1px solid #D7DAC1; 
    border-bottom: 1px solid #D7DAC1; 
    border-top: 1px solid #D7DAC1; 
    border-top: 0; 
    border-left: 0; 
    border-right: 1px solid #D7DAC1; 
    background: #EAE8CA; 
} 

td { 
font-weight: bold; 
    border-right: 1px solid #C1DAD7; 
    border-bottom: 1px solid #C1DAD7; 
    background: #fff; 
    padding: 6px 6px 6px 12px; 
    color: #6D929B; 
} 


td.alt { 
    background: #F5FAFA; 
    color: #B4AA9D; 
} 

我的结果显示异常,但它也显示了样式第二次子节点 ,所以我在列表中得到了一组双重异常子。 PS这是正在进行中的工作,如果您发现任何问题,我真的很感激,如果您给我提示或指示什么是错误或不正确的做法。 我明白为什么会发生,因为它运行所有模板,但不知道如何阻止它发生。

在此先感谢您的帮助。

+0

+1对于详细的问题和努力。 –

回答

1

我的结果显示异常,但它也显示了子节点的样式的第二次,所以我得到一个双套异常儿童在列表中。

的不必要的重复被的xsl:apply-templates一个错误的用法在XSLT引起的。

通常情况下,在同一个序列构造,您可以使用xsl:apply-templatesselect或不select,不能同时

例如,下面的代码:

<xsl:apply-templates select="Type"/> 
    <xsl:apply-templates select="Message"/> 
    <xsl:apply-templates select="VSSFullPath"/> 
    <xsl:apply-templates select="VSSFilename"/> 
    <xsl:apply-templates select="Source"/> 
    <xsl:apply-templates select="TargetSite"/> 
    <xsl:apply-templates select="StackTrace"/> 
    <xsl:apply-templates /> 

应该与改变:

<xsl:apply-templates /> 

该指令,没有select你默认应用模板上下文节点的所有子

要获得想要的结果,您需要在模板Root/ExceptionException/Exception模板中仅使用<xsl:apply-templates />。这是因为很显然你希望模板与所有的孩子结盟。


PS:通过删除不需要的xsl:for-each,可以真正改善您的代码。例如,这是你(让我说惊人)迭代:

<xsl:for-each select="*"> 
    <xsl:apply-templates select="."/> 
    </xsl:for-each> 

是逻辑上等同于:

<xsl:apply-templates /> 
+0

'举例来说,这是你的(让我说惊人的)迭代:' - 他他很棒的评论。感谢您的帮助。它正在工作。 – schultz

+0

@Schultz,欢迎来到SO。确保[接受答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)这是最有用的解决您的问题。 –

+0

yep不知道那个。现在已被接受。 – schultz

0

您的解决方案只是修改:

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


    <xsl:template match="/"> 
     <html> 
      <head> 
       <title>Cascading Style Sheet</title> 
       <link rel="stylesheet" type="text/css" href="table.css" title="Style"/> 
      </head> 
      <body> 
       <table border="1"> 
        <xsl:apply-templates select="Root/Exception"/> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="Root/Exception"> 
     <tr> 
      <th colspan="2" class="Exception">Exception</th> 
     </tr> 
     <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="Exception/Exception"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       <xsl:attribute name="colspan">2</xsl:attribute> 
       <xsl:attribute name="class">Exception</xsl:attribute> 
       SUB Exception 
      </xsl:element> 
     </xsl:element> 
     <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="Type"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       Type 
      </xsl:element> 
      <xsl:element name="td"> 
       <xsl:value-of select="." /> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="Message"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       Message 
      </xsl:element> 
      <xsl:element name="td"> 
       <xsl:value-of select="." /> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="Source"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       Source 
      </xsl:element> 
      <xsl:element name="td"> 
       <xsl:value-of select="." /> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="TargetSite"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       TargetSite 
      </xsl:element> 
      <xsl:element name="td"> 
       <xsl:value-of select="." /> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="StackTrace"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       StackTrace 
      </xsl:element> 
      <xsl:element name="td"> 
       <xsl:value-of select="." /> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 

    <!--Print data in a new table inside the cell--> 
    <xsl:template match="Data"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       Data 
      </xsl:element> 
      <xsl:element name="td"> 
       <xsl:element name="table"> 
        <xsl:attribute name="border">1</xsl:attribute> 
        <xsl:for-each select="*"> 
         <xsl:element name="tr"> 
          <xsl:element name="th"> 
           <xsl:value-of select="local-name()"/> 
          </xsl:element> 
          <xsl:element name="td"> 
           <xsl:value-of select="."/> 
          </xsl:element> 
         </xsl:element> 
        </xsl:for-each> 
       </xsl:element> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="VSSFilename"> 
     <xsl:element name="tr"> 
      <xsl:element name="th"> 
       VSS File 
      </xsl:element> 
      <xsl:element name="td"> 
       <xsl:value-of select="." /> 
      </xsl:element> 
     </xsl:element> 
    </xsl:template> 

    <!--Sub Exceptions 
       <xsl:for-each select="child::Exception"> 
        <xsl:element name="tr"> 
         <xsl:element name="th"> 
          Nested Exception 
         </xsl:element> 
         <xsl:element name="td"> 
          <xsl:value-of select="." /> 
         </xsl:element> 
        </xsl:element> 
       </xsl:for-each>--> 
</xsl:stylesheet> 

你也可以简单地写

<table border="1"> 

<xsl:element name="table"> 
    <xsl:attribute name="border">1</xsl:attribute> 

以提高可读性。

+0

由于我在答案中解释的原因,此代码仍将显示_subexception_部分输出的重复项。 –

+0

@empo,对。忘记删除'Exception/Exception' –

+0

@polishchuk,谢谢你对的信息我将不得不坚持使用 kind。 – schultz

1

审查样式表无法抗拒。

这里53行与原来的。

干杯。

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

    <xsl:template match="/"> 
     <html> 
      <head> 
       <title>Cascading Style Sheet</title> 
       <link rel="stylesheet" type="text/css" href="table.css" title="Style"/> 
      </head> 
      <body> 
       <table border="1"> 
        <xsl:apply-templates /> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="Exception[parent::Root]"> 
     <tr> 
      <th colspan="2" class="Exception">Exception</th> 
     </tr> 
     <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="Exception[parent::Exception]"> 
     <tr> 
      <th colspan="2" class="Exception">SUB Exception</th> 
     </tr> 
     <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="Data"> 
     <tr> 
      <th>Data</th> 
      <td> 
       <table border="1"> 
         <xsl:apply-templates/> 
       </table> 
      </td> 
     </tr> 
    </xsl:template> 

    <xsl:template match="Type|Message|Source|TargetSite|StackTrace|VSSFilename|VSSFullPath"> 
     <tr> 
      <th> 
       <xsl:value-of select="name()"/> 
      </th> 
      <td> 
       <xsl:value-of select="."/> 
      </td> 
     </tr> 
    </xsl:template> 

</xsl:stylesheet> 
相关问题