Adding Attributes to ID XML

Found a question on Adobe discussions that asked how to add attributes to some first level elements. The question is here.

The question is about how to add attributes when you are tagging an XML document in InDesign.

My answer follows:

Hi,
Maybe I’m too late, but…
Everyone will tell you that the best way to do this is through an XSLT done after you have exported the XHTML or XML from this document. They are right, but the learning curve (at least for me) for XSLT is steep.

Attached is an Applescript that could possibly handle this request, if all your “first level” elements contain the word “Chapter” (or some consistently recurring word.) If that does not work, try putting all your first level element names into a list variable. (in Script Editor uncomment the lines with the word “tagList” in them, and comment out the if statement with the word “contains.”

Of course, this only works on a Mac.

--Script adapted from a script by bengt on macscripter.net
--tagList {}
--set tagList to {"tag1, tag2, tag3, etc."}
tell application "Adobe InDesign CS3"
tell active document
set theRoot to (item 1 of XML elements) -- the root element
end tell
end tell
myLoopLoop(theRoot)

on myLoopLoop(myElement)

tell application "Adobe InDesign CS3"
tell active document
--if (name of markup tag of myElment is in tagList then
if (name of markup tag of myElement contains "Chapter") then
tell myElement
make XML attribute with properties {name:"yourattributename", value:"yourattributevalue"}
end tell
end if

set moreElement to every XML element of myElement
repeat with x from 1 to (count of moreElement)
tell me to myLoopLoop(item x of moreElement)
end repeat
end tell
end tell
end myLoopLoop

Notice: This script was adapted from a script posted by bengt on macscripter.net.

Advertisements

About this entry