使用XSL替换xml的单个元素中的多个字符(replacement of multiple characters in a single element of xml using XSL)

编程入门 行业动态 更新时间:2024-10-27 02:27:10
使用XSL替换xml的单个元素中的多个字符(replacement of multiple characters in a single element of xml using XSL)

我正在用br标签替换&#10以在使用xsl转换xml时显示新行。 我想将空格替换为可能同时出现的相应代码。示例代码如下所示。 请告诉我该怎么办。

而xml文件可能是------------

<?xml version="1.0" encoding="iso-8859-1"?><?xml-stylesheet type="text/xsl" href="task.xsl"?><Nodes><sNode><Word><![CDATA[1 2.............3............4............5 3]]></Word></sNode></Nodes>

因为空格自动省略所以这里........代表空格。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <table> <xsl:for-each select="Nodes/sNode"> <tr> <td> <xsl:call-template name="replace-string-with-element"> <xsl:with-param name="text" select="Word"/> <xsl:with-param name="replace" select="'&#10;'"/> <xsl:with-param name="with" select="'br'"/> </xsl:call-template> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> <xsl:template name="replace-string-with-element"> <xsl:param name="text"/> <xsl:param name="replace"/> <xsl:param name="with"/> <xsl:choose> <xsl:when test="contains($text,$replace)"> <xsl:value-of select="substring-before($text,$replace)"/> <xsl:element name="{$with}"/> <xsl:call-template name="replace-string-with-element"> <xsl:with-param name="text" select="substring-after($text,$replace)"/> <xsl:with-param name="replace" select="$replace"/> <xsl:with-param name="with" select="$with"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$text"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>

i am replacing &#10 with br tag to show new line while transforming xml using xsl. i want to replace blank spaces to its according code that may &nbsp or something else at the same time.sample code is below. please suggest for me what should i do.

while xml file may be as ------------

<?xml version="1.0" encoding="iso-8859-1"?><?xml-stylesheet type="text/xsl" href="task.xsl"?><Nodes><sNode><Word><![CDATA[1 2.............3............4............5 3]]></Word></sNode></Nodes>

since blank spaces ommitted automatically so here ........ represents blank spaces.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <table> <xsl:for-each select="Nodes/sNode"> <tr> <td> <xsl:call-template name="replace-string-with-element"> <xsl:with-param name="text" select="Word"/> <xsl:with-param name="replace" select="'&#10;'"/> <xsl:with-param name="with" select="'br'"/> </xsl:call-template> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> <xsl:template name="replace-string-with-element"> <xsl:param name="text"/> <xsl:param name="replace"/> <xsl:param name="with"/> <xsl:choose> <xsl:when test="contains($text,$replace)"> <xsl:value-of select="substring-before($text,$replace)"/> <xsl:element name="{$with}"/> <xsl:call-template name="replace-string-with-element"> <xsl:with-param name="text" select="substring-after($text,$replace)"/> <xsl:with-param name="replace" select="$replace"/> <xsl:with-param name="with" select="$with"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:value-of select="$text"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>

最满意答案

您可以使用xsl:character-map ,如下所示:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs"> <xsl:character-map name="replaceChars"> <xsl:output-character character="&#10;" string="br"/> </xsl:character-map> <xsl:output method="xml" version="1.0" encoding="UTF-8" use-character-maps="replaceChars" indent="yes"/> <!-- Implement your templates --> </xsl:stylesheet>

甚至可以在外部XSLT中的xsl:character-map中保存所有字符,并使用<xsl:import href="characterFile.xslt" />


要在样式表中实现此功能,请使用以下XSLT:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:character-map name="replaceChars"> <xsl:output-character character="&#10;" string="br"/> </xsl:character-map> <xsl:output method="html" use-character-maps="replaceChars"/> <xsl:template match="/"> <html> <body> <table> <xsl:for-each select="Nodes/sNode"> <tr> <td> <xsl:value-of select="Word" /> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template>

You could use xsl:character-map as follows:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs"> <xsl:character-map name="replaceChars"> <xsl:output-character character="&#10;" string="br"/> </xsl:character-map> <xsl:output method="xml" version="1.0" encoding="UTF-8" use-character-maps="replaceChars" indent="yes"/> <!-- Implement your templates --> </xsl:stylesheet>

It is even possible to save all characters in a xsl:character-map in an external XSLT and use <xsl:import href="characterFile.xslt" />


To implement this in your stylesheet use the following XSLT:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:character-map name="replaceChars"> <xsl:output-character character="&#10;" string="br"/> </xsl:character-map> <xsl:output method="html" use-character-maps="replaceChars"/> <xsl:template match="/"> <html> <body> <table> <xsl:for-each select="Nodes/sNode"> <tr> <td> <xsl:value-of select="Word" /> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template>

更多推荐

本文发布于:2023-07-14 20:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   字符   元素   xml   XSL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!