General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion Tip: Validating the main document from a file entity; also cleaning bad PIs

  • dcramer

    Tip: Validating the main document from a file entity; also cleaning bad PIs

    Participants 1
    Replies 0
    Last Activity 14 years, 3 months ago

    Out-of-the-box, XMetaL can't validate file entities because they don't have DOCTYPEs. I do the following in On_Before_Document_Validate to validate the parent doc. This looks for , where path_to/parent.xml is the full path to the parent document that contains the DOCTYPE. This goes in your dtd-level customization file.

    You should replace “broadbook.dtd” with the filename of your DTD. IIRC, XMetaL returns the filename of the dtd as the system id for sub-documents.

    There's also a function in there to remove cases where you accidentally delete the contents of a PI without deleting the PI, , XMetaL doesn't catch it as a validation error but downstream processors might. Derek Read gave me this one to solve a problem I was having.

    function doCleanBadPis(){
        allPIs = ActiveDocument.getNodesByXPath(“//processing-instruction()”);
        for (i=allPIs.length-1; i>=0; i–) { //we're removing nodes from the tree so it's safer to do this backwards
            node = allPIs.item(i);
            // Application.Alert(i + “: ” + node.nodeName);
            if (node.nodeName == “”) { //check if PITarget is null
                node.parentNode.removeChild(node); //remove the node
            }
        }
    } if(ActiveDocument.ViewType == sqViewNormal || ActiveDocument.ViewType == sqViewTagsOn) {
        doCleanBadPis();
        // I can't remember why I need to do ActiveDocument.doctype.systemId == “broadbook.dtd”,
        // but broadbook.dtd is the name of our dtd, tho the system id is never just the dtd name.
        // I have a vague memory that if you're in a sub-document, XMetaL returns the bare dtd name
        // as the system id. So try replacing broadbook.dtd with your dtd name.
        if(ActiveDocument.doctype.systemId == “broadbook.dtd” || ActiveDocument.doctype.systemId == “”){
            Application.StopValidation();         var parentDoc = ActiveDocument.getNodesByXPath(“//processing-instruction('parent-doc')”);
            // Store the object representing
            // the active document
            var doc = Application.ActiveDocument;
           
            if(parentDoc.length != 0){
                try{
                    var parentDocObj = Documents.item(ActiveDocument.Path + “\” + parentDoc.item(0).nodeValue);
                    parentDocObj.validate();
                    if(parentDocObj.isValid){
                        doc.Activate();
                    }else{
                        parentDocObj.Activate();
                    }
                }catch(e){
                    if(Application.FileExists(ActiveDocument.Path + “\” + parentDoc.item(0).nodeValue)){
                        var parentDocObj2 = Documents.Open(ActiveDocument.Path + “\” + parentDoc.item(0).nodeValue);
                        ActiveDocument.validate();
                        if(ActiveDocument.isValid){
                            doc.Activate();
                        }
                    }
                }
            }else{
                Application.Alert(“Can't validate a document without a DOCTYPE or a “,”Sorry”);
            }
        }
    }else if(ActiveDocument.doctype.systemId == “broadbook.dtd” || ActiveDocument.doctype.systemId == “”){
            Application.StopValidation();
            // Warn them that validating in plain text view is a bad idea if there's no doctype.   
            Application.Alert(“Validating a doc with no doctype in plain text view is pretty useless.n I can't swtich to your parent doc even if you have a n processing instruction  declared.nn Try switching to normal or tags on.”); }                                                            
    ]]>

    Reply

  • You must be logged in to reply to this topic.

Lost Your Password?

Products
Downloads
Support