В XML действительно ли порядок важен?

Я использую $(document.createElement('div')); шоу Сравнительного тестирования , эта техника является самой быстрой. Я размышляю, что это вызвано тем, что jQuery не должен идентифицировать его как элемент и создать сам элемент.

необходимо действительно выполнить сравнительные тесты с различными механизмами JavaScript и взвесить аудиторию с результатами. Примите решение оттуда.

51
задан Landon Kuhn 15 July 2009 в 02:54
поделиться

10 ответов

Order of elements is significant in XML, so in your example the two documents are different. Attribute order is not significant, though.

<people>
  <person name="kathy" id="1"/>
</people>

This is exactly the same as:

<people>
  <person id="1" name="kathy"/>
</people>
51
ответ дан 7 November 2019 в 09:59
поделиться

Они не идентичны - Значение последовательности зависит от программы или пользователя, которые ее обрабатывают. Например, последовательность элементов в документе XHTML определяет, как они отображаются в браузере, а поисковые системы используют позицию в документе для оценки относительной важности данных.

23
ответ дан 7 November 2019 в 09:59
поделиться

The order is captured.

1
ответ дан 7 November 2019 в 09:59
поделиться

The order is potentially important, but it depends on what's being transmitted.

In XHTML, for example, the order is extremely important - if you had sibling paragraphs in a random order, it would be very confusing!

In many other cases, it's unimportant.

XML is just a way of representing a tree of nodes. XML itself says that the order is important: APIs must preserve the order, for example - but it's up to whatever produces/interprets the data to really care about the order or not.

The XML specification effectively has to "err on the side of ordering" - it's easy to ignore ordering if you don't care about it, but it's a pain to reconstruct ordering if APIs decide to switch things around. (You'd have to put the order into attributes etc.)

20
ответ дан 7 November 2019 в 09:59
поделиться

There is a difference. You can use various XML API's to process elements in sequence or find an element by index. Of course order may not matter in your particular example, but that depends on the semantics of the data.

1
ответ дан 7 November 2019 в 09:59
поделиться

According to this article, the 1.0 version of the standard doesn't even require that parsers report siblings in the order they appear in the document. In that light, they would not be considered different, as both children are there. Perhaps this has changed, so that other answers are for newer versions of XML, though.

4
ответ дан 7 November 2019 в 09:59
поделиться

I think those should be considered identical, but it's really up to the software or person reading it to decide. XML is just a way of writing out data. The application determines how that data is used and therefore much of the meaning.

If your application reads in all of the person elements and then alphabetizes them by name, then the order in the XML document is meaningless. If your application reads them in and assigns seats in the same order the people appear in the XML, then the order is very important.

It's up to the application that uses the data. If the order is important, it should be described in the specs for people generating the files.

1
ответ дан 7 November 2019 в 09:59
поделиться

While XML attribute ordering is not significant as far as the XML standard is concerned, the textual representation of XML does by necessity place the attributes in a specific order. This can be an issue for things like XML Signature, which generates a digital signature for XML documents. A different attribute order would generate a different signature, which clearly is wrong.

For this (and other) reasons, there is now a standard for XML Canonicalization, which defines rules for re-organising XML documents such that they retain the same information content, but have things like whitespace, namespace declarations and attributes rearranged in a predictable way.

From xml.com

Canonical XML requires the inclusion of namespace declarations and attributes in ascending lexicographic order.

5
ответ дан 7 November 2019 в 09:59
поделиться

http://www.ibm.com/developerworks/xml/library/x-eleord.html

Возможно, обсуждение в статье поможет ответить на ваш вопрос. Поскольку ваш вопрос в некоторой степени открытый, я не уверен, что он охватывает вас.

1
ответ дан 7 November 2019 в 09:59
поделиться

Спецификация XML 1.0 ничего не говорит о порядке элементов с одинаковыми именами, как дочерние элементы одного и того же родительского элемента. Похоже, проблема не решена.

Однако большинство анализаторов XML и API-интерфейсов сохранят последовательность, указанную в текстовом представлении. Таким образом, можно реализовать приложения, которые заботятся о порядке элементов. И де-факто ответ на ваш вопрос: да, порядок имеет значение. Это два разных примера.

При более внимательном рассмотрении вам необходимо выяснить, каков ваш вариант использования. Если ваш XML должен взаимодействовать с различными (возможно, сторонними) приложениями, вы всегда должны предполагать, что порядок имеет значение. Если у вас есть полный контроль над производящим и потребляющим приложением, вы можете ослабить это правило.

Как всегда, вам придется судить.

3
ответ дан 7 November 2019 в 09:59
поделиться
Другие вопросы по тегам:

Похожие вопросы: