2012-07-31 59 views
0



我导入XML到InDesign,我得到这个消息:InDesign CS5脚本:如何在导入XML时忽略DTD?

The external entity 'blahblah.dtd' cannot be found. Continue to import anyway?

当我再继续导入XML,我得到这个错误信息:

Javascript Error!

Error Number: 103237 Error String: DOM transformation error: Invalid namespace.

Engine: session File: C:\blahblah\blahblah.jsx Line: 259 Source:
obj.doc.importXML(File(xmlDoc));

..问题是,我无法访问DTD,无论如何我都不需要它。


  • 那么,有没有一个Extendscript办法忽略的DTD?
  • 如果没有,是否有一种方法可以用XSLT忽略DTD?



下面是相关代码:

function importXML(xmlDoc, xslt) 
{ 
    with(obj.doc.xmlImportPreferences) 
    { 
     importStyle = XMLImportStyles.MERGE_IMPORT; // merges XML elements into the InDesign document, merging with whatever matching content 
     createLinkToXML = true; // link elements to the XML source, instead of embedding the XML 

     // defining the XSL transformation settings here 
     allowTransform = true; // allows XSL transformation 
     transformFilename = File(xslt); // applying the XSL here 

     repeatTextElements = true; // repeating text elements inherit the formatting applied to placeholder text, **only when import style is merge! 
     ignoreWhitespace = true; // gets rid of whitespace-only text-nodes, and NOT whitespace in Strings 
     ignoreComments = true; 
     ignoreUnmatchedIncoming = true; // ignores elements that do not match the existing structure, **only when import style is merge! 
     importCALSTables = true; // imports CALS tables as InDesign tables 
     importTextIntoTables = true; // imports text into tables if tags match placeholder tables and their cells, **only when import style is merge! 
     importToSelected = false; // import the XML at the root element 
     removeUnmatchedExisting = false; 
    } 

    obj.doc.importXML(File(xmlDoc)); 
    obj.doc.mapXMLTagsToStyles(); // automatically match all tags to styles by name (after XSL transformation) 

    alert("The XML file " + xmlDoc.name + " has been successfully imported!"); 

} // end of function importXML 

...这是基于页。 407(第18章)InDesign CS5 Automation Using XML & Javascript,Grant Gamble

+0

您是否尝试过使用xslt修改xml以删除对dtd的引用? – zanegray 2012-07-31 19:10:09

+0

谢谢@zanegray,这似乎是最好的方法...我正在尝试''with' ',但它显示出这个错误:'Token'!'没有被识别。“# – 2012-07-31 19:15:42

+0

......我也试图实施在http://www.stylusstudio.com/xsllist/200104/post90620.html找到的解决方案,但是也没有工作。 – 2012-07-31 19:25:06

回答

1

好,甚至simplier。我们只需要防止交互,然后删除附加的任何dtds:

function silentXMLImport(file) 
{ 
    var doc, oldInteractionPrefs = app.scriptPreferences.userInteractionLevel; 

    if (!(file instanceof File) || !file.exists) 
    { 
     alert("Problem with file : "+file); 
    } 

    if (app.documents.length == 0) 
    { 
     alert("Open a document first"); 
     return; 
    } 

    //Prevent interaction and warnings 
    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT; 
    doc = app.activeDocument; 
    doc.importXML (file); 

    //Remove any dtd attached to the document 
    doc.dtds.everyItem().remove(); 

    app.scriptPreferences.userInteractionLevel = oldInteractionPrefs; 
} 

//Now import xml 
silentXMLImport (File (Folder.desktop+"/foobar.xml")); 

它在这里工作。

+0

谢谢@Loic,问题解决了!我很好奇 - 是否可以像'doc.namespaceDeclarations()。everyItem()。remove()'或doc.removeNamespace(doc.namespaceDeclarations()。everyItem())一样去除所有名称空间。 ? – 2012-08-01 18:20:11

+0

不确定它可以这样修复。无法看到有关命名空间的任何可访问的属性。 – Loic 2012-08-01 18:50:45

1

我认为zanegray给了你主要的概念,尽管我认为你过于复杂。 为什么不只是获取xml文件内容,用正则表达式去除tetd dtd声明,然后输出一个新的XML文件,用于输入?

//Open and retrieve original xml file content 
var originalXMLFile = File (Folder.desktop+"/foo.xml"); 
originalXMLFile.open('r'); 
var content = originalXMLFile.read(); 
//Looks for a DOCTYPE declaration and remove it 
content = content.replace (/\n<!DOCTYPE[^\]]+\]>/g , ""); 
originalXMLFile.close(); 
//Creates a new file without any DTD declaration 
var outputFile = new File (Folder.desktop+"/bar.xml"); 
outputFile.open('w'); 
outputFile.write(content); 
outputFile.close(); 

然后,您可以使用此过滤XML为您的导入。

+0

这个正则表达式只会删除一个带有内部子集('[]')的doctype,并且在同一行结束。怎么样一个没有内部子集的文档类型?如何在一个跨越多行的内部子集中包含内容的文档类型? (或者包含类似'<!ENTITY foo“[bar]”>'?我不认为正则表达式是剥离doctypes的好主意(我过去做过类似的事情,虽然删除了一切直到根元素(在doctype声明中标识)。) – 2012-07-31 19:50:14

+0

@DevNull,你是完全正确的。你的XSL很棒。 – Loic 2012-07-31 20:25:27

+0

谢谢@Loic!嗯,这会保持与原始XML文档的链接吗?有需要的原始链接,以便对XML的任何更改会自动更新InDesign文档... – 2012-07-31 20:38:27

1

这里是一个XSLT将剥离DOCTYPE声明:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <xsl:copy-of select="."/> 
    </xsl:template> 
</xsl:stylesheet> 
+0

谢谢@DevNull,但是,这不工作...我使用这个基本的XML测试这个在http://www.w3schools.com/XSL/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex2:'<?xml version =“1.0”encoding =“ utf-8“?><!DOCTYPE文章SYSTEM”blahblah.dtd“>'。 – 2012-07-31 21:17:50

+1

@IanCampbell - 我认为它不工作,因为w3schools工具试图显示HTML输出。尝试不同的处理器。另一个可以尝试的在线工具是XML Playground。试试这个保存的会话:http://www.xmlplayground.com/84o19w(不要忘记点击“查看源代码”标签查看实际输出。) – 2012-08-01 02:59:24

+0

啊,@DevNull你是正确的 - 它*是*在http://xslt.online-toolz.com/tools/xslt-transformation.php以及您提供的链接中工作。然而,它是*不*工作在InDesign不幸的.. – 2012-08-01 03:28:22

相关问题