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
  • 4840 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

Marc Autret
Legend
July 1, 2019

Hi Marc,

during my tests, the namespaced attribute would not get created when the namespace was not present on the very same element - parent/root element would not be sufficient. On the other hand, I tried "@aid:pstyle" rather than your "aid::@pstyle" .

Interesting, let's give that another try:

function main()

{

    var rootXE = <root xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/">

  <p>This is first paragrah</p>

  <p>This is not first paragrah</p>

</root>;

    var testXE = rootXE..p;

    testXE[0]["aid::@pstyle"] = "myStyle";

    return  rootXE.toXMLString();

}

main();

Result, targeting a CC 2018 InDesign:

<root xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/">
  <p xmlns:aid="aid" aid:pstyle="myStyle">This is first paragrah</p>
  <p>This is not first paragrah</p>
</root>

So while the attribute was added even with a namespace prefix, the element also got a wrong local namespace for that prefix.


Hi Dirk,

Thanks. After posting I did the same tests and got the same results (with the same conclusions!)

Note. — The syntax x['aid::@pstyle'] used in my example is formally invalid (although supported in ExtendScript). According to ECMA-357 (E4X), the correct syntax would be x['@aid::pstyle']

I also tried variants of x.@yyy::pstyle = "test" where the variable yyy refers either to a string (the namespace prefix), a pure Namespace object —e.g myXML.namespaceDeclarations()[0]— or even the desired qualified name like new QName(myNamespace,'pstyle').

In any case indeed the result is something like a fake extra namespace and never the one declared in the root xmlns declaration.

So, as stated by Sunil, it seems that we must create the QName before the loop, say

   // ...incoming xml...

   var qn = new QName( xml.namespaceDeclarations()[0], 'pstyle' );

then we need two statements for each node:

   for each( var x in nodes ){ x.@pstyle = "test"; x.@pstyle.setName(qn); }

That's ugly but we have no better option so far.

I now understand why E4X has been abandoned…

Best,

Marc