ltxmlts
    Preparing search index...

    Class XAttribute

    Represents an XML attribute on an element.

    Attributes are not nodes in the XNode sense -- they do not appear in nodes() or descendants() results -- but they inherit from XObject and carry the same parent reference and annotation API.

    const a = new XAttribute('id', '42');
    a.name.localName; // 'id'
    a.value; // '42'

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    name: XName

    The fully-qualified name of this attribute.

    nodeType: XmlNodeType = null

    The kind of XML node this object represents.

    const el = new XElement('root');
    console.log(el.nodeType); // 'Element'
    parent: XObject | null = 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
    value: string

    The string value of this attribute.

    Accessors

    • 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
    • get isNamespaceDeclaration(): boolean

      Indicates whether this attribute is a namespace declaration (xmlns or xmlns:prefix).

      Returns boolean

      new XAttribute(XNamespace.xmlns + 'w', 'http://...').isNamespaceDeclaration; // true
      
    • get nextAttribute(): XAttribute | null

      Returns the next sibling attribute on the parent element, or null.

      Returns XAttribute | null

      const el = new XElement('a', new XAttribute('x','1'), new XAttribute('y','2'));
      el.firstAttribute?.nextAttribute?.name.localName; // 'y'

    Methods

    • 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).

    • Compares this attribute to another by name and value.

      Parameters

      • other: XAttribute

        The attribute to compare against.

      Returns boolean

      true if both name and value are equal.

    • Removes this attribute from its parent element.

      Returns void

      Error if the attribute has no parent.

    • 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
    • Sets the value of this attribute.

      Parameters

      • value: string

        The new string value. Must not be null or undefined.

      Returns void

      Error if value is null or undefined.

    • Returns the XML serialization of this attribute, e.g. name='value'.

      Returns string

      The serialized attribute string with XML-escaped value.