2011-04-13 53 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"/> 

请将来,请不要告诉我们,如果不向我们显示您的代码,您的代码不起作用。