我有一个XML文档,类似于
<root>
<item>_x0034_SOME TEXT</item>
<item>SOME_x0020_TEXT</item>
<item>SOME_x0020_TEXT_x0032_</item>
</root>
Run Code Online (Sandbox Code Playgroud)
我将它导出为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>
Run Code Online (Sandbox Code Playgroud)
我不知道如何使用它来进行多次替换.我试过这个:
<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>
..."
Run Code Online (Sandbox Code Playgroud)
但这只是连接几次字符串,每个都有不同的替换.我也研究了变量; 如果我可以将模板调用的结果分配给变量,我可以得到一个肮脏但有效的解决方案,这对我来说已经足够了.但是,我无法也不知道是否可能.
最好的方法是什么?
我被限制为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>
Run Code Online (Sandbox Code Playgroud)
由于输出是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>
Run Code Online (Sandbox Code Playgroud)
这是结果输出.
<root>
<item>4SOME TEXT</item>
<item>SOME TEXT</item>
<item>SOME TEXT2</item>
</root>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6983 次 |
最近记录: |