XML Node text problems

Unfortunately there was a very long pause for me, but now I try to get back into normal life.

I’m trying to create ITT XMLs. Fortunately I haven’t forgot the basics but there is one thing I haven’t figured our yet.
Here a very simplified description.

If I have some text:

Line 1
Line 2
Line 3

The ITT text cue should look like:

<p begin="00:00:00:00" end="00:00:10:00">Line 1<br/>Line 2<br/>Line 3</p>

Here the simplified code:

set someCue to "Line 1
Line 2
Line 3"

set txParList to every paragraph of someCue
set parCount to (count of txParList)

set parTx to (xmlNode's textWithStringValue:((item 1 of txParList) as string))
(theP's addChild:(parTx))

repeat with p from 2 to parCount
	set brNode to (xmlNode's elementWithName:"br" stringValue:"")
	(theP's addChild:(brNode))
	
	set parTx to (item p of txParList) as string
	set parTx to (xmlNode's textWithStringValue:(parTx))
	(theP's addChild:(parTx))
end repeat

And I get this result:

<p begin="00:00:00:00" end="00:00:10:00">Line 1<br></br>
    Line 2<br></br>
    Line 3</p>

This is only partially what I expect, since “Line 2” and “Line 3” have extra white space.
Any help to point me in the right direction is really appreciated.

Andreas

It’s not a string element. Try this:

set brNode to current application's NSXMLNode's alloc()'s initWithKind:(current application's NSXMLElementKind) options:(current application's NSXMLNodeCompactEmptyElement)
brNode's setName:"br"

Many thanks Shane,

You finally answered a question of mine which I didn’t want to ask yet but probably would have asked later :smiley:

My problem wasn’t the empty line break node. The problem is the whitespace before each of Line 2 and Line 3 etc

Line 1
   Line 2
   Line 3

instead of

Line 1
Line 2
Line 3

Means something like “append value” instead of “add”.

Best
Andreas

I don’t see the space here. It might have something to do with how you create theP, or some other option you’ve set elsewhere.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set someCue to "Line 1
Line 2
Line 3"

set theP to current application's NSXMLNode's elementWithName:"p"

set txParList to every paragraph of someCue
set parCount to (count of txParList)

set parTx to (current application's NSXMLNode's textWithStringValue:((item 1 of txParList) as string))
(theP's addChild:(parTx))

repeat with p from 2 to parCount
	set brNode to (current application's NSXMLNode's alloc()'s initWithKind:(current application's NSXMLElementKind) options:(current application's NSXMLNodeCompactEmptyElement))
	(brNode's setName:"br")
	(theP's addChild:(brNode))
	
	set parTx to (item p of txParList) as string
	set parTx to (current application's NSXMLNode's textWithStringValue:(parTx))
	(theP's addChild:(parTx))
end repeat
return theP
--> (NSXMLElement) <p>Line 1<br/>Line 2<br/>Line 3</p>

Hi Shane,

You’re right. But that makes me feel worse :frowning:

Here a bit more complete stuff. I got a XML:

<?xml version="1.0" encoding="UTF-8"?>
<tt xml:lang="en-US" xmlns="http://www.w3.org/ns/ttml" xmlns:ttm="http://www.w3.org/ns/ttml#metadata" xmlns:tts="http://www.w3.org/ns/ttml#styling" xmlns:ttp="http://www.w3.org/ns/ttml#parameter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ttp:timeBase="smpte" ttp:dropMode="nonDrop" ttp:frameRate="24" ttp:frameRateMultiplier="999 1000">
    <head>
        <styling>
            <style xml:id="normal" tts:color="white" tts:fontStyle="normal" tts:fontWeight="normal" tts:fontSize="100%" tts:fontFamily="sansSerif"></style>
            <style xml:id="italic" tts:color="white" tts:fontStyle="italic" tts:fontWeight="normal" tts:fontSize="100%" tts:fontFamily="sansSerif"></style>
        </styling>
        <layout>
            <region xml:id="top" tts:displayAlign="before" tts:origin="0% 0%" tts:extent="100% 15%" tts:textAlign="center"></region>
            <region xml:id="bottom" tts:displayAlign="after" tts:origin="0% 85%" tts:extent="100% 15%" tts:textAlign="center"></region>
        </layout>
    </head>
    <body region="bottom" style="normal">
        <div>
            <p begin="00:00:00:00" end="00:00:10:00">old text</p>
        </div>
    </body>
</tt>

And I have the script

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

property cA : a reference to current application
property xmlNode : a reference to cA's NSXMLNode


set theFilePath to POSIX path of (choose file)
set theURL to current application's |NSURL|'s fileURLWithPath:theFilePath
set {theXML, theError} to current application's NSXMLDocument's alloc()'s initWithContentsOfURL:theURL options:(current application's NSXMLNodePreserveWhitespace) |error|:(reference)
if theXML = missing value then error (theError's localizedDescription() as text)
set theRoot to (theXML's rootElement)

set {theNodeList, theError} to theXML's nodesForXPath:("//*:p") |error|:(reference)
if theNodeList = missing value then
	set theNodeList to {}
end if

set theP to item 1 of theNodeList

set someCue to "Line 1
Line 2
Line 3"

set txParList to every paragraph of someCue
set parCount to (count of txParList)

(theP's setStringValue:((item 1 of txParList) as string) resolvingEntities:(true))

repeat with p from 2 to parCount
	set brNode to (current application's NSXMLNode's alloc()'s initWithKind:(current application's NSXMLElementKind) options:(current application's NSXMLNodeCompactEmptyElement))
	(brNode's setName:"br")
	(theP's addChild:(brNode))
	
	set parTx to (item p of txParList) as string
	set parTx to (xmlNode's textWithStringValue:(parTx))
	(theP's addChild:(parTx))
end repeat

--return theP --> <p>Line 1<br/>Line 2<br/>Line 3</p>

set check to (theRoot's XMLStringWithOptions:(cA's NSXMLNodePrettyPrint)) as string
set the clipboard to check

The “check” then unfortunately shows

...
    <p begin="00:00:00:00" end="00:00:10:00">Line 1<br/>
        Line 2<br/>
        Line 3</p>

Andreas

That’s because you’ve asked for NSXMLNodePrettyPrint – it’s pretty print in action.

That just came into my mind.
So I replaced the NSXMLNodePrettyPrint with NSXMLNodePreserveAll.
That seems to work but instead of double quotes (") now only single quotes (’) are used in the XML.
I haven’t seen that before and don’t know about overall consequences.

Andreas

Try NSXMLNodeUseDoubleQuotes.

Thanks again, but I don’t know how to do :frowning:

set check to (theRoot's XMLStringWithOptions:((cA's NSXMLNodePreserveAll) + (cA's NSXMLNodeUseDoubleQuotes))) as string

doesn’t work

Andreas

I’m not sure why. Double quotes should be the default.

Maybe my understanding of NSXMLNodePrettyPrint is wrong, but

...
    <p begin="00:00:00:00" end="00:00:10:00">Line 1<br/>
        Line 2<br/>
        Line 3</p>
…

and

...
    <p begin="00:00:00:00" end="00:00:10:00">Line 1<br/>
Line 2<br/>
Line 3</p>
...

are different. Latter “could” be a “pretty print”, the first though is just wrong.

Andreas