xml – XSLT多次替换
发布时间:2020-12-16 23:02:01 所属栏目:百科 来源:网络整理
导读:我有一个 XML文档,类似于 root item_x0034_SOME TEXT/item itemSOME_x0020_TEXT/item itemSOME_x0020_TEXT_x0032_/item/root 我将它导出为HTML,但我在替换转义字符时遇到问题. 我在网上找到了几个模板来进行文本替换,但它们都与此类似: xsl:template name="
我有一个
XML文档,类似于
<root> <item>_x0034_SOME TEXT</item> <item>SOME_x0020_TEXT</item> <item>SOME_x0020_TEXT_x0032_</item> </root> 我将它导出为HTML,但我在替换转义字符时遇到问题. <xsl:template name="replaceString"> <xsl:param name="strOrig"/> <xsl:param name="strSearch"/> <xsl:param name="strReplace"/> <xsl:choose> <xsl:when test="contains($strOrig,$strSearch)"> <xsl:value-of select="substring-before($strOrig,$strSearch)"/> <xsl:value-of select="$strReplace"/> <xsl:call-template name="replaceString"> <xsl:with-param name="strOrig" select="substring-after($strOrig,$strSearch)"/> <xsl:with-param name="strSearch" select="$strSearch"/> <xsl:with-param name="strReplace" select="$strReplace"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$strOrig"/> </xsl:otherwise> </xsl:choose> </xsl:template> 我不知道如何使用它来进行多次替换.我试过这个: <xsl:for-each select="PinnacleSys.PMC.Plugins.PVR.PvrChannelDescriptorWrapper/PinnacleSys.PMC.Plugins.PVR.DVBTPvrChannelDescriptor"> <!--name="<xsl:value-of select="replace(replace(Name,'_x0020_',' '),'_x0034_','3')"/>" --> <!--name="<xsl:value-of select="Name"/>"--> <xsl:variable name="var1" select="Text" /> <xsl:value-of select="replace($FeatureInfo,'Feature=','TESTING')"/> name=" <xsl:call-template name="replaceString"> <xsl:with-param name="strOrig" select="Name"/> <xsl:with-param name="strSearch" select="'_x0020_'"/> <xsl:with-param name="strReplace" select="' '"/> </xsl:call-template> <xsl:call-template name="replaceString"> <xsl:with-param name="strOrig" select="Name"/> <xsl:with-param name="strSearch" select="'_x0030_'"/> <xsl:with-param name="strReplace" select="'0'"/> </xsl:call-template> ..." 但这只是连接几次字符串,每个都有不同的替换. 最好的方法是什么? 我被限制为1.0 XSLT(使用2.0我可以在另一个内部调用一个replace()). 解决方法
对于使用模板进行替换的本机XSLT 1.0解决方案,需要嵌套单独的替换,如此处所示.由于潜在的替换次数,这显然效率不高.下面给出了优化版本.
<xsl:template match="item"> <xsl:copy> <xsl:call-template name="replace-substring"> <xsl:with-param name="original"> <xsl:call-template name="replace-substring"> <xsl:with-param name="original"> <xsl:call-template name="replace-substring"> <xsl:with-param name="original" select="."/> <xsl:with-param name="substring" select="'_x0020_'"/> <xsl:with-param name="replacement" select="' '"/> </xsl:call-template> </xsl:with-param> <xsl:with-param name="substring" select="'_x0032_'"/> <xsl:with-param name="replacement" select="'2'"/> </xsl:call-template> </xsl:with-param> <xsl:with-param name="substring" select="'_x0034_'"/> <xsl:with-param name="replacement" select="'4'"/> </xsl:call-template> </xsl:copy> </xsl:template> <xsl:template name="replace-substring"> <xsl:param name="original"/> <xsl:param name="substring"/> <xsl:param name="replacement" select="''"/> <xsl:choose> <xsl:when test="contains($original,$substring)"> <xsl:value-of select="substring-before($original,$substring)"/> <xsl:copy-of select="$replacement"/> <xsl:call-template name="replace-substring"> <xsl:with-param name="original" select="substring-after($original,$substring)"/> <xsl:with-param name="substring" select="$substring"/> <xsl:with-param name="replacement" select="$replacement"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$original"/> </xsl:otherwise> </xsl:choose> </xsl:template> 由于输出是HTML(XML也可以),_xNNNN_字符串可以转换为它们的十六进制数字字符引用,例如& #xNNNN;.这个模板很有效. <xsl:template match="item"> <xsl:copy> <xsl:call-template name="replaceChars"> <xsl:with-param name="original" select="."/> </xsl:call-template> </xsl:copy> </xsl:template> <xsl:template name="replaceChars"> <xsl:param name="original"/> <xsl:choose> <xsl:when test="contains($original,'_x')"> <xsl:value-of select="substring-before($original,'_x')"/> <xsl:variable name="after" select="substring-after($original,'_x')"/> <xsl:variable name="char" select="substring-before($after,'_')"/> <xsl:value-of select="concat('&#x',$char,';')" disable-output-escaping="yes"/> <xsl:call-template name="replaceChars"> <xsl:with-param name="original" select="substring-after($after,'_')"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$original"/> </xsl:otherwise> </xsl:choose> </xsl:template> 这是结果输出. <root> <item>4SOME TEXT</item> <item>SOME TEXT</item> <item>SOME TEXT2</item> </root> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |