从另一个文件中查找并替换 XML 标签之间的内容

Find and replace content between XML tags from another file

我想要完成的是: 我想自动更新我的 indesign 目录中的 10.000 个价格。 唯一的方法是使用 XML。 当我在 indesign 中标记我的 table 单元格时,我可以导出一个 XML 文件,看起来像这样。

    -<Root>
-<Artikel>
-<Tabel aid:tcols="5" aid:trows="6" aid:table="table" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">
<Cel aid:table="cell" aid:ccols="5" aid:crows="1">.: Rollerflam </Cel>
<Cel aid:table="cell" aid:ccols="5" aid:crows="1"/>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="79.29730708729332"/>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="65.97828346376967">Artikel code</Cel>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="144.56692913385828">Omschrijving</Cel>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="42.519685039370074">Prijs / st.</Cel>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="34.01574803149606">PG</Cel>
<Cel aid:table="cell" aid:ccols="1" aid:crows="3" aid:ccolwidth="79.29730708729332"/>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="65.97828346376967">AIRK7274</Cel>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="144.56692913385828">Rollerflampost </Cel>
-<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="42.519685039370074">

<AIRK7274>717,00</AIRK7274>

</Cel>

<Cel aid:table="cell" aid:ccols="1" aid:crows="3" aid:ccolwidth="34.01574803149606">-</Cel>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="65.97828346376967">AIRK7275</Cel>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="144.56692913385828">Rollerflampost met lastoebehoren</Cel>
+<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="42.519685039370074">
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="65.97828346376967">AIRK7277</Cel>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="144.56692913385828">Rollerflampost +las-en snijtoebehoren</Cel>
-<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="42.519685039370074">

<AIRK7277>1055,00</AIRK7277>

</Cel>
</Tabel>
</Artikel>

-<Artikel>
-<Tabel aid:tcols="5" aid:trows="7" aid:table="table" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">
<Cel aid:table="cell" aid:ccols="5" aid:crows="1">.: Junior flessen </Cel>
<Cel aid:table="cell" aid:ccols="5" aid:crows="1"/>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="79.29730708729332"/>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="65.97828346376964">Artikel code</Cel>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="144.56692913385828">Omschrijving</Cel>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="42.519685039370074">Prijs / st.</Cel>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="34.01574803149606">PG</Cel>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="79.29730708729332"/>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="65.97828346376964">AIRK7272</Cel>
<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="144.56692913385828">Fles met vulling van 2.30m³ zuurstof</Cel>
-<Cel aid:table="cell" aid:ccols="1" aid:crows="1" aid:ccolwidth="42.519685039370074">

<AIRK7272>345,00</AIRK7272>

</Cel>

这些只是几篇文章。 现在我有了所有这些带有标签的文章列表,看起来像这样

<AIRK7274>222</AIRK7274>
<AIRK7275>222</AIRK7275>
<AIRK7277>1055</AIRK7277>
<AIRK7272>345</AIRK7272>
<AIRK7271>309</AIRK7271>
<AIRK7276>310</AIRK7276>
<AIRK7270>310</AIRK7270>
<AIR01150>42</AIR01150>
<AIR02150>42</AIR02150>
<AIR08193>42</AIR08193>
<AIR08197>42</AIR08197>
<AIR73142>39,5</AIR73142>
<AIRK7250>291</AIRK7250>
<AIRK7280>435</AIRK7280>
<BI1410>15,746</BI1410>

是否可以用我的文件内容替换indesign文件XML标签之间的内容? 我读过 XML 解析器和正则表达式,但我还不是很了解。 谁能试着给我一个很好的解释,并告诉我是否有可能实现我想要实现的目标?非常感谢!

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

这是一种通过访问 InDesign 脚本的可能方法 DOM:

var main = function() {
 var doc = app.properties.activeDocument,
 root, xe, price,
 tags = <tags>
<AIRK7274>222</AIRK7274>
<AIRK7275>222</AIRK7275>
<AIRK7277>1055</AIRK7277>
<AIRK7272>345</AIRK7272>
<AIRK7271>309</AIRK7271>
<AIRK7276>310</AIRK7276>
<AIRK7270>310</AIRK7270>
<AIR01150>42</AIR01150>
<AIR02150>42</AIR02150>
<AIR08193>42</AIR08193>
<AIR08197>42</AIR08197>
<AIR73142>39,5</AIR73142>
<AIRK7250>291</AIRK7250>
<AIRK7280>435</AIRK7280>
<BI1410>15,746</BI1410>
</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(Number ( String(tag).replace (/,/g, "." ) )*2).replace (/\./g, "," );
  }
 }
}


var u;

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

确实,"times 2" 是您可以做的事的一个例子。在需要时 "round" 个数字,它们有很多方法,但快速修复可能包括:

xe.contents = String(Number ( String(tag).replace (/,/g, "." ) )*2).replace (/\./g, "," ).replace ( ',00', '' );

@Loic

我想我找到了答案。 我删除了

xe.contents = String(Number ( String(tag).replace (/,/g, "." ) )*2).replace (/\./g, "," );

并将其替换为

xe.contents = String(tag).replace (/,/g, "." ) .replace (/\./g, "," );

似乎有效,问题已解决,谢谢 Loic 的帮助!