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

Finding wrong xml tag in a table

Guide ,
Nov 20, 2017 Nov 20, 2017

Copy link to clipboard

Copied

Hi,

It the second part of the thread XML Tage name  The sample file is attached in the previous mail.

Thanks Loic for your support on the thread. He provided me  script  with a logic of count of XML tag name. But in some cases it wont work.

So the revised thing is getting the header column xml tag name and check the other in below rows. If anything found then mark the words with red color.

Tried coding:

//removing unsed xml elements

var mains = function() {    var doc = app.properties.activeDocument, 

  xes, xe; 

  if (!doc) return; 

  xes = doc.xmlElements[0].evaluateXPathExpression(".//*"); 

  while ( xe = xes.pop() ) { 

  xe.texts[0].isValid && !xe.texts[0].parentTextFrames.length && xe.remove(); 

  } 

var u; 

app.doScript("mains();",u,u,UndoModes.ENTIRE_SCRIPT);

//main function

var main = function() { 

    myDoc=app.documents[0]

corrs= { 

"Item #":"sku", 

"Description":"description",  

"Brand":"brand",  

"Product Dimensions":"product_dimensions",  

"Quantity":"quantity",  

"List Price":"list_price",

"Pen Style":"pen_style",

"Nib Type":"nib_type",

"Nib Size":"nib_size",

"Ink Color":"ink_color",

"Color":"color"

};   

var sel = app.activeDocument.textFrames, tf, ps, ln,  n = 0, tableHeader, lines,errors = [], check; 

for(z=0;z<sel.length;z++){  

tf = sel;   

var headers = {count:0};

var xe = tf.properties.associatedXMLElement;   

if ( !xe ) return;   

ps = xe.xmlContent;   

tableHeading = xe.evaluateXPathExpression ( "//table_heading" );   

if ( !tableHeading.length ) return;   

ln = tableHeading[0].xmlContent.contents.split("\t");   

n = ln.length;   

while ( n-- ) {  

headers[corrs[ln]] = 0;   

headers.count+=1;   

}   

checkContents ( ps , headers ) ;

    }   

  }

function checkContents ( story , headers ) {   

var xes = story.associatedXMLElement.evaluateXPathExpression ( "//*[name(./..)='copy_frame']" ).reverse(),  

currIndex, currNumber, n = xes.length, i = 0, nXe, xEc, xEl, xeName, data = {}, lnCount = story.lines.length; 

var myarray=[];

var myarr=[];

for (k=0; k< story.lines.length; k++)

  {

           if ((story.lines.contents).search("Item #") != -1 )

           {

           lnCount=lnCount-1-k;

           }

           else

           continue;

    }

var row=0;

for (k=0; k< story.lines.length; k++)//row length

  {

      if ((story.lines.contents).search("Item #") != -1 )

           {

                   var mycolumnlnnth=story.lines.contents.split("\t");//column split

                   for(row=k+1;row<=lnCount;row++){

                      var values=story.lines[row].contents.split("\t");//row split

                        for(column=0;column<mycolumnlnnth.length;column++){

                        if(values[column].associatedXMLElements[0].markupTag.name == "table_subhead")  {continue};

                           if (values[column].markupTag.name != corrs[mycolumnlnnth[column]])

                           {                                               

                                alert("InvalidTag: "+mycolumnlnnth[row]);

                                mycolumnlnnth([row]).contents.fillColor="Red"

                           }

                        }

                    }

      }

    }

var u;   

app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );  

Thanks,

K

TOPICS
Scripting

Views

761

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
People's Champ ,
Nov 20, 2017 Nov 20, 2017

Copy link to clipboard

Copied

Ok,

what about this road then :

var main = function() { 

var sel = app.properties.selection, tf, ps, ln, headers = {count:0}, n

 

 

if ( !sel || sel.length!=1 || !(sel[0] instanceof TextFrame) ) return

 

tf = sel[0]; 

tf2 = tf.duplicate();

table = tf2.texts[0].convertToTable();

tf2.fit ( FitOptions.FRAME_TO_CONTENT );

check = checkTable(table);

tf2.remove();

alert(

( check.result )?

"all seems fine"

:

"Content is wrong\r"+check.errors.join("\r")

);

 

var u; 

var checkTable = function(table) {

var check = {result:false, errors:[]},

cols = table.columns.everyItem().getElements(), n = cols.length, nCo

while ( n-- ) {

nCol = cols;

colCheck = checkACol ( nCol );

if ( !colCheck.result ) check.errors.push ( "Missing values for "+col

}

check.result = check.errors.length==0;

return check;

}

var checkACol = function(col) {

var cells = col.cells.everyItem().getElements(), n = cells.length, nCe

ctrl.header = cells[0].contents;

i++;

while ( i<n ) {

nCell = cells;

nCell.contents=="" && ctrl.lacking.push ( (i+1) );

i++;

}

ctrl.result = (ctrl.lacking.length == 0 );

return ctrl;

}

 

 

app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" ); 

Votes

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
Guide ,
Nov 20, 2017 Nov 20, 2017

Copy link to clipboard

Copied

Hi Loic,

Thanks again for your reply in this thread. While running the above code throw undefined is not an object in line 48. Previously bracket missing in line 36, and ctrl is undefined, so i just added.

var main = function() {   

var sel = app.properties.selection, tf, ps, ln, headers = {count:0}, n  

   

   

if ( !sel || sel.length!=1 || !(sel[0] instanceof TextFrame) ) return 

   

tf = sel[0];   

 

 

tf2 = tf.duplicate(); 

table = tf2.texts[0].convertToTable(); 

tf2.fit ( FitOptions.FRAME_TO_CONTENT ); 

 

check = checkTable(table); 

 

tf2.remove(); 

alert( 

( check.result )? 

"all seems fine" 

"Content is wrong\r"+check.errors.join("\r")  

); 

 

}   

   

var u;   

 

 

var checkTable = function(table) { 

var check = {result:false, errors:[]}, 

cols = table.columns.everyItem().getElements(), n = cols.length, nCo 

 

while ( n-- ) { 

nCol = cols

colCheck = checkACol ( nCol ); 

if ( !colCheck.result ) check.errors.push ( "Missing values for "+col )

 

check.result = check.errors.length==0; 

return check; 

 

 

 

var checkACol = function(col) { 

var cells = col.cells.everyItem().getElements(), n = cells.length, nCe, ctrl 

 

ctrl.header = cells[0].contents; 

i++; 

 

while ( i<n ) { 

nCell = cells

nCell.contents=="" && ctrl.lacking.push ( (i+1) ); 

i++; 

 

ctrl.result = (ctrl.lacking.length == 0 ); 

 

return ctrl; 

   

   

app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );   

Regards,

K

Votes

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
People's Champ ,
Nov 20, 2017 Nov 20, 2017

Copy link to clipboard

Copied

While running the above code throw undefined

Guessing that the copy/paste failed because it was there in my editor. Anyway…

Votes

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
Guide ,
Nov 21, 2017 Nov 21, 2017

Copy link to clipboard

Copied

HI Loic,

Any suggestions with my OP code, I tried again with some changes but still not fixed my issue. Could you please suggest what I am doing wrong?

//removing unsed xml elements 

var mains = function() {    var doc = app.properties.activeDocument,   

  xes, xe;   

  if (!doc) return;   

  xes = doc.xmlElements[0].evaluateXPathExpression(".//*");   

  while ( xe = xes.pop() ) {   

  xe.texts[0].isValid && !xe.texts[0].parentTextFrames.length && xe.remove();   

  }   

}   

var u;   

app.doScript("mains();",u,u,UndoModes.ENTIRE_SCRIPT);  

 

 

//main function 

var main = function() {   

    myDoc=app.documents[0] 

corrs= {   

"Item #":"sku",   

"Description":"description",    

"Brand":"brand",    

"Product Dimensions":"product_dimensions",    

"Quantity":"quantity",    

"List Price":"list_price", 

"Pen Style":"pen_style", 

"Nib Type":"nib_type", 

"Nib Size":"nib_size", 

"Ink Color":"ink_color", 

"Color":"color" 

 

 

};     

 

 

var sel = app.activeDocument.textFrames, tf, ps, ln,  n = 0, tableHeader, lines,errors = [], check;   

 

 

for(z=0;z<sel.length;z++){    

tf = sel;     

var headers = {count:0}; 

var xe = tf.properties.associatedXMLElement;     

if ( !xe ) return;     

ps = xe.xmlContent;     

tableHeading = xe.evaluateXPathExpression ( "//table_heading" );     

if ( !tableHeading.length ) return;     

ln = tableHeading[0].xmlContent.contents.split("\t");     

n = ln.length;     

while ( n-- ) {    

headers[corrs[ln]] = 0;     

headers.count+=1;     

}     

checkContents ( ps , headers ) ; 

 

 

    }     

  } 

 

 

function checkContents ( story , headers ) {     

var xes = story.associatedXMLElement.evaluateXPathExpression ( "//*[name(./..)='copy_frame']" ).reverse(),    

currIndex, currNumber, n = xes.length, i = 0, nXe, xEc, xEl, xeName, data = {}, lnCount = story.lines.length;   

var myarray=[]; 

var myarr=[]; 

for (k=0; k< story.lines.length; k++) 

  { 

           if ((story.lines.contents).search("Item #") != -1 ) 

           { 

           lnCount=lnCount-1-k; 

           } 

           else 

           continue; 

    } 

var line_contents=[], column_name=[];

var values=[];

var row=0;

for (k=0; k< story.lines.length; k++)

  {

      if ((story.lines.contents).search("Item #") != -1 )

           {

                   line_contents=story.lines.contents;                  

                   column_name = line_contents.split("\t");

                   for(row=k+1;row<=lnCount;row++){

                      if(story.lines[row].words[0].associatedXMLElements[0].markupTag.name == "table_subhead")  {continue};

                      values=story.lines[row].contents.split("\t");

                      for(column=0;column<column_name.length;column++){

                            if (story.lines[row].words[column].associatedXMLElements[0].markupTag.name  != corrs[column_name[column]])

                           {                                               

                                alert("InvalidTag"+story.lines[row].words[column].contents);

                           }

                    }

                }

          }

    }

 

 

 

 

}   

var u;     

 

 

app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" ); 

Votes

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
People's Champ ,
Nov 21, 2017 Nov 21, 2017

Copy link to clipboard

Copied

LATEST

Hi

It seems that the indeces are wrong. Logging at your code returns this:

column:2

story:[object Story]

story.lines:[object Lines]

story.lines[row]:[object Line]

story.lines[row].words:[object Words]

story.lines[row].words[column]:[object Word]

story.lines[row].words[column].associatedXMLElements[0]:[object XMLElement]

story.lines[row].words[column].associatedXMLElements[0].markupTag.name:description

column_name:Item #,Description,Brand,Product Dimensions,Quantity,List Price

column_name[column]:Brand

corrs[column_name[column]]):brand

story.lines[row].words[column].contents:Fossball

ERROR

So on index 2, the tag name is description but the column_name[column] for that index return "Brand".

Votes

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
Guide ,
Nov 20, 2017 Nov 20, 2017

Copy link to clipboard

Copied

In my script of original post, I assume the string is not supported by associatedXMLElements[0].markupTag.name option. Please suggest how to convert those to object.

Votes

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