General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion CSS Selector last-child not working

  • ndudek

    CSS Selector last-child not working

    Participants 0
    Replies 1
    Last Activity 5 years, 7 months ago


    I'm trying to customize a certain element through css. I want to use the last-child selector in order to add a line break through css. Unfortunately it seems like XMetaL doesn't understand this selector. Any Idea on how to give css instruction to the last appearance of an element?


    Derek Read

    Reply to: CSS Selector last-child not working

    For any selectors that are unsupported (now or into the future) or for other complex selectors that CSS just does not support in general you can use the Selection.ContainerStyle or the Range.ContainerStyle API. This will work for any element you can programmatically move a Range to in your document (ie: there is some logic you can use to uniquely identify an element). In your case running the following would likely give you what you want:

    [code=Example(JScript): setting CSS through script]
    //XMetaL Script Language JScript:
    /*Change following XPath to suit your needs.
    Hopefully it is as simple as changing the element
    name from Para to yours.
    Setting color:red shows how to set multiple CSS
    properties on the same node. Remove that if you
    don't want them red.
    var xpath = “//Para[last()]”;
    var nodes = ActiveDocument.getNodesByXPath(xpath);
    var rng = ActiveDocument.Range;
    for(var i=0; i rng.SelectNodeContents(nodes.item(i));
    rng.ContainerStyle = “margin-bottom:2em; color:red”;

    If that doesn't do it please provide more information on the XML structure (include a sample).

    Note that in this example I use getNodesByXPath. You can use any logic to move the Range to the element you need to style. Other methods would include walking the document element by element to identify node name, content, attributes, etc. I merely use the getNodesByXPath API here because that was the simplest way to do it given the requirements.


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

Lost Your Password?