Надлежащие XML, форматирующие, [копируют]

Можно использовать его, чтобы помочь сделать запрос более эффективным. Это действительно позволяет Вам реструктурировать запросы в SQL Server для использования внешнего объединения вместо внутреннего, которое удаляет необходимость SQL-серверов необходимости проверить, существует ли пустой указатель в столбце. Вы уже не должны вставлять тот спецификатор потому что отношения внешнего ключа inforces это для Вас.

Так это:

    select p.ProductId, p.Name, c.CategoryId, c.Name AS Category 
from Products p inner join ProductCategories c on p.CategoryId = c.CategoryIdwhere c.CategoryId = 1;

Становится этим:

SELECT p.ProductId, p.Name, c.CategoryId, c.Name AS Category 
FROM ProductCategories c 
LEFT OUTER JOIN Products P ON
c.CategoryId = p.CategoryId 
WHERE c.CategoryId = 1;

Это не обязательно сделает огромную производительность в небольших запросах, но когда таблицы станут большими, это может быть более эффективно.

5
задан Community 23 May 2017 в 12:30
поделиться

6 ответов

I usually treat the attributes as metadata, or data about, or qualifying, the data within the nodes.

e.g. in your above example, I'd perhaps put the message as a text node, and have an attribute id relating to it.

There's no right or wrong here, but I find the above heuristic useful. One scenario where you may want to store as much as possible in attributes is for SAX parsing, in which your element callback has a parameter of all the attributes. i.e. you get one callback for all attributes, rather than 'n' callbacks, one for each piece of data. However that's a pretty specialised reason for constructing your document that way, and I'd think carefully before doing that.

14
ответ дан 18 December 2019 в 08:29
поделиться

I'd put the ID (and references, short names, etc) in as an attribute. The rest depends on you. I've seen a lot of "attribute only" schemas, and it's not so bad for certain types of data (short strings, dates, etc) and makes writing SAXParsers easier, but it doesn't feel "right" to me.

Some people support both, i.e., data as attributes or fields, so the user can decide.

I personally think that attributes as meta-data as described above is the sane way to do things.

1
ответ дан 18 December 2019 в 08:29
поделиться

Specifying with elements is certainly more verbose and tiresome, on the other hand it is not possible to validate as fully with attributes.

If you have two properties, one of which must be specified, you can validate this if they are child elements within the schema, with attributes you can only say if the attribute is required or not.

From the W3Schools Attributes page:

Some of the problems with using attributes are:

  • attributes cannot contain multiple values (elements can)
  • attributes cannot contain tree structures (elements can)
  • attributes are not easily expandable (for future changes)

In this (slightly contrived) example the vehicle might have both wings and wheels or some combination, with attributes we can't enforce that at least one is defined, whereas with elements we could use a choice element with a minOccurs to ensure at least one of the elements is defined.

The name attribute is always required so it can be either an attribute or element with no validation issues.

<vehicle name="plane" wheels="3" wings="2"/>
<vehicle name="sled" runners="2"/>
<vehicle name="robin reliant" wheels="3"/>

<vehicle name="plane">
  <wheels>3</wheels>
  <wings>2</wings>
</vehicle>
<vehicle name="sled">
  <runners>2</runners>
</vehicle>
<vehicle name="robin reliant">
  <wheels>3</wheels>
</vehicle>
2
ответ дан 18 December 2019 в 08:29
поделиться

Another point to consider: attributes cannot have significant whitespace in them.

1
ответ дан 18 December 2019 в 08:29
поделиться

The best practice is to favor elements over attributes. Elements are easier to access from the DOM tree and provide structure data about the data it describes. By structure data I mean that you can determine who the element's parents and children are versus what they should be or can be. This is very important from the perspective of schema where XML defined by schema is structurally self-aware. The only benefit to attributes is brevity, which can also be important. If you can do with a single attribute what would otherwise require three or four nested tags then you should probably use an attribute.

1
ответ дан 18 December 2019 в 08:29
поделиться

Your question is about attributes versus elements. Theres is a page that summarizes sources of information about attributes versus elements. You can also Google for attributes versus elements.

0
ответ дан 18 December 2019 в 08:29
поделиться
Другие вопросы по тегам:

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