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

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

Advocate ,
May 22, 2019 May 22, 2019

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

2.9K

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

Advocate , Jun 28, 2019 Jun 28, 2019

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

Votes

Translate

Translate
Participant ,
Jun 26, 2019 Jun 26, 2019

Copy link to clipboard

Copied

try with perl  or java.

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
Advocate ,
Jun 26, 2019 Jun 26, 2019

Copy link to clipboard

Copied

Hi priyak7746321,

I know how to do it with xslt as well.

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

Thanks

Sunil

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
Guide ,
Jun 26, 2019 Jun 26, 2019

Copy link to clipboard

Copied

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.

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
Advocate ,
Jun 26, 2019 Jun 26, 2019

Copy link to clipboard

Copied

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

I dont want to create XML.

solve this:

var myXMLFile = File("~/Desktop/Test.xml")

myXMLFile.open('r');

var allElements = new XML(myXMLFile.read());

myXMLFile.close();

var nodes = allElements[0].xpath("//p");

for(var i = 0; i < nodes.length(); i++){

    // Add attributes aid:pstyle="test"

    }

Thanks

Sunil

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
Guide ,
Jun 26, 2019 Jun 26, 2019

Copy link to clipboard

Copied

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());

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
Advocate ,
Jun 26, 2019 Jun 26, 2019

Copy link to clipboard

Copied

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

I can see here you are trying to create a XML.

Here is a xml, Just try to read it and add aid:pstyle into this:

<?xml version='1.0' encoding='UTF-8'?><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>

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

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

</root>

Thanks

Sunil

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
Advocate ,
Jun 28, 2019 Jun 28, 2019

Copy link to clipboard

Copied

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

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
Guide ,
Jun 29, 2019 Jun 29, 2019

Copy link to clipboard

Copied

Hi Sunil,

I still don't see the problem with Dirk's technique (or equivalent). What is wrong with:

// ...incoming xml read from a file or whatever

var nodes = xml.xpath('//p');

for each( var x in nodes ) x['aid::@pstyle'] = "test";

?

Can you summarize what you've discovered?

Best,

Marc

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
Advocate ,
Jun 30, 2019 Jun 30, 2019

Copy link to clipboard

Copied

Hi Marc Autret,

Setting name is taking only local name.

But if you are willing to set a property name for using a name space.

You have to set it is qualified name only.

I tried his code, it didn't work out.

After that I explored a bit more instead of giving up.

Found the difference n again I give it a shot to qualified name by setting name space into it.

Finally I got it.

If property name doesn't belong to name space, setName is working fine.

But with name space only qualified name will work.

Thanks

Sunil

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
Guide ,
Jul 01, 2019 Jul 01, 2019

Copy link to clipboard

Copied

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.

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
Advocate ,
Jul 01, 2019 Jul 01, 2019

Copy link to clipboard

Copied

Meanwhile, If we want to add any local attribute then we can set any name with any string, but when it comes to a attributes from a name space we've to set it as qualified name only giving its URI and attribute name which will directly take namespace's prefix anyway, we don't have to define prefix there (Just like I did).

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


Thanks

Sunil

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
Guide ,
Jul 01, 2019 Jul 01, 2019

Copy link to clipboard

Copied

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

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
Contributor ,
Jul 02, 2019 Jul 02, 2019

Copy link to clipboard

Copied

Hi!

I've also struggled with this issue before:

var _rootXML = new XML("<xml xmlns:aid='http://xyz.at'><p>Para</p><p>Para</p></xml>");

var _paraXML = _rootXML.xpath("//p");

var _aidNamespace = _rootXML.namespace("aid");

_paraXML[0].@["pstyle"] = "stylname1";

_paraXML[0].@["pstyle"].setNamespace(_aidNamespace);

$.writeln(_rootXML.toXMLString());

But it's nearly the same solution.

Roland

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
Advocate ,
Jul 02, 2019 Jul 02, 2019

Copy link to clipboard

Copied

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
Explorer ,
Oct 26, 2019 Oct 26, 2019

Copy link to clipboard

Copied

LATEST

working fine in read mode 

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
Explorer ,
Oct 20, 2019 Oct 20, 2019

Copy link to clipboard

Copied

i wanted to open in write mode and edit in xml is it possible 

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
Advocate ,
Oct 21, 2019 Oct 21, 2019

Copy link to clipboard

Copied

You can simply open xml file as text file and read text as xml (if it is valid xml) & write into it like text file.

 

//////////////////////////////////

file.open('r');

var myXMLString = new XML(file.read().toString());

file.close();

//////////////////////////////////

// Manipulate your myXMLString here

//////////////////////////////////

file.open('w');

file.encoding = 'UTF-8';

file.write(myXMLString);

file.close();

//////////////////////////////////

 

Best

Sunil

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
Guide ,
Oct 22, 2019 Oct 22, 2019

Copy link to clipboard

Copied

Sunil,

that's "encoding" without the "h".

 

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
Advocate ,
Oct 22, 2019 Oct 22, 2019

Copy link to clipboard

Copied

My bad... Mispelled smiley.png 

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
Participant ,
Jun 26, 2019 Jun 26, 2019

Copy link to clipboard

Copied

In my knowledge Its not possible

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
Participant ,
Jun 28, 2019 Jun 28, 2019

Copy link to clipboard

Copied

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

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
Guide ,
Jun 28, 2019 Jun 28, 2019

Copy link to clipboard

Copied

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.

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
Advocate ,
Jun 28, 2019 Jun 28, 2019

Copy link to clipboard

Copied

Thank you guys for your time and attention.

I really appreciate it.

Thanks

Sunil

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