2011-04-13 32 views
0

我有一個xml看起來像這樣,字符串處理:應用模板

<Parent> Running text with marked up entities like 
    <Child>Entity1</Child> 
and, text in the middle too, and 
    <Child> Entity2 </Child> 
</Parent> 

我有渲染父時保留了換行和縮進,同時也應用突出模板每一個孩子標籤。

現在,當我捕獲變量中父標記的內容以在XSL中執行一些字符串處理時,我失去了基礎xml結構,無法將突出顯示模板應用於子項。

鑑於,我不能想出任何其他方式來保留父標記中包含的文本的換行符和縮進。

任何想法?

+0

你是什麼意思的「應用突出顯示模板」?你需要用另一個元素包裝'Child'元素? – 2011-04-13 14:44:54

+0

請給出您的樣本輸入XML,並顯示您當前的XSL,以及您期望的轉換輸出的樣子。 – 2011-04-13 14:51:07

回答

0

這個樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:preserve-space elements="*"/> 
    <xsl:template match="Parent"> 
     <div> 
      <xsl:apply-templates mode="preserve"/> 
     </div> 
    </xsl:template> 
    <xsl:template match="text()" mode="preserve" name="split"> 
     <xsl:param name="pString" select="."/> 
     <xsl:choose> 
      <xsl:when test="contains($pString,'&#xA;')"> 
       <xsl:value-of select="substring-before($pString,'&#xA;')"/> 
       <br/> 
       <xsl:call-template name="split"> 
        <xsl:with-param name="pString" 
        select="substring-after($pString,'&#xA;')"/> 
       </xsl:call-template> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:value-of select="$pString"/> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
    <xsl:template match="Child" mode="preserve"> 
     <b> 
      <xsl:apply-templates mode="preserve"/> 
     </b> 
    </xsl:template> 
</xsl:stylesheet> 

輸出:

<div> Running text with marked up entities like<br/> <b>Entity1</b><br/> and, text in the middle too, and<br/> <b> Entity2 </b><br/></div>

爲渲染:

註明了實體運行的文字像
ENTITY1
和文本中間太,和
實體2


編輯:更好的示例,保留只有空白的文本節點。

+0

@Ajjandro這太棒了!我不知道如何使用'模式'。大。非常感謝! – user706213 2011-04-14 15:40:09

+0

@lwburk高亮模板只是一個模板,它將放在的周圍。 – user706213 2011-04-14 15:42:45

+0

@ user706213:不客氣! – 2011-04-14 15:55:29

0

您還沒有表現出任何代碼,這使得很難告訴你做了什麼錯,但是這將佔到描述的症狀一個常見的錯誤是寫

<xsl:variable name="x"> 
    <xsl:value-of select="some/node/path"/> 
</xsl:variable> 

時,你應該有書面

<xsl:variable name="x" select="some/node/path"/> 

請將來,請不要告訴我們,如果不向我們顯示您的代碼,您的代碼不起作用。