OpenXmlSdkTs - v1.0.7
    Preparing search index...

    Class XProcessingInstruction

    Represents an XML processing instruction (<?target data?>).

    Throws if the target contains whitespace or if the target or data contains "?>".

    const pi = new XProcessingInstruction('xml-stylesheet', 'type="text/css" href="style.css"');
    pi.toString(); // '<?xml-stylesheet type="text/css" href="style.css"?>'

    Hierarchy

    • XNode
      • XProcessingInstruction
    Index

    Constructors

    Properties

    nodeType: XmlNodeType

    The kind of XML node this object represents.

    const el = new XElement('root');
    console.log(el.nodeType); // 'Element'
    parent: XObject | null

    The parent of this object in the XML tree, or null if it is a root.

    const child = new XElement('child');
    const parent = new XElement('parent', child);
    console.log(child.parent === parent); // true
    target: string

    The target name of the processing instruction.

    data: string

    The data (body) of the processing instruction.

    Accessors

    • get previousNode(): XNode | null

      Gets the previous sibling node, or null if this is the first node.

      Returns XNode | null

    • get nextNode(): XNode | null

      Gets the next sibling node, or null if this is the last node.

      Returns XNode | null

    • get document(): XDocument | null

      Gets the XDocument that contains this object, or null if the object is not part of a document tree.

      Returns XDocument | null

      const doc = new XDocument(new XElement('root', new XElement('child')));
      const child = doc.root!.elements()[0];
      console.log(child.document === doc); // true

    Methods

    • Inserts the specified content immediately after this node in its parent.

      Parameters

      • ...content: unknown[]

        One or more nodes or strings to insert.

      Returns void

      Error if this node has no parent.

      const parent = new XElement('p', new XElement('a'), new XElement('c'));
      const a = parent.elements()[0];
      a.addAfterSelf(new XElement('b'));
      // parent now contains <a/>, <b/>, <c/>
    • Inserts the specified content immediately before this node in its parent.

      Parameters

      • ...content: unknown[]

        One or more nodes or strings to insert.

      Returns void

      Error if this node has no parent.

    • Replaces this node with the specified content.

      Parameters

      • ...content: unknown[]

        One or more nodes or strings that replace this node.

      Returns void

      Error if this node has no parent.

    • Returns a collection of the ancestor elements of this node.

      Returns XElement[]

      An array of ancestor XElement instances.

      When called with no arguments, returns all ancestor elements from the immediate parent up to the root. When a name is supplied, only ancestors with that name are returned. The collection is ordered from nearest ancestor to farthest.

      const xml = XElement.parse('<root><a><b/></a></root>');
      const b = xml.descendants('b')[0];
      b.ancestors(); // [<a>, <root>]
      b.ancestors('root'); // [<root>]
    • Returns a collection of the ancestor elements of this node filtered by name.

      Parameters

      • name: string | XName

        Name to filter ancestors by.

      Returns XElement[]

    • Returns the sibling elements that follow this node.

      Returns XElement[]

      An array of sibling XElement instances after this node.

      const parent = new XElement('p', new XElement('a'), new XElement('b'), new XElement('c'));
      const a = parent.elements()[0];
      a.elementsAfterSelf(); // [<b>, <c>]
    • Returns the sibling elements that follow this node, filtered by name.

      Parameters

      • name: string | XName

        Name to filter by.

      Returns XElement[]

    • Returns the sibling elements that precede this node.

      Returns XElement[]

      An array of sibling XElement instances before this node.

    • Returns the sibling elements that precede this node, filtered by name.

      Parameters

      • name: string | XName

        Name to filter by.

      Returns XElement[]

    • Returns all sibling nodes that precede this node.

      Returns XNode[]

      An array of XNode instances before this node.

    • Returns all sibling nodes that follow this node.

      Returns XNode[]

      An array of XNode instances after this node.

    • Removes this node from its parent.

      Returns void

      Error if this node has no parent.

    • Performs a deep structural comparison of this node with another.

      Parameters

      • other: XNode

        The node to compare against.

      Returns boolean

      true if the two nodes are structurally identical.

      Two nodes are deeply equal when they have the same node type and their content (including all descendants and attributes for elements) is equal.

      const e1 = XElement.parse('<a x="1"><b/></a>');
      const e2 = XElement.parse('<a x="1"><b/></a>');
      e1.deepEquals(e2); // true
    • Static convenience method for deep structural comparison of two nodes.

      Parameters

      • a: XNode

        First node.

      • b: XNode

        Second node.

      Returns boolean

      true if the two nodes are structurally identical.

      const e1 = XElement.parse('<a x="1"><b/></a>');
      const e2 = XElement.parse('<a x="1"><b/></a>');
      XNode.deepEquals(e1, e2); // true
    • Attaches an arbitrary annotation object to this XML object.

      Parameters

      • obj: unknown

        The annotation object to attach.

      Returns void

      class MyMeta { constructor(public tag: string) {} }
      const el = new XElement('item');
      el.addAnnotation(new MyMeta('important'));
    • Returns the first annotation of the specified type, or null if none exists.

      Type Parameters

      • T

        The annotation class to look up.

      Parameters

      • ctor: new (...args: any[]) => T

        Constructor of the annotation type to retrieve.

      Returns T | null

      The first matching annotation, or null.

      class MyMeta { constructor(public tag: string) {} }
      const el = new XElement('item');
      el.addAnnotation(new MyMeta('important'));
      const m = el.annotation(MyMeta);
      console.log(m?.tag); // 'important'
    • Returns all annotations of the specified type.

      Type Parameters

      • T

        The annotation class to look up.

      Parameters

      • ctor: new (...args: any[]) => T

        Constructor of the annotation type to retrieve.

      Returns T[]

      An array of matching annotations (may be empty).

    • Removes annotations from this object.

      Returns void

      When called with no arguments, all annotations are removed. When called with a constructor, only annotations of that type are removed.

      class MyMeta { constructor(public tag: string) {} }
      const el = new XElement('item');
      el.addAnnotation(new MyMeta('important'));
      el.removeAnnotations(MyMeta); // removes only MyMeta annotations
      el.removeAnnotations(); // removes everything
    • Removes annotations from this object.

      Type Parameters

      • T

      Parameters

      • ctor: new (...args: any[]) => T

      Returns void

      When called with no arguments, all annotations are removed. When called with a constructor, only annotations of that type are removed.

      class MyMeta { constructor(public tag: string) {} }
      const el = new XElement('item');
      el.addAnnotation(new MyMeta('important'));
      el.removeAnnotations(MyMeta); // removes only MyMeta annotations
      el.removeAnnotations(); // removes everything
    • Compares this processing instruction to another by target and data.

      Parameters

      Returns boolean

      true if both target and data are equal.

    • Returns the XML serialization of this processing instruction.

      Returns string

      The serialized PI string, e.g. <?target data?>.