如何使用XSLT从XML中删除换行符?
发布时间:2020-12-16 23:04:36 所属栏目:百科 来源:网络整理
导读:我有这个XSLT: xsl:strip-space elements="*" /xsl:template match="math" img class="math" xsl:attribute name="src"http://latex.codecogs.com/gif.latex?xsl:value-of select="text()" //xsl:attribute /img/xsl:template 哪个适用于此XML(请注意换行符
我有这个XSLT:
<xsl:strip-space elements="*" /> <xsl:template match="math"> <img class="math"> <xsl:attribute name="src">http://latex.codecogs.com/gif.latex?<xsl:value-of select="text()" /></xsl:attribute> </img> </xsl:template> 哪个适用于此XML(请注意换行符): <math>text{average} = alpha times text{data} + (1-alpha) times text{average}</math> 不幸的是,转换创建了这个: <img class="math" src="http://latex.codecogs.com/gif.latex?text{average} = alpha times text{data} + (1-alpha) times 					text{average}" /> 注意空白字符文字.虽然它有效,但它非常混乱.我怎么能阻止这个? 解决方法
使用
normalize-space() 功能是不够的,因为它离开了中间空间!
这是一个简单而完整的解决方案: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes"/> <xsl:strip-space elements="*" /> <xsl:template match="math"> <img class="math"> <xsl:attribute name="src">http://latex.codecogs.com/gif.latex?<xsl:value-of select="translate(.,' 	 ','')" /></xsl:attribute> </img> </xsl:template> </xsl:stylesheet> 在提供的XML文档上应用此转换时: <math>text{average} = alpha times text{data} + (1-alpha) times text{average}</math> 产生了想要的正确结果: <img class="math" src="http://latex.codecogs.com/gif.latex?text{average}=alphatimestext{data}+(1-alpha)timestext{average}" /> 请注意: >使用XPath 1.0 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |