Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

Set XML attribute from SetValue using Xpath

Avatar

Former Community Member

Is there a trick to assigning a value to an attribute of an element in an XML variable using XPATH inside a setValue activity?  When I try something like

Location

/process_var/xml_var/test/@ID

Expression:

/process_var/test_value

it complains about the @ sign in the location assignment.  I seem to be able to retrieve attributes just fine.

8 Replies

Avatar

Former Community Member

Hi,

Did you try following?

Location

/process_var/xml_var/test[@ID]

regards,

Parth

Avatar

Former Community Member

Yup.  Tried all manner of variants.  Tried messing with the XSD.  Tried not using an XSD.  Bottom line, I don't seem to be able to set ONLY the attribute of an element.

Avatar

Former Community Member

Just tried it here and it worked fine using your original syntax:

/process_var/xml_var/test/@ID

I do find it strange that you woudl have an ID attribute off of the root node test. Is that your structure? You must path from the root to the appropriate node:

/process_var/xml_var/test/node1/node2/@ID

where ID is an attribute of node2.

If that does not work post your XML and I will give it a try here.

Paul

Avatar

Former Community Member

Still having this issue.

If I have an XML process variable which currently contains

<Document>

    <Title />

    <Author />

    <Date />

</Document>

Try this, it fails.

Location: /process_data/xml/Document/@ID

Expression: 54

Try this, it works.

Location: /process_data/xml/Document/ID

Expression: 54

Now, alter the XML by adding an ID attribute

<Document ID="">

    <Title />

    <Author />

    <Date />

</Document>

Try this, it works, where it failed earlier.

Location: /process_data/xml/Document/@ID

Expression: 54

It appears to me, you can ADD a new node, but you can't ADD an attribute

Avatar

Former Community Member

Some more testing.

If I have an XML process variable which currently contains

<Document>

    <Title />

    <Author />

    <Date />

</Document>

Create a process variable, small_xml, which looks like <Date ID="27822"/>

Try this, it doesn't throw an error, but it doesn't set the value.

Location: /process_data/xml/Document/Date

Expression: /process_data/small_xml/Date

Create a process variable, small_xml, which looks like <Date><ID>27822</ID></Date>

Try this, it works.

Location: /process_data/xml/Document/Date

Expression: /process_data/small_xml/Date

Avatar

Former Community Member

Hi,

I think you are right. The setValue will create the element if it does not exist. BUT that's not the case with the attribute. I also couldn't get the setvalue operation to create the attribute.

In your last post what are you trying to do? Do you want to assign attribute value to Date element? Creating ID element as child element of Date still has the same issue when you want to create element attribute on at runtime.

Avatar

Former Community Member

That's a specific instance, but in general, yes, I want to be able to assign a process variable to an attribute without the attribute being specified ahead of time.

I think my options are

  1. Use a DSC to do my XML manipulation
  2. Create the ID attribute ahead of time, and just leave it blank
  3. Do string manipulation and coerce to XML.
  4. Dont use attributes, only use elements

Avatar

Former Community Member

Hi,

I think you have put correct options. Here are my preferences in easier to harder order.

4. Dont use attributes, only use elements

2. Create the ID attribue ahead of time, and just leave it blank ( This has to be done outside of orchestration or you'll be back to square one) (But you can always use option 3 for this to make it simpler)

3. Do the manipulation and coerce to xml

1. Use a dsc to do xml manipulation. I know XSLT can do this but I find XQuery component much easier to do xml manipulation.

Parth Pandya

Blog: http://blog.pandyaparth.com