2013-08-23 144 views
1

我正在編寫用於在系統中預覽xml的通用xsl樣式表,它大部分工作正常,但在某些情況下,它會從標記中跳過span.label和span.value。XSLT缺少一些標籤

因此,如果有標籤只有文本的孩子 - - 它的工作。 (a > b+c

如果第一級只有文本標籤,則只會輸出文本內容,省略標籤名稱。 (a

此外,如果有標籤有一個孩子有幾個孩子的文本 - 它將省略第一級標籤名稱,顯示第二級標籤名稱,並只顯示第三級別的文本內容。 (a > b > c + d + e

這裏是XSLT:

<?xml version='1.0'?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="/"> 
    <html> 
    <head> 
    <title>Preview</title> 
    <meta charset="utf-8" />  
    <style> 
       .level { line-heigth: 20px; } 
       .label { width: 150px; display:inline-block; 
         background-color:#eee; margin-right:10px; 
         margin-top:5px; padding:5px; vertical-align:top; } 
       .value { display:inline-block; vertical-align:top; 
         padding: 5px; margin-top:5px;}    
    </style> 
    </head> 
    <body> 
     <div class='level'> 
       <xsl:apply-templates /> 
      </div> 
    </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="*" > 
     <span class='label'><xsl:value-of select ="local-name(.)"/><xslt:text>:</xslt:text></span> 
     <span class='value'><xsl:value-of select="text()" /></span> 
     <xsl:if test="attribute::*"> 
     <br /> 
     <span class="label"> attributes: 
         <xsl:number value="count(attribute::*)" format="1"/></span> 
     <span class="value"> 
         <xsl:for-each select="attribute::*"> 
      <xsl:value-of select="local-name()" /> 
         <xslt:text> : </xslt:text> 
         <xsl:value-of select="." /> 
      </xsl:for-each> 
     </span> 
     </xsl:if> 
     <xsl:for-each select="*"> 
      <div class='level'> 
       <xsl:apply-templates /> 
      </div> 
     </xsl:for-each> 
     <br /> 
    </xsl:template> 
</xsl:stylesheet> 

回答

1

很難沒有樣品輸入/輸出的說法,但我認爲這個問題是在這裏:

<xsl:for-each select="*"> 
     <div class='level'> 
      <xsl:apply-templates /> 
     </div> 
    </xsl:for-each> 

你遍歷每個孩子元素(select="*")並且不輸出任何關於它的信息。

嘗試刪除xsl:for-each並只做一個<xsl:apply-templates select="*"/>。你也必須移動div

也許是這樣的:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="/"> 
     <html> 
      <head> 
       <title>Preview</title> 
       <meta charset="utf-8" />  
       <style> 
        .level { line-heigth: 20px; } 
        .label { width: 150px; display:inline-block; 
        background-color:#eee; margin-right:10px; 
        margin-top:5px; padding:5px; vertical-align:top; } 
        .value { display:inline-block; vertical-align:top; 
        padding: 5px; margin-top:5px;}    
       </style> 
      </head> 
      <body> 
       <xsl:apply-templates /> 
      </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="*" > 
     <div class='level'> 
      <span class='label'><xsl:value-of select ="local-name(.)"/><xsl:text>:</xsl:text></span> 
      <span class='value'><xsl:value-of select="text()" /></span> 
      <xsl:if test="@*"> 
       <br /> 
       <span class="label"> attributes: 
         <xsl:number value="count(@*)" format="1"/></span> 
       <span class="value"> 
        <xsl:for-each select="@*"> 
         <xsl:value-of select="local-name()" /> 
         <xsl:text> : </xsl:text> 
         <xsl:value-of select="." /> 
        </xsl:for-each> 
       </span> 
      </xsl:if> 
      <br /> 
      <xsl:apply-templates select="*"/> 
     </div> 
    </xsl:template> 
</xsl:stylesheet> 

(我也全部換成attribute::軸與abbreviated syntax@

+0

它的工作,謝謝! – vittore