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

replace paragraph return inside specific xml tag

Community Beginner ,
Apr 08, 2017 Apr 08, 2017

Copy link to clipboard

Copied

Hello Everyone,

I need help to replace within one specific xml tag, <Name>, all paragraph return with a Line break and keep the paragraph return that is already at the end of the tag.

Thanks

Paul

TOPICS
Scripting

Views

987

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

Contributor , Apr 10, 2017 Apr 10, 2017

You have to adapt this code snippets to your situation, e.g. ...

var _rootXMLElement = app.activeDocument.xmlElements.firstItem();

var _nameXMLElements = _rootXMLElement.evaluateXPathExpression("//Name");

for(var i=0; i<_nameXMLElements.length; i++) {

    app.findGrepPreferences = NothingEnum.nothing;

    app.changeGrepPreferences = NothingEnum.nothing;

  

    app.findGrepPreferences.findWhat = "~b(?=[^~b])";

    app.changeGrepPreferences.changeTo = "\n";

    _nameXMLElements.changeGrep(true);

  

    app

...

Votes

Translate

Translate
Contributor ,
Apr 09, 2017 Apr 09, 2017

Copy link to clipboard

Copied

Hello Paul,

you can try this ...

Get xml elements with evaluateXPathExpression:

RootXMLElement.evaluateXPathExpression("//Name")

And change the paragraph returns in this xml elements with changeGrep in a loop:

app.findGrepPreferences.findWhat = "~b(?=[^~b])";

app.changeGrepPreferences.changeTo = "\n";

XMLElement.changeGrep(true);

Roland

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
Community Beginner ,
Apr 09, 2017 Apr 09, 2017

Copy link to clipboard

Copied

Hello Roland,

Thank you for your help. I am getting an error message "RootXMLElement is undefined".

Could you please help? I am truly a beginner in Javascript.

Thank you again.

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 ,
Apr 10, 2017 Apr 10, 2017

Copy link to clipboard

Copied

You have to adapt this code snippets to your situation, e.g. ...

var _rootXMLElement = app.activeDocument.xmlElements.firstItem();

var _nameXMLElements = _rootXMLElement.evaluateXPathExpression("//Name");

for(var i=0; i<_nameXMLElements.length; i++) {

    app.findGrepPreferences = NothingEnum.nothing;

    app.changeGrepPreferences = NothingEnum.nothing;

  

    app.findGrepPreferences.findWhat = "~b(?=[^~b])";

    app.changeGrepPreferences.changeTo = "\n";

    _nameXMLElements.changeGrep(true);

  

    app.findGrepPreferences = NothingEnum.nothing;

    app.changeGrepPreferences = NothingEnum.nothing;

}

Roland

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
Community Beginner ,
Apr 10, 2017 Apr 10, 2017

Copy link to clipboard

Copied

Hi Roland,

Thank you again for your support. Unfortunately the code still showing the error " undefined is not an objet". I am not sure what to do.Error.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
Participant ,
Apr 10, 2017 Apr 10, 2017

Copy link to clipboard

Copied

The first line add;

#target indesign

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
Community Beginner ,
Apr 10, 2017 Apr 10, 2017

Copy link to clipboard

Copied

Thank you uniqued_tol​ but now I am getting : Result: undefined

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
Community Beginner ,
Apr 11, 2017 Apr 11, 2017

Copy link to clipboard

Copied

Hello Everyone,

I am getting result: undefined. And after running the script in indesign nothing change in the document.

Error_resultUndefined.PNG

Can you please help.

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 ,
Apr 13, 2017 Apr 13, 2017

Copy link to clipboard

Copied

60705211.jpg

The console would only log what YOU would return. As your code returns anything but undefined, console displays…

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
Community Beginner ,
Apr 13, 2017 Apr 13, 2017

Copy link to clipboard

Copied

Yes, I understand. So what should I do?

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 ,
Apr 13, 2017 Apr 13, 2017

Copy link to clipboard

Copied

//Some code

//some code

//some code

"yolo" //will output "yolo" in the console

or if you are using a function

function yolo() {

return "yolo";

}

yolo();

//will output "yolo" in the console

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 ,
Apr 13, 2017 Apr 13, 2017

Copy link to clipboard

Copied

var _rootXMLElement = app.activeDocument.xmlElements.firstItem(); 

var _nameXMLElements = _rootXMLElement.evaluateXPathExpression("//Name"); 

var count = 0;

 

for(var i=0; i<_nameXMLElements.length; i++) { 

    app.findGrepPreferences = NothingEnum.nothing; 

    app.changeGrepPreferences = NothingEnum.nothing; 

    

    app.findGrepPreferences.findWhat = "~b(?=[^~b])"; 

    app.changeGrepPreferences.changeTo = "\n"; 

    count+=_nameXMLElements.changeGrep(true).length; 

    

    app.findGrepPreferences = NothingEnum.nothing; 

    app.changeGrepPreferences = NothingEnum.nothing; 

}

alert(count+" replacements have been made…");

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
Community Beginner ,
Apr 13, 2017 Apr 13, 2017

Copy link to clipboard

Copied

Thank you so much, Loic.AigonThe script is working now. Also my many thanks to drerol74 uniqued_tol

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 ,
Apr 13, 2017 Apr 13, 2017

Copy link to clipboard

Copied

You could have scored drerol74​ as I mostly used his snippet here

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
Community Beginner ,
Apr 13, 2017 Apr 13, 2017

Copy link to clipboard

Copied

LATEST

Yes, You are right. I will do that. Sorry.

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
Community Beginner ,
Apr 13, 2017 Apr 13, 2017

Copy link to clipboard

Copied

Can you please suggest the code I should put to have the result. The problem actualy is that when I run the code in Indesign nothing happen. All I wanted to do is : to replace within one specific xml tag, <Name>, all paragraph return(\r) with a Line break(\n) and keep the paragraph return that is already at the end of the tag.

Thanks for your help.

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