General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion How Does Selection.CanDelete Work?

  • scotth

    How Does Selection.CanDelete Work?

    Participants 1
    Replies 2
    Last Activity 11 years, 5 months ago

    Using XMetaL 6.0, it seems like Selection.CanDelete is a bit haphazard in determining what can and cannot be deleted. At first, I thought I had it figured out, as it appeared that required elements could not be deleted after they were added to the document. However, I can delete some required elements, while other required elements cannot be deleted. What is the determining factor behind the result of Selection.CanDelete?




    Derek Read

    Reply to: How Does Selection.CanDelete Work?

    The determining factor is basically this: If an author could press the Delete key to remove something then this API should return true.

    Confusion might be due to you wondering “why can the user delete something that is required?”
    Required elements can be deleted in almost all cases and there is good reason for this. I'll try to explain why that is necessary in an XML editor (even one like XMetaL that has a “Rules Checking” feature, which most other XML editors do not have).

    I think you can think of it like this:
    If your document requires an element and that element is not yet present you must be able to insert it. It think this much is obvious. Note that just before insertion of the element the document is invalid (and the Validation Log will indicate this). If the product did not allow you to remove a required element you just inserted then Undo would not function and you might be stuck. In all cases it is possible to delete everything in a document up to an including the root element and then start over with a different root element (that might be the most relevant key point).

    I will try to explain with some examples.

    Example 1:

    In this case there are two possible minimally valid documents:


    If an author starts with DocB (having created it themselves or from a template or opened from elsewhere) and then wishes to create DocA, XMetaL allows them to remove the element, then remove the . This allows them to insert to create DocA. While the document contains without the it is temporarily invalid and the user must rely on the Validation Log, or bolded elements in the Element List to know that a required element is needed.

    Example 2:

    In this case must have one of either or , but not both.

    So, say you start with the following:

    But you actually wanted this:

    You need to be able to remove the required element before you can insert because both are not allowed at the same time (XMetaL will stop you from inserting both). It does let you remove , at which point the document is temporarily invalid (and you need to rely on the Validation Log if you do not know has required elements. Note that in this situation the Element List will not indicate that child1 and child2 are required (it does that by bolding the name) because the Element List only does so when there is only one possible option.

    In other words, your actual interaction with XMetaL might be this:
    1. Start with .
     The document is invalid at this point and pressing F9 (or attempting a save) will indicate that in the Validation Log.
    2. Insert giving you .
    3. Oops, you didn't want to insert so you press Undo or you select and delete it.
     The document is invalid at this point and pressing F9 (or attempting a save) will indicate that in the Validation Log.
    4. Insert giving you .
    The document is valid at this point.

    Example 3:

    In this case these are both valid options:

    In both cases XMetaL allows you to remove all children of (in order to allow you to switch between its two models.
    However, there is also a special case. With DocA XMetaL will not allow you to remove before you remove . If you combine this logic with what XMetaL shows you in the Element List how Rules Checking functions might make a little more sense:

    1. If the doc is empty the element list shows all elements (because any are valid).
    2. If the doc is the element list shows only child1 and child3 because you must make an OR choice. To be valid you MUST insert one of these, however, there is no absolute choice so neither are indicated in bold. XMetaL allows you to delete parent.
    3. If the doc is ^ and your insertion point is at the ^ the Element List shows that no elements can be inserted here. XMetaL allows you to delete child1.
    4. If the doc is ^ and your insertion point is at the ^ element child2 appears bold in the Element List indicating that you MUST insert child2 in order for the document to be valid. XMetaL allows you to delete child1.
    5. If the doc is the Element List shows that no elements can be inserted anywhere. XMetaL allows you to delete child2 but does not allow you to delete child1.
    6. If the doc is the Element List shows that no elements can be inserted anywhere. XMetaL allows you to delete child3.

    More APIs:
    If the standard “Rules Checking” functionality does not guide your authors enough you can help them further by providing document templates (Template subfolder in the XMetaL install path) and templates for individual elements (in the CTM file). For restricting what they can remove you can use additional APIs and your own scripting logic. APIs related to this functionality include (all are on the Selection object):

    CanDelete takes the properties for any elements affected by those APIs into account as well. You do need to be careful if you try to stop people from removing things using these APIs (for all the reasons described above at least), so you will need to be very aware of your document model and possibly your authors' desired workflows as well.



    Reply to: How Does Selection.CanDelete Work?

    Thanks for the detailed explanation. It is much appreciated. The piece that I wasn't getting was that you cannot delete an element when it is required to appear before another existing sibling. Once I read your explanation, I went into XMetaL and did some experiments, finding that it works just as you described.


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

Lost Your Password?