InDesign CS5 Script: How can I ignore the DTD when importing XML?

佐手、 提交于 2019-11-28 12:18:51

问题




I am importing XML into InDesign, and I get this message:

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

And when I then continue to import the XML, I get this error message:

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

...the problem is, is that I won't have access to the DTD, and I won't need it for my purposes anyway.


  • So, is there a Extendscript way to ignore the DTD?
  • If not, is there a way to ignore the DTD with XSLT?



Here is the relevant code:
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

...this is based on p. 407 (Chapter 18) of InDesign CS5 Automation Using XML & Javascript, by Grant Gamble


回答1:


Ok, even simplier. We just have to prevent interaction and then remove any dtds attached:

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

It's working here.




回答2:


Here's an XSLT that will strip the DOCTYPE declaration:

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



回答3:


I think zanegray gave you the main concept although I think you overcomplicate stuff. Why not just getting xml file content, remove teh dtd declaration with a regexp and then output a new XML File that will be used for input ?

//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();

You can then use this filtered xml for your import.



来源:https://stackoverflow.com/questions/11746415/indesign-cs5-script-how-can-i-ignore-the-dtd-when-importing-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!