Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xslt中的带前导和尾随换行符_Xslt - Fatal编程技术网

xslt中的带前导和尾随换行符

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> 有些

我有一个结构如下的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>

有些文本中有空格
另一个文本
我想生成将使用空格: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(.,'&#x0d;&#x0a;', '')" />
</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(.,'&#x0d;&#x0a;', '')" />
</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'

没错,上面的代码删除了所有换行符。这不是我所需要的。你是对的,上面的代码删除了所有换行符。那不是我所需要的。