Xml 使用XSLT删除换行符和断开的实体

Xml 使用XSLT删除换行符和断开的实体,xml,xslt,xslt-1.0,Xml,Xslt,Xslt 1.0,“我的XML”是从web表单生成的,一些用户正在插入换行符和字符,这些字符将转换为换行符\n和断开的实体,如&;amp 我正在使用一些变量来转换和删除坏字符,但我不知道如何去掉这些类型的字符 下面是我用来转换或去除其他坏字符的方法。 如果需要查看整个XSL,请告诉我。 XML中的文本包含如下内容: <Office_photos>bn_1.jpg: Showing a little Red Sox Pride!&#13;\nLeft to right: T

“我的XML”是从web表单生成的,一些用户正在插入换行符和字符,这些字符将转换为换行符
\n
和断开的实体,如
&;amp

我正在使用一些变量来转换和删除坏字符,但我不知道如何去掉这些类型的字符

下面是我用来转换或去除其他坏字符的方法。 如果需要查看整个XSL,请告诉我。



XML中的文本包含如下内容:

<Office_photos>bn_1.jpg: Showing a little Red Sox Pride!&#13;\nLeft to right: 
 Tessa Michelle Summers, \nJulie Gross, Alexis Drzewiecki</Office_photos>
bn_1.jpg:表现出一点红袜队的自豪感
\从左到右:
泰莎·米歇尔·萨默斯、朱利·格罗斯、亚历克西斯·德泽维茨基

我试图去掉数据中的
\n
字符

,正如Lingamurthy CS在注释中解释的那样
\n
在XML中不作为单个字符处理。它被简单地解析为两个字符,无需任何特殊处理

如果您确实希望更改,那么在XSLT1.0中,您需要使用递归模板替换文本(XSLT2.0有替换函数,XSLT1.0没有)

在Stackoverflow上快速搜索可以在以下位置找到一个这样的模板:

叫这个,而不是做这个

<xsl:value-of select="translate(normalize-space(Office_photos), $linebreaks, $nolinebreaks)"/>

你会这么做的

  <xsl:call-template name="string-replace-all">
     <xsl:with-param name="text" select="Office_photos" />
     <xsl:with-param name="replace" select="$linebreaks" />
     <xsl:with-param name="by" select="$nolinebreaks" /> 
  </xsl:call-template>

试试这个XSLT

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

   <xsl:variable name="linebreaks" select="'\n'" />
   <xsl:variable name="nolinebreaks" select="' '" />

   <xsl:template match="/">
      <xsl:call-template name="string-replace-all">
         <xsl:with-param name="text" select="Office_photos" />
         <xsl:with-param name="replace" select="$linebreaks" />
         <xsl:with-param name="by" select="$nolinebreaks" /> 
      </xsl:call-template>
   </xsl:template>

   <xsl:template name="string-replace-all">
     <xsl:param name="text" />
     <xsl:param name="replace" />
     <xsl:param name="by" />
     <xsl:choose>
       <xsl:when test="contains($text, $replace)">
         <xsl:value-of select="substring-before($text,$replace)" />
         <xsl:value-of select="$by" />
         <xsl:call-template name="string-replace-all">
           <xsl:with-param name="text" select="substring-after($text,$replace)" />
           <xsl:with-param name="replace" select="$replace" />
           <xsl:with-param name="by" select="$by" />
         </xsl:call-template>
       </xsl:when>
       <xsl:otherwise>
         <xsl:value-of select="$text" />
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>
</xsl:stylesheet>


(感谢创建替换模板的马克·埃利奥特)

我不确定从大写到小写的翻译如何解决这个问题。是否有要保留的字符列表?或者相反,是否有要丢弃的字符列表?我不需要转换,变量正在转换字母字符或从文本中删除“,”和“.”。我需要的是删除
\n
字符。变量忽略“n”,只看到斜杠“\”。和变量只查看实际实体
&但忽略损坏的
&;amp如果不需要转换,则不需要。你的代码表明你做到了。你说“这是我用来转换或去除其他坏字符的方法。”但你不是这么做的。无论如何,您还没有回答我的问题。我遇到的问题不是转换变量。这是有效的。我在XSLT中做了不止一件事。我把它放在这里只是为了说明它不适合剥离
\n
字符。我需要帮助摆脱断线和断裂的实体。是的,这个XSLT在另一个页面上有介绍,但这是一个单独的问题。很久以前有人告诉我,单独的问题应该放在单独的页面上。\n在XML中不是一个字符,而是两个字符,一个反斜杠(“\”)和一个“n”。您应该使用替换函数从数据中删除“\n”。
  <xsl:call-template name="string-replace-all">
     <xsl:with-param name="text" select="Office_photos" />
     <xsl:with-param name="replace" select="$linebreaks" />
     <xsl:with-param name="by" select="$nolinebreaks" /> 
  </xsl:call-template>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output omit-xml-declaration="yes" indent="yes" />

   <xsl:variable name="linebreaks" select="'\n'" />
   <xsl:variable name="nolinebreaks" select="' '" />

   <xsl:template match="/">
      <xsl:call-template name="string-replace-all">
         <xsl:with-param name="text" select="Office_photos" />
         <xsl:with-param name="replace" select="$linebreaks" />
         <xsl:with-param name="by" select="$nolinebreaks" /> 
      </xsl:call-template>
   </xsl:template>

   <xsl:template name="string-replace-all">
     <xsl:param name="text" />
     <xsl:param name="replace" />
     <xsl:param name="by" />
     <xsl:choose>
       <xsl:when test="contains($text, $replace)">
         <xsl:value-of select="substring-before($text,$replace)" />
         <xsl:value-of select="$by" />
         <xsl:call-template name="string-replace-all">
           <xsl:with-param name="text" select="substring-after($text,$replace)" />
           <xsl:with-param name="replace" select="$replace" />
           <xsl:with-param name="by" select="$by" />
         </xsl:call-template>
       </xsl:when>
       <xsl:otherwise>
         <xsl:value-of select="$text" />
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>
</xsl:stylesheet>