InDesign CS5脚本:导入XML时如何忽略DTD?

时间:2012-07-31 18:12:28

标签: xml dtd adobe-indesign extendscript



我正在将XML导入InDesign,我收到此消息:

  

无法找到外部实体'blahblah.dtd'。继续导入   呢?

当我继续导入XML时,我收到以下错误消息:

  

Javascript错误!

     

错误号:103237错误字符串:DOM转换错误:无效   命名空间。

     

引擎:会话文件:C:\ blahblah \ blahblah.jsx行:259来源:
  obj.doc.importXML(File(xmlDoc));

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

<小时/>

  • 那么,是否存在一种忽略DTD的Extendscript方式?
  • 如果没有,有没有办法忽略使用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

......这是基于p。格兰特赌博的InDesign CS5 Automation Using XML & Javascript第407页(第18章)

3 个答案:

答案 0 :(得分:1)

我认为zanegray为您提供了主要概念,尽管我认为您过于复杂。 为什么不只是获取xml文件内容,使用regexp删除teh 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进行导入。

答案 1 :(得分:1)

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

<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>

答案 2 :(得分:1)

好的,甚至更简单。我们只需要阻止交互,然后删除任何附加的dtd:

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" ) );

它在这里工作。