Is it possible to use an xpath string on xml within a script activity? | Community
Skip to main content
December 23, 2021
Solved

Is it possible to use an xpath string on xml within a script activity?

  • December 23, 2021
  • 2 replies
  • 1381 views

I'd like to be able to use xpath on an xml rather than E4X, I know E4X works but I'd like to be able to use string expressions.
e.g.

var xml = new XML( <root> <item count="4"/> </root> );

given the code above I'd like to be able to do something like xml.getValue("//@count").

 

I see that there is a method in the docs that should do what I want however i haven't been able to use it successfully.
https://experienceleague.adobe.com/developer/campaign-api/api/m-DOMElement-getValue.html

 

If anyone could please help I would be extremely grateful.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Jonathon_wodnicki

Hi,

 

Doc link isn't accurate here- XML objects in workflows don't have getValue().

 

Here are e4x equivalents for xpaths though, from https://svn.wso2.org/repos/wso2/tags/carbon/0.1alpha/mashup/java/xdocs/e4xquickstart.html:

XPathMeaningE4X Equivalent

element/*

Select all children of element

element.*

element/@*

Select all attributes of element

element.@*

element//descendent

Select all descendents (children, grandchildren, etc.) of element

element..descendent

.. or parent::element

Select the parent of element

element.parent()

xmlns:foo="..."

element/foo:bar

Select the foo:bar child of element where foo is the prefix of a declared namespace

var foo = new Namespace(...);

element.foo::bar

name(element)

Return the full name (including prefix if any) of element

element.name()

local-name(element)

Return the local name of element

element.localName()

namespace-uri(element)

Return the namespace uri (if any) of element

element.namespace()

element/namespace::*

Return the collection of namespaces as an Array of Namespace objects (E4X) or a nodeset of Namespaces nodes (XPath)

element.inScopeNamespaces()

element/processing-instructions(name)

Return the processing instruction children of element with the specified name (if omitted, all are returned).

element.processingInstructions(name)

string(element)

Return the concatenated text nodes of this element and all its descendants

stringValue(element);

stringValue.visible = false;
function stringValue(node) {
    var value = "";
    if (node.hasSimpleContent()) {
        value = node.toString();
    } else {
        for each (var c in node.children()) {
            value += stringValue(c);
        }
    }
    return value;
}

 

xml.getValue("//@count") -> xml..@count 

 

Thanks,

-Jon

2 replies

Jyoti_Yadav
Level 8
December 24, 2021

Hi @m083743 ,

 

Use root.item.@count 

 

Thanks.

m083743Author
December 24, 2021

Hi Jyoti,

I'm well aware E4X syntax works but it won't allow me to use a string expression without using eval. I'm looking for something that allows me to use string expressions.

 

Thanks.

Jonathon_wodnicki
Community Advisor
Jonathon_wodnickiCommunity AdvisorAccepted solution
Community Advisor
January 2, 2022

Hi,

 

Doc link isn't accurate here- XML objects in workflows don't have getValue().

 

Here are e4x equivalents for xpaths though, from https://svn.wso2.org/repos/wso2/tags/carbon/0.1alpha/mashup/java/xdocs/e4xquickstart.html:

XPathMeaningE4X Equivalent

element/*

Select all children of element

element.*

element/@*

Select all attributes of element

element.@*

element//descendent

Select all descendents (children, grandchildren, etc.) of element

element..descendent

.. or parent::element

Select the parent of element

element.parent()

xmlns:foo="..."

element/foo:bar

Select the foo:bar child of element where foo is the prefix of a declared namespace

var foo = new Namespace(...);

element.foo::bar

name(element)

Return the full name (including prefix if any) of element

element.name()

local-name(element)

Return the local name of element

element.localName()

namespace-uri(element)

Return the namespace uri (if any) of element

element.namespace()

element/namespace::*

Return the collection of namespaces as an Array of Namespace objects (E4X) or a nodeset of Namespaces nodes (XPath)

element.inScopeNamespaces()

element/processing-instructions(name)

Return the processing instruction children of element with the specified name (if omitted, all are returned).

element.processingInstructions(name)

string(element)

Return the concatenated text nodes of this element and all its descendants

stringValue(element);

stringValue.visible = false;
function stringValue(node) {
    var value = "";
    if (node.hasSimpleContent()) {
        value = node.toString();
    } else {
        for each (var c in node.children()) {
            value += stringValue(c);
        }
    }
    return value;
}

 

xml.getValue("//@count") -> xml..@count 

 

Thanks,

-Jon