Skip to main content
AJMrly
Participating Frequently
June 7, 2015
Answered

Create inline text frame from contents of an xml tag?

  • June 7, 2015
  • 1 reply
  • 2624 views

Hi! I have a rather large document created from an XML file, and I'd like to convert a series of my XML tags to anchored text frames so I can hang them off the left edge of lines they appear in.

My XML file is laid out as

>root

>chapter

>para

>id

1

>verse

text

>id

>verse

etc... see file snippet here http://1drv.ms/1dX6lYO

So far I have the following, which gets me an array containing all the id elements.

var myDocument = app.activeDocument;

var myXMLidElement = myDocument.xmlElements.itemByName("book").xmlElements.itemByName("c").xmlElements.itemByName("p").xmlElements.itemByName("id");

var myXMLidText= (myXMLidElement.contents);

var myXMLidLength = myXMLidText.length;

for (var i = 0; i < myXMLidLength; i++) {  //loop to go through

    //alert (myXMLidText);

    myDocument.select(myXMLidText[0]);  //this gives me an error saying expected data type needs to be array. Can someone explain? Where do I find info on the select command?

    myDocument.selection[0].insertionPoints[0].textFrames.add(myXMLidText[0]); //haven't managed to get to this line yet, but I want it to create an inline text frame at the point of the selected text and set the frame's contents to be my XML Id Text

I suppose I'd then want to delete all my id text too, but that's unimportant.

Some other random snippets I could use?

myInlineFrame.contents = "myXMLidText";

Any pointers on creating the inline frames and logic loop? I'm an old hand with InDesign, but new to scripting, so be gentle.

This topic has been closed for replies.
Correct answer Sajeev Sridharan

Try this,

var myDocument = app.activeDocument; 

var root = myDocument.xmlElements[0];

var myXMLidElement = root.evaluateXPathExpression("//book/c/p/id");

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

{

    myDocument.select(myXMLidElement);

    var myTextframe = myXMLidElement.placeIntoInlineFrame(["1p","1p"]);

}

1 reply

Sajeev SridharanCorrect answer
Legend
June 8, 2015

Try this,

var myDocument = app.activeDocument; 

var root = myDocument.xmlElements[0];

var myXMLidElement = root.evaluateXPathExpression("//book/c/p/id");

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

{

    myDocument.select(myXMLidElement);

    var myTextframe = myXMLidElement.placeIntoInlineFrame(["1p","1p"]);

}

AJMrly
AJMrlyAuthor
Participating Frequently
June 8, 2015

Hi Sajeev,

Thanks for the response. It doesn't seem to be doing anything yet, but that's probably because I don't understand it. Note that I've moved everything up a level, chapters now appear under the root tag as the book tag was superfluous. Here's what I think is going on and this is bound to show how limited my understanding is:

function myXMLSnippet(){

var myDocument = app.activeDocument;

var root = myDocument.xmlElements[0];//set root equal to the XML document root

var myXMLidElement = root.evaluateXPathExpression("//c/p/id"); /*set XMLidElement equal to a specific tag in the DOM.

What data type is this? Is it the name of the tag as it appears in XML or the actual text in between the <id></id> tags? How is it stored?*/

//alert (myXMLidElement);

for (var i = 0; i < myXMLidElement.length; i++) //Length is the number of these tags in the entire document?

{

  myDocument.select(myXMLidElement); //does this place the cursor at the point the tag appears in my text?

  var myTextframe = myXMLidElement.placeIntoInlineFrame(["1p","1p"]); //creates an inline text frame within my existing text frame at that point in my text flow? What does the 1p mean?

}

}

If I'm completely wrong, please explain. If I'm just mostly wrong, also please explain. This is a huge help! I can't believe I got a response.

Regards,

Andrew

Legend
June 8, 2015

Hi Andrew,

Please find my response below:

  1. var myXMLidElement = root.evaluateXPathExpression("//c/p/id"); /*set XMLidElement equal to a specific tag in the DOM.
  2. What data type is this? Is it the name of the tag as it appears in XML or the actual text in between the <id></id> tags? How is it stored?*/ 
  3. //alert (myXMLidElement); 

Evaluates an XPath expression starting at this XML element in the structure, it stores all the properties like tag name, tag contents etc.

  • You need tag name means you have to code like myXMLidElement.markupTag.name
  • you need contents of the tag means - myXMLidElement.contents

  1. for (var i = 0; i < myXMLidElement.length; i++) //Length is the number of these tags in the entire document? 

Number of tags (i.e., for your case "id" tags) appears in the entire document

myDocument.select(myXMLidElement);

Nope this will select the XML tag (for your case "id" tags) in the document

var myTextframe = myXMLidElement.placeIntoInlineFrame(["1p","1p"]);

This line will create inline frame in your existing textframe and "1p" (1 pica) means size of your inline text frame (you can change the size as per your requirements)

HTH