Sidebar

invalid index number for predicate

0 votes
177 views
asked Aug 24, 2022 by randy-c-3449 (200 points)

This is a two part question:

  1. How do I use variables for XML element instances
  2. How do I set attributes using the addChild method (if possible)

I'm trying to build an XML message where I can add child nodes using a variable as the node index.  

message.addChild('/dataSets/dataset','selectField','',s);
message.addChild('/dataSets/dataset/selectField','@name', 'someValue'); 

Where "s" is the variable with the index so I can have repeating nodes based on a for loop.  

I've tried different things like; 

message.addChild('/dataSets/dataset/selectField[' + s + ']','@name', 'someValue'); 

or

message.addChild('/dataSets/dataset/selectField' + [s],'@name', 'someValue');

 

If I try this without an index, it kind of works except instead of setting an attribute it sets another node:

<dataSets><dataset><selectField><@name>someValue</@name></selectField.</dataset></dataSets>

where it should be:

<dataSets><dataset><selectField name="someValue"</selectField></dataset></dataSets>

1 Answer

0 votes

There are two types of elements that you're adding, and as such there are two methods to use.

To add a new child node to the XML, you would use message.addChild(). The addChid() function will add a new child node below the node specified.  This is used to add complete nodes, but not used to add attributes.

To add an attribute, you will use message.setNode().  The setNode() function is used to create new items, as well as update existing items.  When the item you're specifying (in this case, an XML attribute) does not exist, QIE will add that item.

Let's take a look at your XML:

<dataSets>
    <dataset>
        <selectField name="someValue"></selectField>
    </dataset>
</dataSets>

Let's add a new child node below /dataSets/dataset, called selectField2, and give it the value of 'some data'.

message.addChild('/dataSets/dataset', 'selectField2', 'some data');

The resulting XML looks like this:

<dataSets>
   <dataset>
      <selectField name="someValue"></selectField>
      <selectField2>some data</selectField2>
   </dataset>
</dataSets>

Now we want to add an attribute to our newly added tag.  We will use message.setNode() to do that, like this:

message.setNode('/dataSets/dataset/selectField2/@name', 'someValue2');

This results in this XML:

<dataSets>
   <dataset>
      <selectField name="someValue"></selectField>
      <selectField2 name="someValue2">some data</selectField2>
   </dataset>
</dataSets>

 

 

answered Aug 25, 2022 by jon-t-7005 (7,590 points)
...