保留属性空格 [英] Preserving attribute whitespace

查看:27
本文介绍了保留属性空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:以下内容是针对 XML 的.这就是为什么我试图用 XSLT 来改变它 :)

我的 XML 目前看起来像这样:

<对象名称="blarg" property1="shablarg" property2="werg".../><object name="yetanotherobject" .../></root>

是的,我将所有文本数据都放在属性中.我希望 XSLT 可以拯救我;我想朝着这样的方向前进:

<对象><name>blarg</name><property1>shablarg</name>...</对象><对象>...</对象></root>

到目前为止,我实际上已经完成了所有这些工作,除了我对 XML 的错误更......例外.一些标签如下所示:

<object description = "这是第一行这是第三行.充满空格的第二行是有意义的"/>

我在 linux 下使用 xsltproc,但它似乎没有任何保留空格的选项.我尝试使用 xsl:preserve-space 和 xml:space="preserve" 无济于事.我发现的每个选项似乎都适用于在元素本身中保留空格,但不适用于属性.每次,以上都会更改为:

<前>这是第一行 这是第三行.充满空格的第二行很有意义

那么问题是,我可以保留属性空格吗?

解决方案

这实际上是一个原始 XML 解析问题,XSLT 无法帮助您解决.根据 XML 标准中的3.3.3 属性值规范化",XML 解析必须将该属性值中的换行符转换为空格.因此,当前读取您的描述属性并保留换行符的任何操作都是错误的.

您可以通过预处理 XML 以将换行符转义为 & 来恢复换行符.#10;字符引用,只要您还没有获得不允许使用字符引用的换行符,例如在标签正文中.Charrefs 应该作为控制字符一直存在到属性值,然后您可以将它们转换为文本节点.

Disclaimer: the following is a sin against XML. That's why I'm trying to change it with XSLT :)

My XML currently looks like this:

<root>
    <object name="blarg" property1="shablarg" property2="werg".../>
    <object name="yetanotherobject" .../>
</root>

Yes, I'm putting all the textual data in attributes. I'm hoping XSLT can save me; I want to move toward something like this:

<root>
    <object>
        <name>blarg</name>
        <property1>shablarg</name>
        ...
    </object>
    <object>
        ...
    </object>
</root>

I've actually got all of this working so far, with the exception that my sins against XML have been more... exceptional. Some of the tags look like this:

<object description = "This is the first line

This is the third line.  That second line full of whitespace is meaningful"/>

I'm using xsltproc under linux, but it doesn't seem to have any options to preserve whitespace. I've attempted to use xsl:preserve-space and xml:space="preserve" to no avail. Every option I've found seems to apply to keeping whitespace within the elements themselves, but not the attributes. Every single time, the above gets changed to:

This is the first line This is the third line.  That second line full of whitespace is meaningful

So the question is, can I preserve the attribute whitespace?

解决方案

This is actually a raw XML parsing problem, not something XSLT can help you with. An XML parse must convert the newlines in that attribute value to spaces, as per ‘3.3.3 Attribute-Value Normalization’ in the XML standard. So anything currently reading your description attributes and keeping the newlines in is doing it wrong.

You may be able to recover the newlines by pre-processing the XML to escape the newlines to & #10; character references, as long as you haven't also got newlines where charrefs are disallowed, such as inside tag bodies. Charrefs should survive as control characters through to the attribute value, where you can then turn them into text nodes.

这篇关于保留属性空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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