Copy link to clipboard
Copied
Hi,
I am getting xml elements using xpath. I have used the below code to get the xml elements. Its returning null for a document but that document contains the specified xml elements.
var xPath = "//title"
var doc = app.activeDocument;
var myRoot = doc.xmlElements.item(0);
if( !myRoot.xmlAttributes.itemByName("xmlns:aid").isValid ){
myRoot.xmlAttributes.add("xmlns:aid", "http://ns.adobe.com/AdobeInDesign/4.0/" );
}
alert("myRoot "+myRoot.markupTag.name);
var elts = myRoot.evaluateXPathExpression(xPath);
alert( "Length:"+elts.length + "\nelts:" +elts )
If i remove the attributes exists (selection), its working. Is it right way? will it affect anything??
Thanks in advance,
Sudha K
Copy link to clipboard
Copied
var xPath = "//*:title"
Copy link to clipboard
Copied
Its also returns 0 elements. If i delete those selected attributes then only i can get elements. Is it right way??
Reading xml elements recursively and xpath is different and having time different?? When document having thousands of elements its taking long time to process.. How Can i reduce the time using script??
Copy link to clipboard
Copied
Removing the xmlns attributes is wrong as long the matching namespaces are still used.
A quick hack and some alternatives were mentioned in Re: How to access XML elements by name in Extendscript??
As you're talking about longer XML documents, consider an outside XSLT processor to strip down the XML to something better usable.
ExtendScript XML will give you hard times anyway with large documents - e.g. better don't pass big sub-trees around as function arguments.
In some cases the InDesign xml import with its prehistoric XSLT processor and different trouble spots could also be an alternative.
Copy link to clipboard
Copied
Sorry... what will the function do??... what is the parameter xml?? is it indesign xml object?? ie., xml = app.activeDocument.xmlElements.item(0)?
function removeAllNamespace(xml)
{
var ns =new Namespace();
var d=xml.descendants();
for (var i=0;i<d.length();i++)
d.setNamespace(ns);
}
I need to get the positions of specified xml element contents in an InDesign document. For this, how should use indesign only? or there is anyother possiblites?? I completed the process but i could not use this process due to script running time.... Don't know wat to do...
Copy link to clipboard
Copied
Sorry that I did not make that clear: the other thread is mainly about ExtendScript's E4X XML objects, while you already work with InDesign document based XML. E4X has its own XPath implementation, so if you just need to look up something from any smaller XML that would be an alternative.
In your InDesign document based scenario I would use an XSLT to change the XML during import into something that matches your InDesign layout needs. E.g. strip unused elements, remove namespaces from elements and attributes, rearrange them as you need. Eventually import into the document's XML story rather than into text stories (backing text frames).
One typical problem with document XML is a missing xmlns:xml namespace attribute, it will hurt XPath searches as soon you use e.g. an xml:lang attribute. In that case have a look at this thread.
Copy link to clipboard
Copied
Thank you....
I need to add the below one also with the indesign namespace and then have to use xpath to evaluate evaluateXPathExpression. Is it right??
if( !myRoot.xmlAttributes.itemByName("xmlns:xml").isValid )
myRoot.xmlAttributes.add( "xmlns:xml", "http://www.w3.org/XML/1998/namespace" );
I have added this but no result for the below code. If i change the xpath to "//*" its returning count. For the xpath "//section" its returning 0;
var xPth = "//section";
var doc = app.activeDocument
var myRoot = doc.xmlElements.item(0);
if( !myRoot.xmlAttributes.itemByName("xmlns:xml").isValid )
{
myRoot.xmlAttributes.add( "xmlns:xml", "http://www.w3.org/XML/1998/namespace" );
}
if( !myRoot.xmlAttributes.itemByName("xmlns:aid").isValid ){
myRoot.xmlAttributes.add("xmlns:aid", "http://ns.adobe.com/AdobeInDesign/4.0/" );
}
elts = myRoot.evaluateXPathExpression(xPth);
alert(elts.length)
Copy link to clipboard
Copied
I had the same problem a while ago. I described my solution -- Prepare xml-attributes script -- here at the bottom of the page. In brief, it temporarily changes : (colons) to _ (uderscore) and, at the end, restores them back to colons.
—Kas
Copy link to clipboard
Copied
Thank you....
I will check and let you know...
One more thing, I have used xpath to process . It returns 1000's of elements. will it affect the processing speed??
My ques is, if we use recursive element it will take time and it affect the process speed. When we use xpath it dont affect speed. Can you pls anyone clarify me...
Copy link to clipboard
Copied
As adding xmlns:xml did not help, I would look for other namespaces deeper in the document.
For Kasyan's approach it will probably be faster to perform the suggested changes via an XSLT executed during import. On the other hand that assumes XSLT knowledge.