XML 消息由一系列附带由标记限定的格式和内容的元素组成。许多 XML 标记还以关联的属性的格式包括信息。元素值和元素可能使用的任何属性在树中都作为元素的子代处理。
以下表列出了必须使用来引用属性的相关名。
语法元素 | 相关名 |
---|---|
属性 | (XML.Attribute) - 也支持 (XML.attr) |
<Title Category="Computer" Form="Paperback" Edition="2">The XML Companion</Title>
元素 InputRoot.XML.Invoice.Purchases.Item[1].Title 在逻辑树中具有四个子代:Category、Form、Edition 和元素值 XML Companion。
如果您要访问此元素的属性,可以编码以下 ESQL。此代码抽取检索来自输入消息的属性并将它们作为输出消息中的元素创建。在此示例中它不处理元素本身的值。
-- 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;
Compute 节点处理此 ESQL 时,将生成以下输出消息:
<Data> <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);
<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> </Data>
您可以使用 WHERE 语句限定 SELECT 来缩小结果的范围,获取与由 WHILE 语句生成的消息相同的输出消息。这第二个示例显示了您可以使用更少的、不太复杂的 ESQL 创建相同的结果。
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');
<Data> <Attributes> <Category>Computer</Category> <Form>Paperback</Form> <Edition>2</Edition> </Attributes> </Data>