2011-04-19 104 views
0

其實我看,我可以添加名字空間。因爲我非常接近我期望看到的輸出。首先代碼:添加命名空間前綴子節點問題

XML:

<helptext> 
    <h6>General configuration options.</h6> 
    <h2>Changing not yet supported.</h2> 
    <p>this is a <b>paragraph</b><br/>this is a new line</p> 
</helptext> 

XSL:

<xsl:template name="transformHelptext"> 
    <xsl:for-each select="./child::*"> 
     <xsl:element name="ht:{local-name()}"> 
      <xsl:choose> 
       <xsl:when test="count(./child::*)>0"> 
        <xsl:call-template name="transformHelptext"/> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:copy-of select="."/> 
       </xsl:otherwise> 
      </xsl:choose> 
     </xsl:element> 
    </xsl:for-each> 
</xsl:template> 

到目前爲止好。 <h6>..</h6><h2>...</h2>行沒有問題。 但第三行有一個子節點,它是一個<b>。不知何故,「段落」是唯一顯示的文字,對於這一行。我在choose聲明中有錯誤。但我無法弄清楚。

由於

PS:HT命名空間在XSL樣式表的標記定義和它是 '的xmlns:HT = 「http://www.w3.org/1999/xhtml」'

PS:我嘗試做的是,使之可以應用HTML標籤類似這樣的替代,在我的具體XML節點風格

回答

1

也許一些:

<xsl:template name="transformHelptext"> 
    <xsl:apply-templates select="@*|node()" /> 
</xsl:template> 

<xsl:template match="*" > 
    <xsl:element name="ht:{local-name(.)}"> 
     <xsl:apply-templates select="@*|node()" /> 
    </xsl:element> 
</xsl:template> 

<xsl:template match="@*|text()" > 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()" /> 
    </xsl:copy> 
</xsl:template> 

裏面的「transformHelptext」 templeate,選擇所有ATTR同步和節點並將模板應用於它們。

第二模板相匹配元素節點,並且改變的命名空間。第三個模板匹配屬性和文本節點,並創建一個副本。

+0

謝謝您的回覆。但它確實顯示純文本,除非瀏覽器是Firefox。 – savruk 2011-04-19 09:54:49

+0

@savruk也許XML頭缺少 – Stephan 2011-04-19 10:14:12

+0

其實我沒有得到它與您的代碼工作,謝謝 – savruk 2011-04-19 10:15:05

1

輸入XML:

<?xml version="1.0" encoding="UTF-8"?> 
<helptext> 
    <h6>General configuration options.</h6> 
    <h2>Changing not yet supported.</h2> 
    <p>this is a <b>paragraph</b><br/>this is a new line</p> 
</helptext> 

XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="*|@*"> 
    <xsl:element name="ht:{local-name()}" namespace="http://www.w3.org/1999/xhtml"> 
     <xsl:copy-of select="@*"/> 
     <xsl:apply-templates/> 
    </xsl:element> 
</xsl:template> 

輸出XML:

<?xml version="1.0" encoding="UTF-8"?> 
<ht:helptext xmlns:ht="http://www.w3.org/1999/xhtml"> 
    <ht:h6>General configuration options.</ht:h6> 
    <ht:h2>Changing not yet supported.</ht:h2> 
    <ht:p> 
     this is a 
     <ht:b>paragraph</ht:b> 
     <ht:br /> 
     this is a new line 
    </ht:p> 
</ht:helptext> 

討論: 儘可能避免使用<xsl:for-each>,因爲它會降低處理器速度。

+0

+1。 – savruk 2011-04-19 10:51:27