在 xslt 中查找和替换实体 [英] Find and replace entity in xslt

查看:27
本文介绍了在 xslt 中查找和替换实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 xslt 中找到并替换一个实体.我需要在不使用模板匹配的情况下执行此操作.

i need to find and replace an entity in xslt. i need to do this without using template match.

假设我的xml是这样的,

suppose my xml like this,

<root>
  <p>Master&apos;s</p>
  <p><blockFixed type="quotation"><p>let&apos;s</p></blockFixed></p>
   <p>student&apos;s<featureFixed id=1/></p>
   <p><blockFixed type="quotation"><p>nurse&apos;s</p></blockFixed></p>
   <p>Master&apos;s</p>
</root>

我需要将 &apos; 改为 &rsquo; 所以输出应该是这样的,

i need to change &apos; into &rsquo; so output should be like this,

<root>
  <p>Master<apos>&rsquo;</apos>s</p>
  <p><blockFixed type="quotation"><p>let<apos>&rsquo;</apos>s</p></blockFixed></p>
   <p>student<apos>&rsquo;</apos>s<featureFixed id=1/><\p>
   <p><blockFixed type="quotation"><p>nurse<apos>&rsquo;</apos>s</p></blockFixed></p>
   <p>Master&apos;s</p>
</root>

我通过使用模板匹配p使用了replace方法,但之后我无法对其他标签(如blockfixed等)进行任何操作.所以请建议使用简单的 xslt 来做到这一点.

i used the replace method by using template match p but after that i can not able to do any operation on other tags like blockfixed etc,. so pls suggest simple xslt to do this.

我使用了以下 xslt,

i used the following xslt,

  <xsl:template match="p">
    <xsl:copy>
   <xsl:apply-templates select="current()/node()" mode="replace"/>
    </xsl:copy>
  </xsl:template>
     <xsl:template match="text()" mode="replace">
<xsl:call-template name="replace-string">
  <xsl:with-param name="text" select="."/>
  <xsl:with-param name="from">&apos;</xsl:with-param>
  <xsl:with-param name="to" select="'&rsquo;'"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="replace-string">
<xsl:param name="text"/>
<xsl:param name="from"/>
<xsl:param name="to"/>
<xsl:choose>
    <xsl:when test="contains($text, $from)">
    <xsl:variable name="before" select="substring-before($text, $from)"/>
    <xsl:variable name="after" select="substring-after($text, $from)"/>
    <xsl:variable name="prefix" select="concat($before, $to)"/>
    <xsl:copy-of select="$before"/>
    <Apos>
      <xsl:copy-of select="$to"/>
    </Apos>
    <xsl:call-template name="replace-string">
      <xsl:with-param name="text" select="$after"/>
      <xsl:with-param name="from" select="$from"/>
      <xsl:with-param name="to" select="$to"/>
    </xsl:call-template>
   </xsl:when>
  <xsl:otherwise>
    <xsl:copy-of select="$text"/>
  </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

提前致谢.

推荐答案

问题在于您的模板与 p 元素匹配

The problem is with your template matching the p element

<xsl:template match="p">
  <xsl:copy>
    <xsl:apply-templates select="current()/node()" mode="replace"/>
  </xsl:copy>
</xsl:template> 

特别是,您使用了模式.尽管您选择了 p 元素的所有子节点,但您只为 text() 节点提供了一个匹配模板,其模式为 replace.这意味着所有其他元素都不会被选中,因此 blockFixed 将被忽略.

In particular, you use of mode. Although you are selecting all child nodes of the p element, you have only provided a matching template for text() nodes with a mode of replace. This means all other elements won't get picked out, and so blockFixed is ignored.

相反,从 XSLT 中删除与 p 匹配的上述模板,并替换以下行:

Instead, remove the above template matching p from your XSLT, and replace the following line:

<xsl:template match="text()" mode="replace"> 

有了这个

<xsl:template match="p/text()">

这假设您在 XSLT 中构建身份转换.这是 XSLT 的删节版

This assumes you building upon the identity transform in your XSLT. Here is the abridged version of the XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

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

  <xsl:template match="p/text()">
      <xsl:call-template name="replace-string">
          <xsl:with-param name="text" select="."/>
          <xsl:with-param name="from">'</xsl:with-param>
          <xsl:with-param name="to" select="'&amp;rsquo;'"/>
      </xsl:call-template>
  </xsl:template>

  <xsl:template name="replace-string">
      <xsl:param name="text"/>
      <xsl:param name="from"/>
      <xsl:param name="to"/>
      <xsl:choose>
          <xsl:when test="contains($text, $from)">
            <xsl:variable name="before" select="substring-before($text, $from)"/>
            <xsl:variable name="after" select="substring-after($text, $from)"/>
            <xsl:variable name="prefix" select="concat($before, $to)"/>
            <xsl:copy-of select="$before"/>
            <Apos>
                <xsl:value-of select="$to" disable-output-escaping="yes"/>
            </Apos>
            <xsl:call-template name="replace-string">
                <xsl:with-param name="text" select="$after"/>
                <xsl:with-param name="from" select="$from"/>
                <xsl:with-param name="to" select="$to"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:copy-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
</xsl:stylesheet>

这篇关于在 xslt 中查找和替换实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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