与正常化html标签空间的问题(Normalize space issue with html ta

2019-10-20 19:34发布

这里有一个适合你XSLT大师:-)

我必须处理Java程序我无法控制XML输出。

在这个输出的文档应用的HTML标签仍为

<u><i><b><em>  

等等,而不是

&lt;u&gt;&lt;i&gt;&lt;b&gt;&lt;em&gt; and so on.

这不是一个大问题,我使用XSLT来解决这个问题,但使用正常化空间以去除多余的空格这些HTML标签之前还删除空格。

<Locator Precode="7">
<Text LanguageId="7">The next word is <b>bold</b> and is correctly spaced 
around the html tag,
but the sentence has extra whitespace and 
line breaks</Text>
</Locator>

如果我运行XSLT脚本中,我们用它来去除多余的空白,这是其中的相关部分

<xsl:template match="text(.)">
<xsl:value-of select="normalize-space()"/>
</xsl:template>

在输出结果的XSLT已正确删除多余的空格和换行,但也去掉造成这种输出的标签之前的空间: -

The next word isboldand is correctly spaced around the html tag, but the sentence has extra whitespace and line breaks.

间距之前和之后单词“大胆”已被剥离为好。

任何人有任何想法如何防止这种情况发生? 非常好,在我束手无策,因此任何帮助,将不胜感激!

:-)

你好,我们又见面了,

是的,当然,这里是完整的样式表。 我们必须应对一个合格的HTML标签和间距

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" encoding="UTF-8"/>
<xsl:strip-space elements="*" />  


<xsl:template match="@*|node()">
 <xsl:copy> 
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>


<xsl:template match="Text//*">
  <xsl:value-of select="concat('&lt;',name(),'&gt;')" />
  <xsl:apply-templates />
  <xsl:value-of select="concat('&lt;/',name(),'&gt;')" />
</xsl:template>
<xsl:template match="text()">
    <xsl:value-of select="normalize-space(.)"/>
</xsl:template>


<xsl:template match="Instruction//*">
  <xsl:value-of select="concat('&lt;',name(),'&gt;')" />
  <xsl:apply-templates />
  <xsl:value-of select="concat('&lt;/',name(),'&gt;')" />
</xsl:template>

<xsl:template match="Title//*">
  <xsl:value-of select="concat('&lt;',name(),'&gt;')" />
  <xsl:apply-templates />
  <xsl:value-of select="concat('&lt;/',name(),'&gt;')" />
</xsl:template>


</xsl:stylesheet>

Answer 1:

一个XSLT 1.0溶液是XPath表达式与单一一个替换的几个空白字符的序列。 这个想法是不是我自己的,它是取自通过Dimitre Novatchev一个答案 。

通过内置的优势normalize-space()函数就是尾随空白(在你的情况下,前后b元素)保持。

编辑 :至于你编辑你的问题的回应。 下面是纳入到你的样式表说XPath表达式。 也:

  • 明确地说omit-xml-declaration="no"是多余的。 它是默认的行动采取XSLT处理器
  • 几个范本中具有相同的内容。 我总结他们使用| 到的单独一个。

样式表

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*" />  


<xsl:template match="@*|node()">
 <xsl:copy> 
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>


<xsl:template match="Text//*|Instruction//*|Title//*">
  <xsl:value-of select="concat('&lt;',name(),'&gt;')" />
  <xsl:apply-templates />
  <xsl:value-of select="concat('&lt;/',name(),'&gt;')" />
</xsl:template>

<xsl:template match="text()">
  <xsl:value-of select=
  "concat(substring(' ', 1 + not(substring(.,1,1)=' ')),
          normalize-space(),
          substring(' ', 1 + not(substring(., string-length(.)) = ' '))
          )
  "/>
  </xsl:template>

</xsl:stylesheet>

XML输出

<?xml version="1.0" encoding="UTF-8"?>
<Locator Precode="7">
   <Text LanguageId="7">The next word is &lt;b&gt;bold&lt;/b&gt; and is correctly spaced around the html tag, but the sentence has extra whitespace and line breaks</Text>
</Locator>


文章来源: Normalize space issue with html tags