As mensagens XML consistem em uma seqüência de elementos com formato e conteúdo delimitados por marcações. Muitas marcações XML também incluem informações no formato de atributos associados. O valor do elemento e quaisquer atributos que o elemento possa ter, são tratados na árvore como filhos do elemento.
A tabela a seguir lista o nome de correlação que deve ser utilizado para referir-se a atributos.
Elemento de Sintaxe | Nome de correlação |
---|---|
Attribute | (XML.Attribute) - (XML.attr) também é suportado |
<Title Category="Computer" Form="Paperback" Edition="2">The XML Companion</Title>
O elemento InputRoot.XML.Invoice.Purchases.Item[1].Title possui quatro filhos na árvore lógica: Category, Form, Edition e o valor de elemento, que é The XML Companion.
Se desejar acessar os atributos para este elemento, você poderá codificar o seguinte ESQL. Esta extração de código recupera os atributos da mensagem de entrada e cria-os como elementos na mensagem de saída. Ela não processa o valor do próprio elemento neste exemplo.
-- Set the cursor to the first XML.Attribute of the Title, note the * after -- (XML.Attribute) meaning any name, because the name might not be known DECLARE cursor REFERENCE TO InputRoot.XML.Invoice.Purchases.Item[1] .Title.(XML.Attribute)*; WHILE LASTMOVE(cursor) DO -- Create a field with the same name as the XML.Attribute and set its value -- to the value of the XML.Attribute SET OutputRoot.XML.Data.Attributes.{FIELDNAME(cursor)} = FIELDVALUE(cursor); -- Move to the next sibling of the same TYPE to avoid the Title value -- which is not an XML.Attribute MOVE cursor NEXTSIBLING REPEAT TYPE; END WHILE;
Quando esse ESQL for executado pelo nó Compute, a seguinte mensagem de saída será gerada:
<Dados> <Attributes> <Category>Computer</Category> <Form>Paperback</Form> <Edition>2</Edition> </Attributes> </Data>
SET OutputRoot.XML.Data.Attributes[] = (SELECT FIELDVALUE(I.Title) AS title, FIELDVALUE(I.Title.(XML.Attribute)Category) AS category, FIELDVALUE(I.Title.(XML.Attribute)Form) AS form, FIELDVALUE(I.Title.(XML.Attribute)Edition) AS edition FROM InputRoot.XML.Invoice.Purchases.Item[] AS I);
<Dados> <Attributes> <title>The XML Companion</title> <category>Computer</category> <form>Paperback</form> <edition>2</edition> </Attributes> <Attributes> <title>A Complete Guide to DB2 Universal Database</title> <category>Computer</category> <form>Paperback</form> <edition>2</edition> </Attributes> <Attributes> <title>JAVA 2 Developers Handbook</title> <category>Computer</category> <form>Hardcover</form> <edition>0</edition> </Attributes> </Data>
Você pode qualificar SELECT com uma instrução WHERE para estreitar os resultados para obter a mesma mensagem de saída que a que foi gerada pela instrução WHILE. Este segundo exemplo mostra que você pode criar os mesmos resultados com menos ESQLs e menos complexos.
SET OutputRoot.XML.Data.Attributes[] = (SELECT FIELDVALUE(I.Title.(XML.Attribute)Category) AS category, FIELDVALUE(I.Title.(XML.Attribute)Form) AS form, FIELDVALUE(I.Title.(XML.Attribute)Edition) AS edition FROM InputRoot.XML.Invoice.Purchases.Item[] AS I WHERE I.Title = 'The XML Companion');
<Dados> <Attributes> <Category>Computer</Category> <Form>Paperback</Form> <Edition>2</Edition> </Attributes> </Data>