Firstly, there is nothing wrong with whitespace in attribute values in XML: roughly speaking, attribute value normalization converts whitespace characters to spaces and collapses adjacent spaces to a single space when a document is parsed, but whitespace is definitely allowed. EDIT: See below for more on this.
Matthew Wilson's approach fails to include whitespace between the possible values, as you mention in your comment thereto. However, his approach is fundamentally sound. The final piece of the jigsaw is your dislike of redundant spaces: these can be eliminated by use of the normalize-space XPath function.
The following stylesheet puts all the bits together - note that it doesn't do anything with its input document, so for testing purposes you can run it against any XML document, or even against itself, to verify that the output meets your requirements.
EDIT: Further to the question of spaces separating discrete components within the value of an attribute: The XML Spec defines a number of possible valid constructs as attribute types, including IDREFS and NMTOKENS. The first case matches the Names production, and the second case matches the NMTokens production; both these productions are defined as containing multiple values of the appropriate type, delimited by spaces. So space-delimited lists of values as the value of a single attribute are an inherent component of the XML information set.
除非您要转换为 XML 语言(HTML 不是,XHTML 是),您不必担心 XSLT 输出 的 XML 有效性。这可以是任何东西,并且不需要符合 XML!
As far as I know, white-space separated attribute values aren't a part of the XML data model and thus cannot directly be created via XSLT
Unless you are converting to an XML language (which HTML is not, XHTML is), you shouldn't worry about the XML validity of the XSLT ouput. This can be anything, and doesn't need to conform to XML!
发布评论
评论(3)
首先,XML 中的属性值中的空格没有任何问题:粗略地说, 属性值规范化将空白字符转换为空格,并在解析文档时将相邻空格折叠为单个空格,但绝对允许使用空格。 编辑:有关详细信息,请参阅下文。
正如您在评论中提到的那样,马修·威尔逊的方法未能在可能的值之间包含空格。然而,他的方法从根本上来说是正确的。拼图的最后一部分是您不喜欢多余的空间:可以使用
normalize-space
XPath 函数。以下样式表将所有部分放在一起 - 请注意,它不会对其输入文档执行任何操作,因此出于测试目的,您可以针对任何 XML 文档甚至针对自身运行它,以验证输出是否满足您的要求。
编辑:进一步讨论属性值中分隔离散组件的空格问题:XML 规范将许多可能的有效构造定义为 属性类型,包括 IDREFS 和 NMTOKENS。第一个案例与 Names 产生式匹配,并且第二种情况与 NMTokens 生产相匹配;这两个产生式都被定义为包含适当类型的多个值,并以空格分隔。因此,以空格分隔的值列表作为单个属性的值是 XML 信息集的固有组成部分。
Firstly, there is nothing wrong with whitespace in attribute values in XML: roughly speaking, attribute value normalization converts whitespace characters to spaces and collapses adjacent spaces to a single space when a document is parsed, but whitespace is definitely allowed. EDIT: See below for more on this.
Matthew Wilson's approach fails to include whitespace between the possible values, as you mention in your comment thereto. However, his approach is fundamentally sound. The final piece of the jigsaw is your dislike of redundant spaces: these can be eliminated by use of the
normalize-space
XPath function.The following stylesheet puts all the bits together - note that it doesn't do anything with its input document, so for testing purposes you can run it against any XML document, or even against itself, to verify that the output meets your requirements.
EDIT: Further to the question of spaces separating discrete components within the value of an attribute: The XML Spec defines a number of possible valid constructs as attribute types, including IDREFS and NMTOKENS. The first case matches the Names production, and the second case matches the NMTokens production; both these productions are defined as containing multiple values of the appropriate type, delimited by spaces. So space-delimited lists of values as the value of a single attribute are an inherent component of the XML information set.
在我的脑海中,你可以建立一个空格分隔的列表,如下所示:
Off the top of my head, you can build up a space-separated list with something like:
据我所知,空格分隔的属性值不是 XML 数据模型的一部分,因此不能直接通过 XSLT 创建
除非您要转换为 XML 语言(HTML 不是,XHTML 是),您不必担心 XSLT 输出 的 XML 有效性。这可以是任何东西,并且不需要符合 XML!
As far as I know, white-space separated attribute values aren't a part of the XML data model and thus cannot directly be created via XSLT
Unless you are converting to an XML language (which HTML is not, XHTML is), you shouldn't worry about the XML validity of the XSLT ouput. This can be anything, and doesn't need to conform to XML!