• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Get the CDATA content Count

Participant ,
Feb 05, 2023 Feb 05, 2023

Copy link to clipboard

Copied

Hi Everyone,

 

It would be helpful for me to get a count of the CDATA content in the XML file. For your convenience, I have attached the script I used to get the CDATA and InDesign content. 

var allCDATA = [];  
myXMl = app.activeDocument.xmlElements[0];
var myXmlNode = myXMl.evaluateXPathExpression("//Root");
var myFrame =  app.activeDocument.textFrames;
        app.findChangeGrepOptions = NothingEnum.nothing;
        app.findGrepPreferences = NothingEnum.nothing;
 for (var i = 0; i<myFrame.length; i++) {
       
        var mySelectedXMLItem = myFrame[i].associatedXMLElement;
        if(mySelectedXMLItem != null && mySelectedXMLItem.markupTag.name != null)
        {
         var node_TBGUID = myFrame[i].associatedXMLElement.xmlAttributes.itemByName("TBGUID").value;
        var myFrames = myFrame[i].texts[0].contents; 
        var xmlTree = GetXMLFile();
         var content = new XML(xmlTree);
        var myDoc = app.activeDocument;
        myLinks = myDoc.links;
    for (j = myLinks.length - 1; j >= 0; j--)
    {  
        myName = myLinks[j].filePath;
        var ext =  myName.substring(myName.length-3,myName.length) ;
        if (ext=="xml")
        {  
            var myXMLFile = File(myName);  
            var myResult = myXMLFile.open("r", undefined, undefined);
            if(myResult == true){ 
                var myXMLDefaults = myXMLFile.read(); 
                myXMLFile.close(); 

            } // if(myResult)
        } // if(ext)
    } // for
var elements =getAllAttributeValues ("TBGUID");
//alert(elements)
for(var k =  0;k<elements.length;k++)
{
        if(elements[k] == node_TBGUID)
        {
            var getxmlcontent = getCDATAWithID(myXMLFile, "TBGUID", elements[k]); // Get the CDATA values
            var testL=myFrames;
            alert(testL)
            var testR=getxmlcontent.toString().replace("<br />","").replace("<br />","").replace("<br />","");
            alert(testR)
       } ///// elements[k] == node_TBGUID
} //// for (k)
}
    }  // first for loop

function GetXMLFile()
{
	
		myDoc = app.activeDocument;
		myLinks = myDoc.links;
		for (j = myLinks.length - 1; j >= 0; j--)
		{  
			myName = myLinks[j].filePath; 
			var ext =  myName.substring(myName.length-3,myName.length) ;
			if (ext=="xml")
			{   
				var myXMLFile = File(myName);  
				var myResult = myXMLFile.open("r"); 
				if(myResult == true)
				{ 
				var myXMLDefaults = myXMLFile.read(); 
				myXMLFile.close(); 
				var myXMLDefaults = new XML(myXMLDefaults); 
				return myXMLDefaults;
				}			
		}//for
	}
	
}
function getAllAttributeValues(attributeName){
    var allAttributes = [];
    var allNodes = content[0].xpath("//*[@"+attributeName+"]");
    for(var i = 0; i < allNodes.length(); i++){
        allAttributes.push(allNodes[i].attribute(attributeName).toString());
        }
    if(allAttributes.length > 0){
      //  alert(allAttributes)
      //  getChildData(allAttributes)
        return allAttributes;
        
        }
    else{
        return null;
        }
    }

function getChildData(nodeData){
    for(var c = 0; c < nodeData.children().length(); c++){
        if(nodeData.children()[c].toXMLString().toString().indexOf("<![CDATA[") == 0){
     //  alert(nodeData.children()[c].toString());
            allCDATA.push(nodeData.children()[c].toString());
            }
        if(nodeData.children()[c].children().length() > 0){
            getChildData(nodeData.children()[c]);
            }
        }
    }
function getCDATAWithID(inputXML, attribute, id){
    var CDATA = [];
    if(inputXML.exists == true) {
        inputXML.encoding = "UTF-8";
        inputXML.open('r');
        var text = inputXML.read();
        inputXML.close();
        var content = new XML(text);
        var elements = content.xpath("//CDATA");
        for(var n = 0; n < elements.length(); n++){
            if(elements[n].parent()["@"+attribute].toString() == id){
                CDATA.push(elements[n].toString());
           //    alert(CDATA)
                }
            }
        }
    return CDATA;
    }
-Monisha
TOPICS
How to , Scripting

Views

83

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
no replies

Have something to add?

Join the conversation