访问 XML 消息中的属性

XML 消息由一系列附带由标记限定的格式和内容的元素组成。许多 XML 标记还以关联的属性的格式包括信息。元素值和元素可能使用的任何属性在树中都作为元素的子代处理。

以下表列出了必须使用来引用属性的相关名。

语法元素 相关名
属性 (XML.Attribute) - 也支持 (XML.attr)
在示例 Invoice 消息中,每个 Item 元素中的 Title 元素具有三个属性:Category、Form 和 Edition。例如,第一个 Title 元素包含:
<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>
您还可以使用 SELECT 语句:
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>
相关概念
消息流概述
ESQL 概述
消息建模
相关任务
设计消息流
定义消息流内容
管理 ESQL 文件
相关参考
Compute 节点
Database 节点
Filter 节点
ESQL 引用
声明 | 商标 | 下载 | 书库 | 支持 | 反馈
Copyright IBM Corporation 1999, 2006 最后一次更新时间:2006/08/14
ac17270_