version 2004
DOM Create XML element (elementRef; xPath{; attrName; attrValue}{; attrName2; attrValue2; ...; attrNameN; attrValueN}) String
Parameter | Type | Description | |
elementRef | String | Root XML element reference | |
xPath | Text | XPath path of the XML element to create | |
attrName | String | Attribute to set | |
attrValue | String | New attribute value | |
Function result | String | Reference of the created XML element |
Description
The DOM Create XML element command allows you to create a new element in the XML element elementRef in the path set by the xPath parameter and to add attributes to it if necessary.
Pass the root element reference in elementRef (created, for example, using the DOM Create XML Ref command).
In xPath, pass the access path in XPath notation of the element to create (see the "Use of XPath notation" paragraph in the Presentation of XML Commands section). If any path elements do not exist, they are created.
It is possible to pass a simple item name directly in the xPath parameter in order to create a sub-item from the current item (see example 3).
Note: If you have defined one or more namespaces for the tree set using elementRef (see the DOM Create XML Ref command), you must precede the xPath parameter with the namespace to be used (for example, "MyNameSpace:MyElement").
You can pass attribute/attribute value pairs (in the form of variables, fields or literal values) in the optional attrName and attrValue parameters. You can pass as many pairs as you want.
The command returns the XML reference of the element created as a result.
Examples
1. We want to create the following element:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <RootElement> <Elem1> <Elem2> <Elem3> </Elem3> <Elem3> </Elem3> </Elem2> </Elem1> </RootElement>
To do so, simply write:
C_STRING(16;vRootRef;vElemRef) vRootRef:=DOM Create XML Ref("RootElement") vxPath:="/RootElement/Elem1/Elem2/Elem3" vElemRef:=DOM Create XML element(vRootRef;vxPath) vxPath:="/RootElement/Elem1/Elem2/Elem3[2]" vElemRef:=DOM Create XML element(vRootRef;vxPath)
2. We want to create the following element (containing attributes):
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <RootElement> <Elem1> <Elem2> <Elem3 Font=Verdana Size=10> </Elem3> </Elem2> </Elem1> </RootElement>
To do so, simply write:
C_STRING(16;vRootRef;vElemRef) C_STRING(80;$aAttrName1;$aAttrName2;$aAttrVal1;$aAttrVal2) $aAttrName1:="Font" $aAttrName2:="Size" $aAttrVal1:="Verdana" $aAttrVal2:="10" vRootRef:=DOM Create XML Ref("RootElement") vxPath:="/RootElement/Elem1/Elem2/Elem3" vElemRef:=DOM Create XML element(vRootRef;vxPath;$aAttrName1;$aAttrVal1;$aAttrName2;$aAttrVal2)
3. We want to create and export the following structure:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <Root> <Elem1>Hello</Elem1> </Root>
We want to use the syntax based on a simple item name. To do this, simply write:
C_STRING(16;$root) C_STRING(16;$ref1) $root:=DOM Create XML Ref ("Root") DOM SET XML ELEMENT VALUE($ref1;"Hello") $ref1:=DOM Create XML element($root;"Elem1") DOM EXPORT TO FILE($root;"mydoc.xml") DOM CLOSE XML($root)
See Also
DOM Get XML element, DOM REMOVE XML ELEMENT.
System Variables or Sets
If the command was executed correctly, the system variable OK is set to 1. Otherwise, it is set to 0 and an error is generated.
Error Handling
An error is generated when:
The root element reference is invalid.
The name of the element to create is invalid (for example, if it starts with a number).