的InDesign CS5脚本:如何导入XML时,我忽略了DTD?(InDesign CS5 Scr

2019-06-26 15:58发布



我导入XML到InDesign,我得到这个消息:

外部实体“blahblah.dtd”无法找到。 继续反正导入?

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

JavaScript错误!

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

发动机:会议文件:C:\ blahblah \ blahblah.jsx线:259来源:
obj.doc.importXML(文件(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自动化使用XML和Javascript ,格兰特宝洁

Answer 1:

好吧,即使simplier。 我们只需要防止的互动,然后删除任何附加的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" ) );

它在这里工作。



Answer 2:

下面是将剥离的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>


Answer 3:

我想zanegray给你的主要概念,但我认为你过于复杂的东西。 为什么不只是让XML文件内容,取出用正则表达式,然后输出将被用于输入一个新的XML文件德DTD声明?

//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为您的导入。



文章来源: InDesign CS5 Script: How can I ignore the DTD when importing XML?