简体   繁体   English

在* value *中带有前缀的XML属性-如何分辨它是名称空间前缀?

[英]XML attribute with a prefix in *value* - how to tell it is a namespace prefix?

I'm writing a code that re-organizes namespaces in an arbitrary XML, potentially changing their prefixes. 我正在编写一个代码,以重新组织任意XML中的名称空间,可能会更改其前缀。 That was pretty straightforward until I ran into the xsi:type attribute: 在我遇到xsi:type属性之前,这非常简单:

<foo xsi:type="xs:string">...</foo>

If I change the xs prefix of XSD namespace, I have to do the same for this xsi:type value, eg into 如果更改XSD命名空间的xs前缀,则必须对此xsi:type值执行相同的操作,例如

<foo i:type="x:string">...</foo>

This attribute is well known. 此属性是众所周知的。 However, in general, if I find a code like this: 但是,通常,如果我找到这样的代码:

<foo xmlns:aaa="http://bbb">
   <bar name="aaa:123">...</bar>
</foo>

Is there a way to tell that in the "aaa:123" value the "aaa" part refers to " http://bbb " namespace? 有没有办法告诉“ aaa:123”值中的“ aaa”部分引用“ http:// bbb ”命名空间?

Ie it could be that the name is simply "aaa:123", without any intended reference to the namespace with "aaa" prefix, and the match is accidental. 也就是说,名称可能只是“ aaa:123”,而没有任何旨在引用前缀为“ aaa”的名称空间的提示,并且匹配是偶然的。

If it helps, the implementation language is Java. 如果有帮助,则实现语言为Java。

Update/Solution : 更新/解决方案

Thanks to the helpful explanations and pointers provided in the answers below, I have modified my code to work by the following rules when it encounters an attribute that has a prefixed value: 由于下面的答案中提供了有用的解释和指针,因此,我修改了代码,使其在遇到带有前缀值的属性时可以按照以下规则工作:

  • For xsi:type attribute, update the attribute value's prefix to match the new prefix for http://www.w3.org/2001/XMLSchema . 对于xsi:type属性,更新属性值的前缀以匹配http://www.w3.org/2001/XMLSchema的新前缀。
  • If in the current context there IS NO namespace with a matching prefix, 如果在当前上下文中不存在带有匹配前缀的名称空间,
    the value is considered literal (not QName) and left as is. 该值被视为文字(不是QName),并保持原样。
  • If in the current context there IS a namespace with a matching prefix, we cannot tell if the attribute value is literal or QName, and so the code cancels the processing and leaves the document as is. 如果在当前上下文中存在一个带有匹配前缀的名称空间,则我们无法确定属性值是文字型还是QName,因此代码取消了处理,并保留了文档。 The document is not modified at all. 该文档完全没有修改。

For anyone interested, the code is here . 对于任何有兴趣人, 代码都在这里

I know the logic can be improved by not touching only the namespaces affected by the ambiguous attributes, but it is Good Enough(tm) for me. 我知道可以通过不只触及受歧义属性影响的名称空间来改善逻辑,但这对我来说是很好的。

This isn't possible in a generic way without knowledge of the intepretation of the XML by the application. 如果不了解应用程序对XML的理解,就不可能以通用的方式实现。 There is, however, a weak convention that if the attribute or element in question has an XML Schema data type of xsd:QName (thus the XML in question must be described by XML Schema in the first place), then the attribute's or element's value is subject to namespace normalization. 但是,有一个较弱的约定,即如果所讨论的属性或元素的XML Schema数据类型为xsd:QName (因此,所讨论的XML首先必须由XML Schema描述),则属性或元素的值受命名空间规范化的约束。

See also Using Qualified Names (QNames) as Identifiers in XML Content . 另请参见在XML内容中使用合格名称(QName)作为标识符

A schema will tell you if an attribute is typed as xs:QName , but it won't tell you that it's a namespace-sensitive XPath expression (such as xsl:value-of/@select in XSLT or xs:selector/@xpath in XSD). 模式将告诉您是否将属性键入为xs:QName ,但不会告诉您它是名称空间敏感的XPath表达式(例如XSLT中的xsl:value-of/@selectxs:selector/@xpath在XSD中)。 And even if you knew these attributes were namespace sensitive, you would have a lot of detailed parsing to do to extract and replace the namespace prefixes. 并且即使您知道这些属性对名称空间敏感,您也需要进行许多详细的解析来提取和替换名称空间前缀。

So even with a schema, the task is not possible in the general case. 因此,即使使用模式,一般情况下也无法完成任务。

Unfortunately you're not the first person to run across this problem. 不幸的是,您不是第一个遇到此问题的人。 Defining the data model used by XPath was always plagued by the problem of QNames-in-content (or more generally, prefixes-in-content). 定义XPath使用的数据模型一直困扰着内容中的QName(或更一般地,内容中的前缀)问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM