在 indesign 中通过 XML 重复替换内容

repeat replacing content via XML in indesign

我有一个脚本可以替换 Indesign 中我在 table 单元格中用标签标记的所有内容,效果很好。唯一缺少的是当我给 2 个单元格相同的标签时,脚本只替换它找到的第一个标签的内容。 所以我希望脚本替换所有具有相同标签的内容。 有没有人知道如何实现这一目标?

var main = function() {
    var doc = app.properties.activeDocument,
    root, xe, price,
    tags = <tags>

<PVDF8HNAPK40>100</PVDF8HNAPK40>
<PVDF8HNAPK50>100</PVDF8HNAPK50>
<PVDF8HNAPK63>100</PVDF8HNAPK63>
<PVDF8HNAPK75>100</PVDF8HNAPK75>

</tags>, tag, xes;


    if ( !doc ) return;

    root = doc.xmlElements[0];

    tags = tags.children(), n = tags.length();

    while ( n-- ) {
        tag = tags[n];
        xes = root.evaluateXPathExpression ( ".//"+ String(tag.name()) );
        if ( xes.length ) {
            xe = xes[0];
            xe.contents = String(tag).replace (/\./g, "," );

        }
    }
}


var u;

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

看起来好像您只需要另一个循环。为了使事情平衡,我也用 for 循环替换了外部 while 循环。

var main = function() {
    var doc = app.properties.activeDocument,
    root, xe, price, tag, xes,
    tags = <tags>
        <PVDF8HNAPK40>100</PVDF8HNAPK40>
        <PVDF8HNAPK50>100</PVDF8HNAPK50>
        <PVDF8HNAPK63>100</PVDF8HNAPK63>
        <PVDF8HNAPK75>100</PVDF8HNAPK75>
    </tags>;

    if ( !doc ) return;

    root = doc.xmlElements[0];
    tags = tags.children();

    for (var t = 0; t < tags.length(); t++) {
        tag = tags[t];
        xes = root.evaluateXPathExpression ( ".//"+ String(tag.name()) );
        for (var i = 0; i < xes.length; i++) {
            xe = xes[i];
            xe.contents = String(tag).replace (/\./g, "," );
        }
    }
}

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

免责声明:我没有 InDesign。这是未经测试的代码。