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

Get xml Elements of a paragraphs

Contributor ,
Oct 09, 2019 Oct 09, 2019

Copy link to clipboard

Copied

Hi,

 

 How to get all xml elements in a selected paragraph to get the aid:pstyle count of a paragraph.  Because a paragraph style is got overwritten of 2nd style. how to get pstyle count using xml element or paragraph?

 

Please refer the attached screenshot. selected paragraph xml element is 2nd li of of ul and p of ul next element.  If i get accosicated xml element its returning ul element so i will get the 1st li element of ul but selected paragraph doest have that text. How to check aid pstyle of selected paragraphs text element or paragraph? selected elements of a paragraphs.

 

 

1.png

 

 
 

 

 

xml.png

TOPICS
Scripting

Views

889

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

correct answers 1 Correct answer

Advocate , Oct 13, 2019 Oct 13, 2019

Try this code sudka_k:

 

//////////////////////////////////////////////////////////////////////////////////
var myDoc = app.documents[0];
var myNode = app.selection[0];
var totalAttributes = [];
var myId = [myNode];
if(myNode.constructor.name == "XMLElement"){
    for(var xid = 0; xid < myNode.paragraphs[0].words.length; xid++){
        var allXML = myNode.paragraphs[0].words[xid].associatedXMLElements;
        for(var x = 0; x < allXML.length; x++){
            var match = false;
            for(var lid =

...

Votes

Translate

Translate
Advocate ,
Oct 10, 2019 Oct 10, 2019

Copy link to clipboard

Copied

Hi sudha_k,

Try this code:

////////////////////////////////////////////////////////////////////////////////////

var totalAttributes = [];
getAttr(app.selection[0].associatedXMLElements);
alert(totalAttributes);
function getAttr(myNodes){
    for(var i = 0; i < myNodes.length; i++){
      if(myNodes[i].xmlAttributes.item("aid:pstyle").isValid){

            totalAttributes.push(myNodes[i].xmlAttributes.item("aid:pstyle").value.toString());

        }
    if(myNodes[i].parent.constructor.name != "Document") getAttr([myNodes[i].parent]);
    }
}

////////////////////////////////////////////////////////////////////////////////////

Best

Sunil

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
Contributor ,
Oct 11, 2019 Oct 11, 2019

Copy link to clipboard

Copied

Hi,

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
Contributor ,
Oct 11, 2019 Oct 11, 2019

Copy link to clipboard

Copied

Hi,

 

 Thanks for your reply...

 

Please see my screenshots 1 & 2. In Document, i am selecting the element p (last element) and if i select the paragraph of the of that p and run the code it returns empty. Because of the tag structure.

 

Paragraphs is merged with the tag of "p" from "ul/li" and the bottom "p" tag. so if i get associatedXML element its return ul element only but selection of the paragraph text tags are one "p" tag from "ul/li" and the bottom "p". 

 

I want to check if selected paragraph contains 2 pstyle i need to place enter mark in front the element of the 2nd pstyle element. i dont know how to check this?

 

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
Advocate ,
Oct 11, 2019 Oct 11, 2019

Copy link to clipboard

Copied

Are you saying that you will select a tag not the text in it? If is it so a little modification in code is required.

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
Advocate ,
Oct 11, 2019 Oct 11, 2019

Copy link to clipboard

Copied

Try this code:

//////////////////////////////////////////////////////////////////////////////////
var totalAttributes = [];
if(app.selection[0].constructor.name == "XMLElement"){
    getAttr([app.selection[0]]);
    }
if(totalAttributes.length > 1){
    app.selection[0].insertTextAsContent("\r", XMLElementPosition.beforeElement);
    }
//////////////////////////////////////////////////////////////////////////////////
function getAttr(myNodes){
    for(var i = 0; i < myNodes.length; i++){
        if(myNodes[i].xmlAttributes.item("aid:pstyle").isValid){
            totalAttributes.push(myNodes[i].xmlAttributes.item("aid:pstyle").value.toString());
            }
        if(myNodes[i].parent.constructor.name != "Document") getAttr([myNodes[i].parent]);
        }
    }
//////////////////////////////////////////////////////////////////////////////////

 

Best

Sunil

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
Advocate ,
Oct 11, 2019 Oct 11, 2019

Copy link to clipboard

Copied

Hi sudha_k,

Try this code : 

 

//////////////////////////////////////////////////////////////////////////////////

var totalAttributes = [];
if(app.selection[0].constructor.name == "XMLElement"){
    getAttr([app.selection[0]]);
    }
else if(app.selection[0].constructor.name == "InsertionPoint" || app.selection[0].constructor.name =="Character" || app.selection[0].constructor.name =="Word" || app.selection[0].constructor.name == "Paragraph" || app.selection[0].constructor.name == "Text"){
    getAttr(app.selection[0].associatedXMLElements);
    }
alert(totalAttributes);
//////////////////////////////////////////////////////////////////////////////////
function getAttr(myNodes){
    for(var i = 0; i < myNodes.length; i++){
        if(myNodes[i].xmlAttributes.item("aid:pstyle").isValid){
            totalAttributes.push(myNodes[i].xmlAttributes.item("aid:pstyle").value.toString());
            }
        if(myNodes[i].parent.constructor.name != "Document") getAttr([myNodes[i].parent]);
        }
    }

//////////////////////////////////////////////////////////////////////////////////

 

Best

Sunil

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
Contributor ,
Oct 12, 2019 Oct 12, 2019

Copy link to clipboard

Copied

Hi Sunil,

 

   The same is returning.  I think that i did not say clearly my requirement.

 

  Please see my both screenshots. As per below screenshot, xml element "p" (outside of ul element) proceeded with ul tag with two list items.

 

XML_1.png

 

  See the below screenshot, 1st paragraph is "ul/p" and 2nd paragraph is combination of ul/p and p (outside tag).

 

XML_2.png

 

 As per xml elements, its a combination of li/p and p and it contains 2 pstyle.   But in paragraph text (selection) style got overrides.  The originals style for p tag (selection) is "bch_tx" but its wrongly here.

 

XML_3.png

 I need to findout these kind of paragraphs and need to place enter infront that element (ie., element which contains 2nd pstyle) Whether using paragraph or xml element. I am trying in both way but not working.

 

 The sample output is:

 

 

XML_4.png

 

 

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
Advocate ,
Oct 13, 2019 Oct 13, 2019

Copy link to clipboard

Copied

LATEST

Try this code sudka_k:

 

//////////////////////////////////////////////////////////////////////////////////
var myDoc = app.documents[0];
var myNode = app.selection[0];
var totalAttributes = [];
var myId = [myNode];
if(myNode.constructor.name == "XMLElement"){
    for(var xid = 0; xid < myNode.paragraphs[0].words.length; xid++){
        var allXML = myNode.paragraphs[0].words[xid].associatedXMLElements;
        for(var x = 0; x < allXML.length; x++){
            var match = false;
            for(var lid = 0; lid < myId.length; lid++){
                if(myId[lid].id == allXML[x].id){
                    match = true;
                    }
                }
            if(!match) myId.push(allXML[x]);
            }
        }
    getAttr(myId);
    }
if(totalAttributes.length > 1){
    myNode.insertTextAsContent("\r", XMLElementPosition.beforeElement);
    }
//////////////////////////////////////////////////////////////////////////////////
function getAttr(myNodes){
    for(var i = 0; i < myNodes.length; i++){
        if(myNodes[i].xmlAttributes.item("aid:pstyle").isValid){
            totalAttributes.push(myNodes[i].xmlAttributes.item("aid:pstyle").value.toString());
            }
        if(myNodes[i].parent.constructor.name != "Document") getAttr([myNodes[i].parent]);
        }
    }
//////////////////////////////////////////////////////////////////////////////////

 

Best

Sunil

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