用html标签标准化空间问题 [英] Normalize space issue with html tags

查看:110
本文介绍了用html标签标准化空间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



在此应用程序输出的文档中,html标记保持为

 < u>< i> ;< b取代;< EM> 

等,而不是

 &安培; LT; U&安培; GT;&安培; LT; I&安培; GT;&安培; LT; b&安培; GT;&安培; LT; EM&安培; GT;等等。 

这不是一个大问题,我使用XSLT来解决这个问题,但是使用normalize-space来删除多余的



示例

 < / p>< ; Locator Precode =7> 
< Text LanguageId =7>下一个字是< b>粗体< / b>并且在html标签
周围正确地分隔
,但句子有额外的空格和
换行符< / Text>
< / Locator>

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

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

在结果输出中,xslt已正确删除多余的空格和换行符,但它也有删除标签之前的空格导致此输出: - $ /

 下一个单词isboldand在html标签周围正确分隔,但是句子有额外的空白和换行符。 

单词bold之前和之后的间距也被剥离了。



任何人有任何想法如何防止这种情况发生?

: - )



再次感谢您的帮助,



当然,这里是完整的样式表。我们必须在一次传递中处理html标记和间隔

 < xsl:stylesheet version =1.0xmlns:xsl = http://www.w3.org/1999/XSL/Transform > 
< xsl:output method =xmlindent =yesomit-xml-declaration =noencoding =UTF-8/>


< 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: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>


解决方案

XSLT 1.0解决方案是一个XPath表达式,几个空白字符的序列与一个单一的。这个想法不是我自己的,它来自于 Dimitre Novatchev的回答。 p>

优于内置的 normalize-space()函数的优点是尾随空格(在你的情况下,在 b 元素之后)。


$ b 编辑:作为对您的回应编辑你的问题。下面是所述样式表中的XPath表达式。另外:


  • 明确地说 omit-xml-declaration =no是多余的。这是XSLT处理器采取的默认操作。

  • 几个模板具有相同的内容。我将 | 汇总为一个。



样式表

 < xsl:stylesheet version =1.0xmlns:xsl =http://www.w3 .ORG / 1999 / XSL /变换> 


< 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()>
concat(substring('',1 + not(substring(。,1,1)='')),
normalize-space (),
substring('',1 + not(substring(。,string-length(。))=''))

/>
< / xsl:template>

< / xsl:stylesheet>

XML输出

 <?xml version =1.0encoding =UTF-8?> 
< Locator Precode =7>
< Text LanguageId =7>下一个字是& lt; b& gt;加粗& lt; / b& gt;并且正确地在html标签周围放置,但是该句子有额外的空格和换行符< / Text>
< / Locator>


Here's one for you XSLT gurus :-)

I have to deal with XML output from a Java program I cannot control.

In the docs outputted by this app the html tags remain as

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

etc, instead of

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

That's not a massive problem, I use XSLT to fix that, but using normalize-space to remove excess whitespace also removes spaces before these html tags.

Example

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

If I run the XSLT script we use to remove extra white space, of which this is the relevant part

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

In the resulting output the xslt has correctly removed the extra whitespace and the line breaks, but it has also removed the space before the tag resulting in this output :-

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

The spacing before and after the word "bold" has been stripped as well.

Anyone have any ideas how to prevent this from happening? Pretty well at my wits end so any help will be greatly appreciated!

:-)

Hi again,

Yes of course, here's the full stylesheet. We have to deal with the html tags and spacing in one pass

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

解决方案

An XSLT 1.0 solution is an XPath expression to replace a sequence of several whitespace characters with a single one. The idea is not my own, it is taken from an answer by Dimitre Novatchev.

The advantage over the built-in normalize-space() function is that trailing whitespace (in your case, before and after the b element) is kept.

EDIT: As a response to you editing your question. Below is the said XPath expression incorporated into your stylesheet. Also:

  • Explicitly saying omit-xml-declaration="no" is redundant. It is the default action taken by the XSLT processor
  • Several of your templates have the same content. I summarized them using | to a single one.

Stylesheet

<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 Output

<?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>

这篇关于用html标签标准化空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆