xslt中的带前导和尾随换行符
我有一个结构如下的xml:xslt中的带前导和尾随换行符,xslt,Xslt,我有一个结构如下的xml: <xxx> <yyy> some text with spaces inside </yyy> <zzz> another text </zzz> </xxx> <div><div>some text with spaces inside</div><div>another text</div></div> 有些
<xxx>
<yyy>
some text with spaces inside
</yyy>
<zzz>
another text
</zzz>
</xxx>
<div><div>some text with spaces inside</div><div>another text</div></div>
有些文本中有空格
另一个文本
我想生成将使用空格:pre样式格式化的html,但我必须从节点yyy和zzz中删除换行符
我的输出应该是这样的:
<xxx>
<yyy>
some text with spaces inside
</yyy>
<zzz>
another text
</zzz>
</xxx>
<div><div>some text with spaces inside</div><div>another text</div></div>
一些文本中有空格,另一个文本
我试过了
<xsl:output method="html" indent="no"/>
<xsl:strip-space elements="*"/>
但它不起作用。换行符仍然存在
我不能使用规范化空格,因为单词之间的空格很重要。以下XSL将从元素
yyy
和zzz
中删除所有换行符:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:template match="/xxx">
<div>
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="yyy | zzz">
<div>
<xsl:value-of select="translate(.,'
', '')" />
</div>
</xsl:template>
</xsl:stylesheet>
以下XSL从元素
yyy
和zzz
中删除所有换行符:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:template match="/xxx">
<div>
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="yyy | zzz">
<div>
<xsl:value-of select="translate(.,'
', '')" />
</div>
</xsl:template>
</xsl:stylesheet>
因为您只想从字符串中删除第一个和最后一个字符,所以这个简单的转换就是这样做的:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="yyy/text() | zzz/text()">
<xsl:value-of select="substring(.,2, string-length() -2)"/>
</xsl:template>
</xsl:stylesheet>
<xxx>
<yyy>
some text with spaces inside
</yyy>
<zzz>
another text
</zzz>
</xxx>
<xxx>
<yyy>some text with spaces inside</yyy>
<zzz>another text</zzz>
</xxx>
<someText>
This is some text
</someText>
'This is some text'
当此转换应用于以下XML文档时:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="yyy/text() | zzz/text()">
<xsl:value-of select="substring(.,2, string-length() -2)"/>
</xsl:template>
</xsl:stylesheet>
<xxx>
<yyy>
some text with spaces inside
</yyy>
<zzz>
another text
</zzz>
</xxx>
<xxx>
<yyy>some text with spaces inside</yyy>
<zzz>another text</zzz>
</xxx>
<someText>
This is some text
</someText>
'This is some text'
因为您只想从字符串中删除第一个和最后一个字符,所以这个更简单的转换就是这样做的:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="yyy/text() | zzz/text()">
<xsl:value-of select="substring(.,2, string-length() -2)"/>
</xsl:template>
</xsl:stylesheet>
<xxx>
<yyy>
some text with spaces inside
</yyy>
<zzz>
another text
</zzz>
</xxx>
<xxx>
<yyy>some text with spaces inside</yyy>
<zzz>another text</zzz>
</xxx>
<someText>
This is some text
</someText>
'This is some text'
当此转换应用于以下XML文档时:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="yyy/text() | zzz/text()">
<xsl:value-of select="substring(.,2, string-length() -2)"/>
</xsl:template>
</xsl:stylesheet>
<xxx>
<yyy>
some text with spaces inside
</yyy>
<zzz>
another text
</zzz>
</xxx>
<xxx>
<yyy>some text with spaces inside</yyy>
<zzz>another text</zzz>
</xxx>
<someText>
This is some text
</someText>
'This is some text'
没错,上面的代码删除了所有换行符。这不是我所需要的。你是对的,上面的代码删除了所有换行符。那不是我所需要的。