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
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
Copy link to clipboard
Copied
try with perl or java.
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
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.
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
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());
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
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
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
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
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.
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
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
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
Copy link to clipboard
Copied
Hi https://forums.adobe.com/people/Marc%20Autret​ & https://forums.adobe.com/people/Dirk%20Becker
Can you suggest something on this: Is there any xml structure for creating and applying group paragraph style?
Because here I am clueless.
Thanks
Sunil
Copy link to clipboard
Copied
working fine in read mode
Copy link to clipboard
Copied
i wanted to open in write mode and edit in xml is it possible
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
Copy link to clipboard
Copied
Sunil,
that's "encoding" without the "h".
Copy link to clipboard
Copied
My bad... Mispelled
Copy link to clipboard
Copied
In my knowledge Its not possible
Copy link to clipboard
Copied
Its time consuming process. XSL is the best for conversion.
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.
Copy link to clipboard
Copied
Thank you guys for your time and attention.
I really appreciate it.
Thanks
Sunil