O analisador XMLNS configura o tipo de campo em cada elemento da árvore de mensagens que cria.
O tipo de campo indica o tipo de construção XML que o elemento representa. Os tipos de campo usados pelo analisador XMLNS podem ser referidos utilizando constantes com nomes prefixados com 'XML'. Constantes de tipo de campo com o prefixo 'XML' são apenas para utilização com os analisadores XMLNS e XML; elas não funcionam com os analisadores XMLNSC ou MRM.
Constante de Tipo de Campo XMLNS | |
---|---|
Marcação | XML.Element |
Atributo |
|
Utilizando o tipo de campo desta maneira, o analisador XMLNS pode distinguir entre um elemento e um atributo que possuem o mesmo nome.
Exemplo de XML
<parent id="12345">
<id>ABCDE</id>
</parent>
Exemplo de ESQL
SET value = FIELDVALUE(InputRoot.XMLNS.parent.(XML.Element)id)
Resultado : valor é 'ABCDE'
SET value = FIELDVALUE(InputRoot.XMLNS.parent.(XML.Attr)id)
Resultado : valor é '12345'
<Title Category="Computer" Form="Paperback" Edition="2">The XML Companion</Title>
o elemento InputRoot.XML.Invoice.Purchases.Item[1].Title tem quatro filhos na árvore lógica: Category, Form, Edition e o elemento
value, que é "The XML Companion".-- 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.XMLNS.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;
<Data>
<Attributes>
<Category>Computer</Category>
<Form>Paperback</Form>
<Edition>2</Edition>
</Attributes>
</Dados>
SET OutputRoot.XMLNS.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);
Esta
instrução gera a seguinte mensagem de saída:<Data>
<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>
</Dados>
SET OutputRoot.XMLNS.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');
Esta
instrução gera a seguinte mensagem de saída:<Data>
<Attributes>
<Category>Computer</Category>
<Form>Paperback</Form>
<Edition>2</Edition>
</Attributes>
</Dados>