XMetaL Tips and Tricks

XMetaL Community Forum XMetaL Tips and Tricks Script Example: Removing Empty Processing Instructions (PI)

  • Derek Read

    Script Example: Removing Empty Processing Instructions (PI)

    Participants 1
    Replies 0
    Last Activity 11 years, 8 months ago

    The XML Recommendation says that at PI must have a target. However, unless you provide an additional UI for your authors by customizing XMetaL Author, when they insert a PI into a document using the built-in “Insert Processing Instruction” functionality (usually on the Insert menu) a PI is initially inserted without a target and without data and simply looks like this:

    The author is expected to enter the target (and optional data) by typing them in. If the author does not enter a target (at minimum) the XML document is invalid.

    Versions of XMetaL Author (Enterprise and Essential) prior to version 6.0 do not complain about an empty PI during validation (a bug). This issue has been corrected with the 6.0 release. If you upgrade to the 6.0 release a document containing empty PIs is considered invalid and the following will appear in the Validation Log: “Processing instruction does not have a target name”. As with other validation errors clicking on the error will take you to the node it corresponds to in the document.

    Such PIs are generally unwanted so you may wish to automate their removal. The product does not automatically strip these PIs from the document because it does not know the author's real intension.

    If you are happy to just remove them all you might implement a script similar to the following:

    //XMetaL Script Language JScript:
    var rng = ActiveDocument.Range;
    var piList = ActiveDocument.getNodesByXPath("//processing-instruction()");
    for(i=piList.length-1; i>=0; i--) {
    if(rng.ContainerNode.nodeType == 7 && rng.ContainerNode.target == "") {
    Application.Alert("found empty PI, removing...");

    You would of course remove the Alert calls so that this runs without annoying the author.

    An appropriate time for this to run (in my opinion) would be whenever the author triggers a full document validation (usually F9 or Save) so that they never need to worry about removing empty PIs. If so, you can place this script into the event named On_Before_Document_Validate. However, you might also allow the authors to manually run your macro (in which case the name and whether you provide a toolbar button, menu item or shortcut is up to you).

    What if the author really needs a PI to be at a given position but perhaps they often just forget to insert a target (and possibly data)?
    In this case automating the removal would cause them to lose the position of these PIs. You may wish to alter this script to locate all the PIs and then perhaps provide some form of wizard (with a dialog) specific to their needs. The dialog might offer various options, perhaps including a “Delete” button, “Delete All” button and maybe an “Enter PI Target” option that lets them select from an approved list of PI “types”, or functionality such as “Goto Next Empty PI”.

    In this case you might wish to also implement a similar wizard (form) that authors would use to insert PIs in the first place to reduce the chances that empty PIs are ever created. Such a feature could also check the PI target and data according to your own set of criteria (aka “business rules”). Of course, because any system will define its own PIs, how this would look and function might vary as widely as the types of PIs you need to deal with, their relationship to the document and to other software. See the Programmers Guide for APIs related to creating and inserting processing instructions.

    Licensed Materials – Property of JustSystems, Canada, Inc.

    (c) Copyright JustSystems Canada, Inc. 2011
    All rights reserved.

    The sample contained herein is provided to you “AS IS”.

    It is furnished by JustSystems Corporation as a simple example and
    has not been thoroughly tested under all conditions. JustSystems
    Canada, Inc., therefore, cannot guarantee its reliability,
    serviceability or functionality.

    This sample may include the names of individuals, companies, brands
    and products in order to illustrate concepts as completely as
    possible. All of these names are fictitious and any similarity to
    the names and addresses used by actual persons or business
    enterprises is entirely coincidental.


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

Lost Your Password?