gpt4 book ai didi

xml - 我可以使用模式强制执行 XML 属性的顺序吗?

转载 作者:数据小太阳 更新时间:2023-10-29 01:39:21 26 4
gpt4 key购买 nike

我们的 C++ 应用程序从如下所示的 XML 文件中读取配置数据:

<data>
<value id="FOO1" name="foo1" size="10" description="the foo" ... />
<value id="FOO2" name="foo2" size="10" description="the other foo" ... />
...
<value id="FOO300" name="foo300" size="10" description="the last foo" ... />
</data>

完整的应用程序配置包含约 2500 个这样的 XML 文件(转换为超过 150 万个键/值属性对)。 XML 文件来自许多不同的来源/团队,并根据模式进行验证。但是,有时 <value/>节点看起来像这样:

<value name="bar1" id="BAR1" description="the bar" size="20" ... />

或者这个:

<value id="BAT1" description="the bat" name="bat1"  size="25" ... />

为了加快这个过程,我们使用 Expat解析 XML 文档。 Expat 将属性公开为一个数组——像这样:

void ExpatParser::StartElement(const XML_Char* name, const XML_Char** atts)
{
// The attributes are stored in an array of XML_Char* where:
// the nth element is the 'key'
// the n+1 element is the value
// the final element is NULL
for (int i = 0; atts[i]; i += 2)
{
std::string key = atts[i];
std::string value = atts[i + 1];
ProcessAttribute (key, value);
}
}

这将所有责任推给了我们的 ProcessAttribute()函数读取“键”并决定如何处理该值。 对应用程序进行分析表明,大约 40% 的 XML 解析总时间用于按名称/字符串处理这些属性。

如果我可以保证/强制执行属性的顺序(对于初学者,ProcessAttribute() 中没有字符串比较),整个过程可以大大加快。例如,如果“id”属性始终是第一个属性,我们可以直接处理它:

void ExpatParser::StartElement(const XML_Char* name, const XML_Char** atts)
{
// The attributes are stored in an array of XML_Char* where:
// the nth element is the 'key'
// the n+1 element is the value
// the final element is NULL
ProcessID (atts[1]);
ProcessName (atts[3]);
//etc.
}

根据 W3C 架构规范,我可以使用 <xs:sequence>在 XML 模式中强制执行元素的顺序 - 但它似乎不适用于属性 - 或者我可能使用不正确:

<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element name="value" type="value_type" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="value_type">
<!-- This doesn't work -->
<xs:sequence>
<xs:attribute name="id" type="xs:string" />
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="description" type="xs:string" />
</xs:sequence>
</xs:complexType>

有没有办法强制 XML 文档中的属性顺序?如果答案是“否”——有人能提出一个不会带来巨大运行时性能损失的替代方案吗?

最佳答案

根据xml规范,

the order of attribute specifications in a start-tag or empty-element tag is not significant

可以在section 3.1查看

关于xml - 我可以使用模式强制执行 XML 属性的顺序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1682131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com