在 XSLT 1.0 中规范混合内容元素中的空白 [英] Normalize whitespace in mixed-content elements, in XSLT 1.0

查看:18
本文介绍了在 XSLT 1.0 中规范混合内容元素中的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

属性@xml:space 的值可以是"default""preserve".XML 指定了第二个含义,但将第一个留给应用程序.(我想我是正确的.)那么如果应用程序希望 default 实现 XSchema 的 collapse 怎么办?XSLT 1.0 如何真正做到这一点?

The value of the attribute @xml:space can be either "default" or "preserve". XML specifies what the second means but leaves the first up to the application. (I think I have that correct.) So what if the application wants default to implement XSchema's collapse? How could XSLT 1.0 actually do this?

我认为处理文本的内置模板,也就是

I think the built-in template for processing text, that is,

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

需要用这样的伪代码替换:

would need to be replaced with something like this pseudo-code:

<xsl:choose>
   <xsl:when test="../@xml:space='preserve'"
     <xsl:value-of select="."/>
   </xsl:when>
   <xsl:otherwise>

      if position(.)=1 then output LTRIM(value-of(.))
      if position(.)=last() then output RTRIM(value-of(.))
      if position(.)= 1 and last()=1 then output normalize-space(.)

   </xsl:otherwise>
</xsl:choose>

这个输入然后:

<persName> The man is 
   <forename>Edward</forename>

   <forename>George</forename>
   <surname type="linked">Bulwer-Lytton</surname>, <roleName>Baron Lytton of
   <placeName>Knebworth</placeName>
   </roleName>
</persName>

将被正确渲染为 The man is Edward George Bulwer-Lytton, Baron Lytton of KnebworthThe man 之前和 Knebworth 之后有空格> 修剪和 EdwardGeorge 之间的空格折叠.(示例来自 TEI.)

would get rendered correctly as The man is Edward George Bulwer-Lytton, Baron Lytton of Knebworth with the space before The man and after Knebworth trimmed and the spaces between Edward and George collapsed. (The example is from TEI.)

实现该伪代码的 XSLT 1.0 需要为每个文本节点执行.那会不会又丑又慢?

The XSLT 1.0 to implement that pseudo-code would need to be executed for every text node. Wouldn't that be ugly and slow?

底线:如何在 XSLT 1.0(只有浏览器嵌入的扩展)中实现 XSchema 的折叠?

Bottom line: How does one implement XSchema's collapse in XSLT 1.0 (with only browser-embedded extensions)?

我希望我说的是对的.我希望代码很简单.我还没有看到它怎么可能.

I hope I'm saying all that correctly. And I hope the code is simple. I haven't yet seen how it can be.

推荐答案

我检查了 xml-dev,结果证明我对 @xml:space 的含义和预期用途是正确的.

I checked at xml-dev, and it turns out I was correct about the meaning and intended use of @xml:space.

这里是规范混合内容元素中的空白的代码(这是表达我想要做的更好的方式):

Here is code to normalize whitespace in mixed-content elements (that's a better way to say what I wanted to do):

<xsl:template priority=".7" match="text()[position()=1 and not((ancestor::node()/@xml:space)[position()=last()]='preserve')]">
    <xsl:value-of select="normalize-space()"/>
    <xsl:if test="normalize-space(substring(., string-length(.))) = ''">
        <xsl:text> </xsl:text>
    </xsl:if>
</xsl:template>
<xsl:template priority=".7" match="text()[position()=last() and not((ancestor::node()/@xml:space)[position()=last()]='preserve')]">
    <xsl:if test="normalize-space(substring(., 1, 1)) = ''">
        <xsl:text> </xsl:text>
    </xsl:if>
    <xsl:value-of select="normalize-space()"/>
</xsl:template>
<xsl:template priority=".8" match="text()[position()=1 and position()=last() and not((ancestor::node()/@xml:space)[position()=last()]='preserve')]" >
    <xsl:value-of select="normalize-space(.)"/>
</xsl:template>

过滤@xml:space 允许覆盖preserve.test= 只是一种测试空格的方法.优先级解决了当一个节点是元素中唯一的文本节点时引起的冲突,因此既是第一个也是最后一个.

Filtering on @xml:space allows preserve to override. The test= is just a way to test for whitespace. The priorities resolve the conflict caused when a node is the only text node in an element, and thus both the first and the last.

这篇关于在 XSLT 1.0 中规范混合内容元素中的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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