我正在用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="' '"/> <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 
 with br tag to show new line while transforming xml using xsl. i want to replace blank spaces to its according code that may   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="' '"/> <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=" " 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=" " 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=" " 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=" " 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>更多推荐
发布评论