使用禁用输出转义转换 XML 混合节点 [英] Transforming XML mixed nodes with disable-output-escaping

查看:33
本文介绍了使用禁用输出转义转换 XML 混合节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这个问题的变体已经发布,但我找不到任何解决基本情况的变体.我认为对问题的最简单版本有一个规范的答案会很好.这个问题假设 xslt 1.0.

我有一个包含混合节点的 XML 文档,例如:

<前><段落>这是一些<bold>bold</bold>的文本.这是一些被<italic>斜体化的.</italic>

我通常会使用如下所示的转换:

<前><xsl:template match="bold"><b><xsl:apply-templates/></b></xsl:模板><xsl:template match="斜体"><i><xsl:apply-templates/></i></xsl:模板><xsl:template match="段落"><p><xsl:apply-templates/></p></xsl:模板>

这很好用,直到我想使用 disable-output-escaping="yes",这是 xsl:value-of 的一个属性.有没有办法选择混合节点的文本部分,我可以应用独立于嵌入节点的值?

这当然行不通,因为我会丢失子节点:

<前><xsl:template match="段落"><p><xsl:value-of select="."disable-output-escaping="yes"/></p></xsl:模板>

我知道我正在尝试这样做的事实可能代表了我处理 XML 的方式中的一个固有问题,但是大部分 XML 是由(受信任的)用户输入相当天真地生成的,我正在尝试避免在 XML->XSLT->HTML 表单之间添加大量额外的处理代码(如果可能).

解决方案

如果我理解正确,您希望文本节点作为文字文本出现 (disable-output-escaping="yes"),但其余的转换应该正常工作( 等)

模板模式可以提供帮助:

<xsl:template match="bold" mode="literal"><b><xsl:apply-templates mode="literal"/></b></xsl:模板><xsl:template match="italic" mode="literal"><i><xsl:apply-templates mode="literal"/></i></xsl:模板><xsl:template match="text()" mode="literal"><xsl:value-of select="."disable-output-escaping="yes"/></xsl:模板><!-- 普通模板(在不使用模板模式时调用)--><xsl:template match="bold"><b><xsl:apply-templates/></b></xsl:模板><xsl:template match="斜体"><i><xsl:apply-templates/></i></xsl:模板></xsl:stylesheet>

Variations on this question have been posted, but I couldn't find any that address the base case. I thought it would be good to have a canonical answer to the simplest version of the problem. This question assumes xslt 1.0.

I have an XML document that contains mixed nodes, e.g.:

<paragraph>
     This is some text that is <bold>bold</bold> 
     and this is some that is <italic>italicized.</italic>
</paragraph>

I would typically use a transformation that looks something like this:

<xsl:template match="bold">
    <b><xsl:apply-templates/></b>
</xsl:template>
<xsl:template match="italic">
    <i><xsl:apply-templates/></i>
</xsl:template>
<xsl:template match="paragraph">
    <p><xsl:apply-templates/></p>
</xsl:template>

which works great until I want to use disable-output-escaping="yes", which is an attribute of xsl:value-of. Is there a way to select the text-portion of the mixed node to which I can apply the value-of independent of the embedded nodes?

This, of course, doesn't work because I would lose the child nodes:

<xsl:template match="paragraph">
    <p><xsl:value-of select="." disable-output-escaping="yes"/></p>
</xsl:template>

I know the fact I am trying to do this probably represents an inherent problem in the way I am handling the XML, but much of the XML is being fairly-naively generated by (trusted) user input, and I am trying to avoid a lot of extra processing code between the XML->XSLT->HTML form (if possible).

解决方案

If I understand you right, you want text nodes to come out as literal text (disable-output-escaping="yes"), but the rest of the transformation should work normally (<bold> to <b> etc.)

Template modes can help:

<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
  <xsl:output method="xml" encoding="utf-8" omit-xml-declaration="yes" />

  <xsl:template match="paragraph">
    <p>
      <xsl:apply-templates mode="literal" />
    </p>
  </xsl:template>

  <!-- literal templates (invoked in literal mode) -->
  <xsl:template match="bold" mode="literal">
    <b><xsl:apply-templates mode="literal"/></b>
  </xsl:template>
  <xsl:template match="italic" mode="literal">
    <i><xsl:apply-templates mode="literal"/></i>
  </xsl:template>
  <xsl:template match="text()" mode="literal">
    <xsl:value-of select="." disable-output-escaping="yes" />
  </xsl:template>

  <!-- normal templates (invoked when you don't use a template mode) -->
  <xsl:template match="bold">
    <b><xsl:apply-templates /></b>
  </xsl:template>
  <xsl:template match="italic">
    <i><xsl:apply-templates /></i>
  </xsl:template>

</xsl:stylesheet>

这篇关于使用禁用输出转义转换 XML 混合节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆