Reputation: 37
Can someone help me in creating the below xml structure using ESQL in IIB
Input:
<animal>
<animaldomestic>dog<animaldomestic>
<animalwild>cheetah<animalwild>
</animal>
Output:
<animals>
<animal type="domestic">cow</animal>
<animal type="wild">cheetah</animal>
</animals>
Upvotes: 0
Views: 2646
Reputation: 2422
@Egorka_nazarov's solution is the best. A couple of improvements are possible, though:
FOR refAnimals AS InputRoot.XMLNSC.animal.*[];
CREATE LASTCHILD OF OutputRoot.XMLNSC.animals
AS refNewAnimal
TYPE NameValue
NAME 'animal'
VALUE FIELDVALUE(refAnimals);
DECLARE type CHARACTER SUBSTRING(FIELDNAME(refAnimals) AFTER 'animal');
SET refNewAnimal.(XMLNSC.Attribute)type = type;
END FOR;
The above code is shorter and less likely to contain bugs (once you have practiced with REFERENCE variables, obviously).
Upvotes: 2
Reputation: 11
If you want universal code:
DECLARE animal REFERENCE TO InputRoot.XMLNSC.animal.*[>];
DECLARE type CHAR;
DECLARE I INTEGER 1;
WHILE LASTMOVE(animal) DO
SET type = SUBSTRING(FIELDNAME(animal) AFTER 'animal');
SET OutputRoot.XMLNSC.animals.animal[I] = FIELDVALUE(animal);
SET OutputRoot.XMLNSC.animals.animal[I].(XMLNSC.Attribute)type = type;
SET I = I + 1;
SET type = '';
MOVE animal NEXTSIBLING;
END WHILE;
Upvotes: 1
Reputation:
SET OuputRoot.XMLNSC.animals.(XMLNSC.Attribute)animal = 'domestic'; SET OutputRoot.XMLNSC.animals.animal = 'cow';
SET OuputRoot.XMLNSC.animals.(XMLNSC.Attribute)animal = 'wild'; SET OutputRoot.XMLNSC.animals.animal = 'cheetah';
Upvotes: 3
Reputation: 37
I found solution for this.Please find the below code:
SET OutputRoot.XMLNSC.animals.animal[1].(XMLNSC.Attribute)type = 'domestic';
SET OutputRoot.XMLNSC.animals.animal[1]VALUE = InputRoot.XMLNSC.animal.animaldomestic;
SET OutputRoot.XMLNSC.animals.animal[2].(XMLNSC.Attribute)type = 'wild';
SET OutputRoot.XMLNSC.animals.animal[2]VALUE = InputRoot.XMLNSC.animal.animalwild;
Upvotes: 0