XSL:用另一个角色替换<br/> <br/>

时间:2013-11-12 17:28:32

标签: xslt xpath

我有一个输入xml文档,我正在转换它在节点中有这个内容:

 <misc-item>22 mm<br></br><fraction>7/8</fraction> in.</misc-item>

当我通过选择'misc-item'创建变量时,br和fraction标签会消失。但是,如果我使用'misc-item / br'创建变量并测试它是否找到了br,那么测试似乎有效。

我想做的是做

 '<br></br>' 
进入空间或分号或其他东西,但我没有运气。我试过让'misc-item / br'的兄弟姐妹,但它没有。我检查了'misc-item'的子计数,它是一个。

非常感谢任何帮助。

我查看了被认定为可能的嫌疑人的帖子。我试过这个无济于事:

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

<xsl:template match="br" mode='PageOutput'>
    <xsl:value-of select="' '" />
</xsl:template>

由于我并没有像建议的欺骗那样忽略一个元素,而是代替,这似乎并不完全正确。

1 个答案:

答案 0 :(得分:3)

  

当我通过选择'misc-item'创建变量时,br和fraction标签会消失。但是,如果我使用'misc-item / br'创建变量并测试它是否找到了br,那么测试似乎有效。

创建变量时,您将在变量中存储对misc-item节点的引用。如果您要求value-of该节点,您将只获取文本,并删除元素,但变量仍保留节点本身。

这可能是您需要使用apply-templates代替value-of来解决的问题。一个共同的主题是拥有一个“身份模板”,它基本上按原样复制所有内容,但可以通过提供更具体的模板来覆盖某些节点的不同行为。

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

<!-- replace any br element with a semicolon -->
<xsl:template match="br">;</xsl:template>

您可以使用模式来限制这些模板,以便仅在特定情况下使用

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

<!-- replace any br element with a semicolon -->
<xsl:template match="br" mode="strip-br">;</xsl:template>

现在你可以使用例如。

<xsl:apply-templates select="$miscitem/node()" mode="strip-br" />

而不是<xsl:value-of select="$miscitem"/>来获得你想要的结果。

相关问题