Working With Xml Stories - Adobe 65009333 - InCopy CS4 - PC Manual

Scripting guide: javascript
Hide thumbs Also See for 65009333 - InCopy CS4 - PC:
Table of Contents

Advertisement

XML
The following script fragment shows how to add an XML attribute to an XML element (for the complete
script, see MakeXMLAttribute):
var myDocument = app.documents.item(0);
var myRootXMLElement = myDocument.xmlElements.item(0);
var myXMLElementB = myRootXMLElement.xmlElements.item(1);
myXMLElementB.xmlAttributes.add("example_attribute", "This is an XML attribute. It
will not appear in the layout!");
In addition to creating attributes directly using scripting, you can convert XML elements to attributes.
When you do this, the text contents of the XML element become the value of an XML attribute added to
the parent of the XML element. Because the name of the XML element becomes the name of the attribute,
this method can fail when an attribute with that name already exists in the parent of the XML element. If
the XML element contains page items, those page items are deleted from the layout.
When you convert an XML attribute to an XML element, you can specify the location where the new XML
element is added. The new XML element can be added to the beginning or end of the parent of the XML
attribute. By default, the new element is added at the beginning of the parent element.
You also can specify an XML mark-up tag for the new XML element. If you omit this parameter, the new
XML element is created with the same XML tag as the XML element containing the XML attribute.
The following script shows how to convert an XML element to an XML attribute (for the complete script,
see the ConvertElementToAttribute tutorial script):
var myDocument = app.documents.add();
var myRootXMLElement = myDocument.xmlElements.item(0);
var myXMLTag = myDocument.xmlTags.add("myXMLElement");
var myXMLElement = myRootXMLElement.xmlElements.add(myXMLTag);
myXMLElement.contents = "This is content in an XML element.";
myXMLElement.convertToAttribute();
//Place the XML content so that you can see the result of the change.
var myStory = myDocument.stories.item(0);
myStory.placeXML(myRootXMLElement);
You also can convert an XML attribute to an XML element, as shown in the following script fragment (from
the ConvertAttributeToElement tutorial script):
var myRootXMLElement = myDocument.xmlElements.item(0);
var myXMLElementB = myRootXMLElement.xmlElements.item(1);
//The "at" parameter can be either LocationOptions.atEnd or
LocationOptions.atBeginning, but cannot
//be LocationOptions.after or LocationOptions.before.
myXMLElementB.xmlAttributes.item(0).convertToElement(XMLElementLocation.elementEnd,
myDocument.xmlTags.item("myXMLElement"));

Working with XML stories

When you import XML elements that were not associated with a layout element (a story or page item),
they are stored in an XML story. You can work with text in unplaced XML elements just as you would work
with the text in a text frame. The following script fragment shows how this works (for the complete script,
see XMLStory):
Scripting XML Elements 82

Advertisement

Table of Contents
loading

This manual is also suitable for:

Incopy cs4

Table of Contents