• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

evaluateXPathExpression - find all Elements with specific attribute

Contributor ,
Mar 19, 2022 Mar 19, 2022

Copy link to clipboard

Copied

Hi,

i have an xml structure like this (without indent, just for better readability here):

 

 

 

<root>
    <master>
        <graphic_0 control="XXXX" href="File:///test.png"/>
        <textframe_0>
            <textframe_1>
                <text_1>DUMMY</text_1>
            </textframe_1>
            <text_0 control="XXXX">DUMMY</text_0>
        </textframe_0>
    </master>
</root>

 

 

 

As you can see, the <graphic_0> and the inner <text_0> element both have a "control" attribute. I'd like to get ALL elements with a control attribute, no matter in which level they are located.

 

I tried this:

 

 

// given: master is the <master> element
var xmlElements = master.evaluateXPathExpression("//*[@control]");
alert(xmlElements.length);

 

 

 But the alert always returns 1 (the <graphic_0> element), but not the inner <text_0> element.

What am I doing wrong?

EDIT: graphic_0 (that one recognized) represents a rectangle, text_0 a few tagged words

TOPICS
Scripting

Views

147

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 19, 2022 Mar 19, 2022

Hi @patMorita , try this:

 

 

var r = app.activeDocument.xmlElements
var x = r.firstItem().evaluateXPathExpression("//*[@control]");

$.writeln(r.count()) //returns 1
$.writeln(r.firstItem().markupTag.name) //returns root
$.writeln(x.length) //returns 2
$.writeln(x[0].markupTag.name) //returns graphic_0
$.writeln(x[0].xmlAttributes[0].name) //returns control

 

 

Also check this helpful thread on the .evaluateXPathExpression method:

 

https://community.adobe.com/t5/indesign-discussions/how-to-change-position-of-new-xml-element-as-immediate-sibling-to-current-xml-element-using-jsscript/td-p/12747332

...

Votes

Translate

Translate
Community Expert ,
Mar 19, 2022 Mar 19, 2022

Copy link to clipboard

Copied

LATEST

Hi @patMorita , try this:

 

 

var r = app.activeDocument.xmlElements
var x = r.firstItem().evaluateXPathExpression("//*[@control]");

$.writeln(r.count()) //returns 1
$.writeln(r.firstItem().markupTag.name) //returns root
$.writeln(x.length) //returns 2
$.writeln(x[0].markupTag.name) //returns graphic_0
$.writeln(x[0].xmlAttributes[0].name) //returns control

 

 

Also check this helpful thread on the .evaluateXPathExpression method:

 

https://community.adobe.com/t5/indesign-discussions/how-to-change-position-of-new-xml-element-as-imm...

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines