1

输入:

<text>
    Please see the registered mark® .
    Please see the copy right ©.
    Please see the Trade mark™.
</text>

输出:

  <text>
        Please see the registered mark<registeredTrademark></registeredTrademark>.
        Please see the copy right <copyright></copyright>.
        Please see the Trade mark <trademark></trademark>.
  </text>

我需要用上面显示的元素替换所有特殊符号

任何人都可以帮助。

谢谢

4

2 回答 2

2

由于这是 XSLT 1.0,您将不得不使用递归命名模板来依次检查每个字符。

首先,在 XSLT 中创建一种“查找”可能更灵活,您可以在其中指定符号列表和所需的元素名称来替换它们

<lookup:codes>
  <code symbol="®">registeredTrademark</code>
  <code symbol="©">copyright</code>
  <code symbol="™">trademark</code>
</lookup:codes>

('lookup' 命名空间实际上可以命名为任何名称,只要它在 XSLT 中声明即可)。

然后,要访问它,您可以定义一个变量来访问此查找

<xsl:variable name="lookup" select="document('')/*/lookup:codes"/>

而且,要根据符号查找实际代码会做这样的事情(其中 $text)是一个包含您正在检查的文本的变量。

  <xsl:variable name="char" select="substring($text, 1, 1)"/>
  <xsl:variable name="code" select="$lookup/code[@symbol = $char]"/>

所有命名模板都会做的是检查文本的第一个字符,如果它存在于查找中,则将其替换为元素,然后使用文本的剩余部分递归调用模板。

这是完整的 XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:lookup="lookup" exclude-result-prefixes="lookup">
   <xsl:output method="xml" indent="no"/>

   <lookup:codes>
      <code symbol="®">registeredTrademark</code>
      <code symbol="©">copyright</code>
      <code symbol="™">trademark</code>
   </lookup:codes>

   <xsl:variable name="lookup" select="document('')/*/lookup:codes"/>

   <xsl:template match="text[text()]">
      <text>
         <xsl:call-template name="text"/>
      </text>
   </xsl:template>

   <xsl:template name="text">
      <xsl:param name="text" select="text()"/>
      <xsl:variable name="char" select="substring($text, 1, 1)"/>
      <xsl:variable name="code" select="$lookup/code[@symbol = $char]"/>
      <xsl:choose>
         <xsl:when test="$code"><xsl:element name="{$code}" /></xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="$char"/>
         </xsl:otherwise>
      </xsl:choose>
      <xsl:if test="string-length($text) &gt; 1">
         <xsl:call-template name="text">
            <xsl:with-param name="text" select="substring($text, 2, string-length($text) - 1)"/>
         </xsl:call-template>
      </xsl:if>
   </xsl:template>

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

应用于您的示例 XML 时,将输出以下内容

<text> 
    Please see the registered mark<registeredTrademark /> . 
    Please see the copy right <copyright />. 
    Please see the Trade mark<trademark />. 
</text>
于 2012-08-08T09:56:26.420 回答
1

通过避免逐字符递归和使用“最大可能步”递归,这种转换更加有效

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

 <my:reps>
  <r char="®">registeredTrademark</r>
  <r char="©">copyright</r>
  <r char="™">trademark</r>
 </my:reps>

 <xsl:variable name="vReps" select="document('')/*/my:reps/*"/>

 <xsl:template match="text()" name="multReplace">
  <xsl:param name="pText" select="."/>
  <xsl:param name="pReps" select="$vReps"/>

   <xsl:if test="$pText">
     <xsl:variable name="vTarget" select="$pReps[1]/@char"/>
       <xsl:choose>
         <xsl:when test="not($vTarget)">
           <xsl:value-of select="$pText"/>
         </xsl:when>
         <xsl:otherwise>
           <xsl:variable name="vReplacement" select="$pReps[1]"/>

           <xsl:call-template name="multReplace">
            <xsl:with-param name="pText" select=
            "substring-before(concat($pText, $vTarget), $vTarget)"/>
            <xsl:with-param name="pReps" select="$pReps[position() >1]"/>
           </xsl:call-template>

           <xsl:if test="contains($pText, $vTarget)">
             <xsl:element name="{$vReplacement}"/>
             <xsl:call-template name="multReplace">
              <xsl:with-param name="pText" select="substring-after($pText, $vTarget)"/>
              <xsl:with-param name="pReps" select="$pReps"/>
             </xsl:call-template>
           </xsl:if>
         </xsl:otherwise>
       </xsl:choose>
   </xsl:if>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时

<text>
        Please see the registered mark® .
        Please see the copy right ©.
        Please see the Trade mark™.
</text>

生成正确替换的文本

    Please see the registered mark<registeredTrademark/> .
    Please see the copy right <copyright/>.
    Please see the Trade mark<trademark/>.
于 2012-08-08T13:11:57.870 回答