Skip to main content
Sunil Yadav
Legend
May 22, 2019
Answered

how to add attribute aid:pstyle="test" in xml without importing into indesign using javascript only?

  • May 22, 2019
  • 3 replies
  • 4837 views

Hi everyone,

                    I am stuck here, need you guy's help. I have read xml and using xpath found the xml node as well, I have added namespace as well in root node of the xml. The only thing I am not able to do is add an attribute named aid:pstyle with some value in it. I need to add this attribute using javascript (not XSLT).

Need your help.

Thanks in advance

Sunil

This topic has been closed for replies.
Correct answer Sunil Yadav

A bit convoluted, but I got it going:

    var rootXE = <xml xmlns:aid="whatever"><p>Para</p><p>Para</p></xml>;

    var testXE = rootXE..p;

    default xml namespace = rootXE.namespace("aid");

    testXE[0].@pstyle = "myStyle";

    testXE[0].@pstyle.setName("aid:pstyle");

    testXE[1].@pstyle = "yourStyle";

    testXE[1].@pstyle.setName("aid:pstyle");

    $.writeln(rootXE.toXMLString());


Hi https://forums.adobe.com/people/Dirk%20Becker ,

I took reference of your code.

And found solution to my problem.

Here it is:

var newStyleName = new QName('http://ns.adobe.com/AdobeInDesign/4.0/','pstyle');

myNode.@pstyle="test Style";

myNode.@pstyle.setName(newStyleName);

I had to set its Qualified name because it contains namespace.

Best

Sunil

3 replies

priyak7746321
Inspiring
June 28, 2019

Its time consuming process. XSL is the best for conversion.

Legend
June 28, 2019

Regarding performance I agree with Priyak, XSLT will be far faster. On the other hand given the sorry state of InDesign XSL I would not invest too much into it, for my own work I prefer libxslt and exslt via plugins.

Besides we don't know the complexity of the additional logic in Sunil's script, also I hope someone else may benefit of a way to set namespaced attributes.

Sunil Yadav
Legend
June 29, 2019

Thank you guys for your time and attention.

I really appreciate it.

Thanks

Sunil

priyak7746321
Inspiring
June 26, 2019

In my knowledge Its not possible

priyak7746321
Inspiring
June 26, 2019

try with perl  or java.

Sunil Yadav
Legend
June 26, 2019

Hi priyak7746321,

I know how to do it with xslt as well.

But only need to know using InDesign JavaScript(JSX).

Thanks

Sunil

Legend
June 26, 2019

At the jsx side, you can address attributes like properties, but with an "@"  prefix.

myXMLElement = <xml/>;

myXMLElement.@attributename = "test";

$.writeln(myXMLElement.toXMLString());

When the attribute name includes characters not permitted in identifiers, use this syntax:

myXMLElement = <xml xmlns:aid="whatever"/>;

myXMLElement["@aid:pstyle"] = "value";

$.writeln(myXMLElement.toXMLString());

Note that namespaced attributes will only stick with a matching xmlns: attribute.