gofastxml

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 23 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-fast-xml

Extracts XML documents of any schema into the shared go-fast-core document model. Standard library only.

import "github.com/AOShei/go-fast-xml" // package gofastxml

doc, err := gofastxml.ExtractFile(ctx, "fiche.xml", nil)
if err != nil { ... }
fmt.Println(doc.Text())

A document whose schema has a profile is read with it: the profile maps the schema's elements onto headings, paragraphs, lists, tables, links, notes and metadata (see Profiles). Twelve profiles are built in: Akoma Ntoso, HL7 CDA, CLML (legislation.gov.uk), DITA, DocBook, Formex (EUR-Lex), JATS, Légifrance, patents (USPTO and EPO), S1000D, Service-Public.fr and TEI. A profile is a JSON file, so other schemas can be given one without code, and -draft-profile drafts one from an XSD. Any other XML document is read generically: its text in document order, as inferred paragraphs, with its attributes and comments as notes.

The document is read as a stream, never as a tree: besides the document it returns, memory is bounded by the fixed bounds below (one tag, one block, the blocks waiting for an undecided element, the namespace declarations in scope), not by the file. For input not read as UTF-8, the map back to the file's bytes adds 16 bytes per 32 KiB of input. A generated 200 MiB document of small elements is read with a peak heap of about 1 MiB (TestLargeStream).

API

  • Extract(ctx, r io.ReaderAt, size, opts), ExtractFile(ctx, path, opts), ExtractBytes(ctx, data, opts).
  • Extractor{Attributes, Profiles, Profile, Charset} implements gofastcore.Extractor; its zero value is ready and safe for concurrent use. Formats() is xml, and Source.Format is xml, Source.MIME application/xml.
  • Attributes: AttributesNotes (the default) makes each attribute a note (below); AttributesNone leaves attribute notes out, for data-style XML where attributes are keys and codes. Leaving them out raises no warning. It applies to generic reading.
  • Extractor.Profiles []*Profile: profiles tried, in order, before the built-in ones. Extractor.Profile string: force the profile of that name (from Profiles or built in) whatever the root element; ProfileNone ("none") forces generic reading; an unknown name is an error.
  • LoadProfile(io.Reader) (*Profile, error) reads and validates a profile; BuiltinProfiles() lists the built-in ones in the order they are tried, BuiltinProfile(name) returns one. A Profile (Name(), Title()) is immutable and safe for concurrent Extract calls.
  • Extractor.Charset: the encoding of the input given from outside the file (see Encodings).
  • DraftProfile(ctx, path) (*Draft, error) drafts a profile from an XSD (see Drafting a profile).

Command

go install github.com/AOShei/go-fast-xml/cmd/gofastxml@latest

gofastxml [flags] file-or-directory...
  -format json|markdown|text  -attributes=false  -o file  -quiet  -version
  -profile NAME|FILE|none  -profiles  -spans  -charset NAME
  -draft-profile schema.xsd
  -furniture  -stored[=kinds]  -part-markers  -children  -exclude-display[=reasons]
  -max-input-bytes N  -max-decoded-bytes N  -max-text-bytes N

A directory stands for the files in it and below it whose names end in .xml (in any case). A file named on the command line is read whatever its name. With one input the JSON is indented; with several, each document is one line. Documents go to standard output; warnings of severity error go to standard error; the exit status is 1 if any input could not be read, 2 for a usage error.

The render flags are the series' shared ones (COMMAND_FLAGS.md), defined by core's AddRenderFlags, and affect Markdown and text only; JSON always has everything. This library writes one stored kind, markup_comment (the file's comments), so -stored=markup_comment puts comments in the text.

-profile takes the name of a built-in profile, a profile file, or none; -profiles lists the built-in profiles (name and title); -spans sets Options.Spans (inline styles and links in the JSON); -charset sets Extractor.Charset for every input; -attributes=false sets AttributesNone; -max-input-bytes, -max-decoded-bytes and -max-text-bytes set core's limits (0 the default, -1 no limit); -quiet says nothing about files that were read; -version prints the version. -draft-profile schema.xsd writes a draft profile (to -o or standard output) and reads no document.

Profiles

A profile is a JSON file that says what a schema's elements mean. How to write one: PROFILE_FORMAT.md.

How one is chosen. When the root element starts, the profiles of Extractor.Profiles are tried in order, then the built-in ones in the order BuiltinProfiles() and gofastxml -profiles list them (by name: akn, cda, clml, dita, docbook, formex, jats, legifrance, patent, s1000d, service-public, tei); the first whose match the root meets (local name, namespace, optionally a DOCTYPE public identifier prefix and attribute tests) is used, else the document is read generically. Extractor.Profile / -profile forces one, or none. Metadata.Extra["xml.profile"] names the profile used; it is absent for generic reading.

What a profile gives. Blocks of every kind core has for text (headings with levels from nesting, paragraphs, quotes, code, captions, footnotes, formulas, notes, list items with depth and Marker, tables in the rows or CALS model with header rows and merges, images), inline styles and links as Spans with Options.Spans, metadata, parts, and the stored part. Blocks a rule makes are native; what the profile does not map is read generically (inferred) and counted. A profile only chooses how the file's text is cut and labelled: it cannot add words, drop text or evaluate a condition.

How to cite a block. Every block has Loc.Offset and Loc.Length, the bytes of the element it was made from in the file, from its start tag to its end tag (a heading: its title element; a table: the table element; an item: the item element; stray text: the text). A table also says where each row came from: Table.RowLocs has one location per row, the row element's bytes and its anchor if the profile gives rows one (empty for a header row the profile supplies), so a single row can be cited. Loc.Anchor is the identifier the source gives the block (the rule's anchor, else xml:id); a block without one carries the nearest enclosing identified element's in Attrs["anchor_parent"] (AttrAnchorParent), so a citation can say "fiche F1199, chapter X" without offsets. The document itself is cited by Source.Name and the identifiers each profile copies to Metadata.Extra (below). Text moved out of the body (metadata, Attrs values, the stored part) has no offsets of its own; a block whose text is an attribute's value says so in Attrs["attribute"]. An inline rule's value (TEI pb@n, Formex QUOT.START@CODE) is written in the block's text where its element stands: that text is in the block's span as the attribute, not as character data. Tests check, for every block of every dev file of every profile's corpus, that its text is in the source at its offsets, and that every row location lies inside its table and starts at a tag.

Warnings of profile reading (all info, on the last part, names with counts, at most 100 names listed):

Code What happened
xml.unmapped_elements Elements the profile has no rule for were read generically
xml.unmapped_attributes Attributes of mapped elements that no rule reads (element@attribute); their values are not in the output
xml.skipped_elements Elements a user profile's skip rules left out

Core's structure.heading_level_clamped (headings nested deeper than 6), input.unread_content (MathML in a formula rule, kept as plain text) and limit.exceeded (below) are raised as core defines them.

The built-in profiles

Each built-in profile has a full note in profiles/<name>.md: the mapping table, the Extra and Attrs keys, how to cite, what is left to generic reading and the versions tested. The sections below summarise them; the note is where the mapping is written down. Every profile's own keys carry its prefix, so they cannot collide with core's keys or another profile's. Every built-in profile reads all the text of its documents; what it has no rule for is read generically and counted in xml.unmapped_elements. Notes on each profile's dev set (unmapped elements and attributes) are in its note.

Akoma Ntoso (akn)

Selected by the root akomaNtoso in the namespace of Akoma Ntoso 3.0, its draft 3.0/CSD03, or akomantoso.org/2.0 (read with the 3.0 rules). Maps the document types, the hierarchy of legislation (parts, chapters, articles, paragraphs, points) with num as Marker and heading as heading, authorialNote as out-of-line footnotes, tables, quoted structures, the FRBR metadata of meta (Extra akn.*) and the lifecycle, workflow and references tables. Keys: akn.*. Cite: Loc.Anchor is @eId (or @id in 2.0), anchor_parent the enclosing provision's. Tested: 3.0 (legislation.gov.uk, Fedlex, OASIS examples), 3.0/CSD03 (Italian Senate), 2.0 (Bungeni); not tested: the 1.0 namespace and the amendmentList, officialGazette, portion and statement documents. profiles/akn.md

HL7 CDA (cda)

Selected by the root ClinicalDocument in urn:hl7-org:v3 (a ClinicalDocument with no namespace is read generically). Maps the header (patient, author, custodian, participants, dates, codes) to Metadata and Extra cda.*, sections with their titles, their code and templateId as attribute notes, narrative blocks (paragraphs, lists, tables, content styles, linkHtml), and nonXMLBody. Structured entries are read generically, every value an attribute note, recorded as written, never interpreted. Keys: cda.*. Cite: Loc.Anchor from @ID. Tested: CDA R2 with C-CDA R1.1 and R2.1 templates, structured and unstructured; not tested: CDA R2.1 or R3, signature data. profiles/cda.md

CLML (clml)

Selected by the root Legislation in the namespace http://www.legislation.gov.uk/namespaces/legislation. Maps primary and secondary prelims, the body's parts, chapters, cross-headings and provisions with Pnumber as Marker, footnotes out of line, tables (XHTML), schedules, explanatory notes and commentaries as parts (each Commentary a footnote), and the ukm: metadata. Keys: clml.*. Cite: Loc.Anchor from @id (section-1), anchor_parent the enclosing provision's. Tested: CLML schema 2.0, primary, secondary and EU retained legislation; not tested: Image/Figure, Formula, Form. profiles/clml.md

DITA (dita)

Selected by the DITA topic and map roots with a DITA DOCTYPE public identifier or ditaarch:DITAArchVersion, and, with neither, by the root and the attribute DITA requires (rules go by element name: @class usually comes from DTD defaults, which are not read). Maps topics and their bodies, sections, steps, tables (CALS and simpletable), notes and maps (topicref items, their href as text when they have none). The filtering attributes on content (platform, product, audience, otherprops) are not read: they are counted in xml.unmapped_attributes, and nothing is filtered. Keys: dita.*. Cite: Loc.Anchor from @id. Tested: DITA 1.3 and DITA-OT documentation and tests; not tested: DITA 2.0, Lightweight DITA. profiles/dita.md

DocBook (docbook)

Selected by DocBook 5 roots in http://docbook.org/ns/docbook, and DocBook 4 roots with no namespace and a DocBook DOCTYPE public identifier (or, with none, a few roots no other schema uses). Maps books, chapters and sections, para, lists, CALS and HTML tables, admonitions, figures and examples, programlisting as code, footnotes (split from their paragraph), glossaries, refentry, and info metadata. An imageobject's condition is recorded in Attrs["db.condition"], never evaluated. Keys: db.*. Cite: Loc.Anchor from xml:id (or id in DocBook 4). Tested: DocBook 5.0 to 5.2 and 4.1.2 to 4.5; not tested: SGML DocBook, assembly. profiles/docbook.json is generated by a test (go test -run TestDocbookGenerated -docbook.update). profiles/docbook.md

Formex (formex)

Selected by the Formex 4 roots in no namespace (ACT, ANNEX, DOC, PUBLICATION, CONS.ACT, CONS.ANNEX, CONS.OTH, GENERAL). Maps the bibliographic record (BIB.INSTANCE) to Metadata and Extra fx.*, the preamble, enacting terms and final part as parts, recitals, articles and paragraphs with their numbers as Marker, lists, tables, notes out of line, and the quotation marks QUOT.START/QUOT.END written from their CODE. Keys: fx.*. Cite: Loc.Anchor from IDENTIFIER (001.002), notes from NOTE.ID; the Official Journal reference in Extra. Tested: Formex 05.20 to 05.59; not tested: consolidated texts, GENERAL, Formex 6. profiles/formex.md

JATS (jats)

Selected by the root article with a NLM or NISO DOCTYPE public identifier or an article-type or dtd-version attribute, and by pmc-articleset. Maps front (title, authors, affiliations, dates, identifiers, abstract) to Metadata and Extra jats.*, body sections, paragraphs, lists, figures and tables (table-wrap, split from their paragraph), formulas, back (references one item each, footnotes, acknowledgements). Keys: jats.*. Cite: Loc.Anchor from @id; the DOI and PMID in Extra. Tested: JATS 1.1 Archiving, NLM 3.0, PMC article sets; not tested: JATS 1.2 to 1.4 Authoring, BITS. profiles/jats.md

Légifrance (legifrance)

Selected by the LEGI and JORF roots in no namespace (ARTICLE, TEXTE_VERSION, SECTION_TA, TEXTELR, JO, CONTENEUR). Maps the metadata blocks (identifiers, dates, status, NOR) to Metadata and Extra lf.*, the title as heading 1, the article text (XHTML-like content with br as line breaks, and tables), and versions, links and structures as one paragraph each with their ids in Attrs. Keys: lf.*. Cite: the ID (LEGIARTI...) in Extra; sections of a JO summary by Loc.Anchor from @id. Tested: LEGI and JORF exports; not tested: CONTENEUR, TEXTEKALI, CNIL, JADE. profiles/legifrance.md

Patents (patent)

Selected by the roots us-patent-grant, us-patent-application, patent-application-publication, ep-patent-document, wo-patent-document, patent-document. Maps the bibliographic data (numbers, dates, classifications, parties, citations) to Metadata and Extra pat.*, the abstract, description (headings, numbered paragraphs with @num), claims (one list item per claim, claim-ref as links), tables, chemistry, formulas and drawings (images; no file is loaded). Keys: pat.*. Cite: Loc.Anchor from @id, claims from @num. Tested: USPTO grant v4.0 to v4.3, application v4.3, pap-v16, EPO v1.1 to v1.5.1; not tested: USPTO v4.4 and later, WIPO ST.36 documents other than the EPO's, ST.96. profiles/patent.md

S1000D (s1000d)

Selected by the roots dmodule, pm and dml in no namespace. Maps the identification and status section to Metadata and Extra s1000d.*, descriptive content (levelled paragraphs, lists, tables, figures, warnings and cautions), procedures (steps), publication module entries and data management list entries. Applicability references (applicRefId) are recorded in Attrs["s1000d.applic_ref"], never evaluated. Keys: s1000d.*. Cite: Loc.Anchor from @id; the data module code in Extra. Tested: issues 4.1 and 4.2; not tested: issues 4.0, 5.0 and 6, fault isolation, IPD, process, wiring. profiles/s1000d.md

TEI (tei)

Selected by the root TEI or teiCorpus in the TEI P5 namespace (P4 and older are read generically). Maps the teiHeader to Metadata and Extra tei.*, divisions with their heads, paragraphs, verse lines (one block each), speeches with their speakers as Marker, stage directions, notes, lists, tables, figures, dictionary entries, and editorial markup (choice, app, add, del) with every alternative in the text in file order and a stored record saying which it is. Page and line numbers inside a block (pb@n, milestone@n) are written in place. Keys: tei.*. Cite: Loc.Anchor from xml:id, and tei.divN_n and tei.n for the canonical reference. Tested: TEI P5 samples (ELTeC, Folger, DraCor, Perseus, papyri.info, Bodleian, FreeDict); not tested: facsimile, P4. profiles/tei.md

Service-Public (service-public)

For the XML of Service-Public.fr published by DILA (schema Publication 3.5): the roots Publication (fiches F, dossiers and themes N, guided searches) and ServiceComplementaire (R: forms, online services, model letters, simulators, definitions), and the index files Arborescence, Menu, Redirections, DatesModImp, CentresDeContact. Conditions are recorded, never evaluated: every case of every condition is extracted, in file order.

Source Output
dc:title Metadata.Title, and a level 1 heading at the top of the body
dc:creator; dc:subject; dc:language; dc:description Metadata.Authors; Keywords; Language as written (Fr); Extra["description"] (no block)
dc:date and the other dc:* Extra["dc:date"] etc. as written (modified 2026-11-03); Metadata.Modified is not set
root @ID, @type, @spUrl, @dateDerniereModificationImportante, @profilsPro, @statut, @pictogramme Extra["sp.id"], sp.type, sp.url, sp.date_important, sp.profils_pro, sp.status, sp.pictogramme
SurTitre, Audience, Canal, NumeroCerfa, AutreNumero (of the root) Extra["sp.surtitre"], sp.audience, sp.canal, sp.numero_cerfa, sp.autre_numero
FilDAriane/Niveau Extra["sp.breadcrumb"] titles joined with >; sp.breadcrumb_ids, sp.breadcrumb_types
Theme, SousThemePere, DossierPere/Titre Extra["sp.theme"], sp.theme_id, sp.theme_audience, sp.sous_theme, sp.sous_theme_id, sp.dossier, sp.dossier_id
Chapitre, SousChapitre, Situation, Cas, OuSAdresser, Definition, Abreviation, Avertissement, Conclusion, Exemple, PartieLibre, CaracteristiqueLibre, Question, Problematique, Branche, Groupe, QuiPeutMAider, SousDossier, SousTheme, IntroductionSimple sections; their Titre a heading by nesting from level 2
Cas/Situation Titre and TitreRiche heading from TitreRiche; the plain Titre in Attrs["sp.title_plain"] only when its text differs
TitreFlottant a heading one level below its section's, opening no section
Chapitre@numerote, @type, TitreAlternatif Attrs["sp.numbered"], sp.type, sp.title_alt on the chapter's heading; no number is invented
Paragraphe; Liste@type/Item paragraph; list_item, ordered (and numbered) for numero, every item with Attrs["sp.list"] = the type (puce, numero, caseACocher)
Tableau/Rangée@type/Cellule@fusionHorizontale|fusionVerticale; TitreRiche, Resume table (rows model) with header rows and merges; caption blocks before it. Colonne is layout: its attributes are counted in xml.unmapped_attributes
ASavoir, ANoter, Attention, Rappel their blocks carry Attrs["sp.box"] = the element name; the box's Titre is the Marker of its first block
MiseEnEvidence, Exposant, Indice; Expression, TermeEtranger, Valeur, Variable, Date bold, superscript, subscript spans; plain text
LienExterne@URL, LienInterne@LienPublication, LienIntra@LienID link spans whose address is the URL or the id as written (F2384); each link's other attributes (commentaireLien, the target's title, as sp.comment; type; audience; ...) are a record in the stored part (note, stored=custom_property, href) whose text is the link's own text. TermeEtranger@langue gives such a record too (sp.langue): core's Span has no language field
BlocCas@affichage, ListeSituations@affichage Attrs["sp.case_group"] on every block inside
Condition (in Cas, Situation, Chapitre, FragmentConditionne, Item, Reference, ...) Attrs["sp.condition"] on every block inside, as an outline: ou(estVrai(var=T10266), estIndefini(var=T10267))
Question/Choix with SiSelectionne; Branche/Problematique with SiSelectionne a list_item with Attrs["sp.sets"] (affecteVrai(var=T12852), affecteFaux(var=T12853)); on the branch's heading; IntituleSiSelectionne, TitreChoix paragraphs
Reference, PourEnSavoirPlus, ServiceEnLigne, NoticeLiee, ServiceEnLigneAnnexe, LienExterneCommente, Video, QuestionReponse, FicheConnexe, CommentFaireSi, RechercheGuidee(Pere), FicheLiee, FichePrecedente, FicheSuivante, ServiceEnLigneAlternatif, Fiche, Dossier, Contact a list_item whose text is the Titre (linked to @URL or the id), Loc.Anchor the @ID, Attrs["sp.group"] = the element name, the other attributes under sp.* (sp.url, sp.type, sp.audience, sp.format, sp.poids, sp.numerocerfa, ...). No heading is invented for a group. Complement, Connexion, Introduction, Source follow as paragraphs
Source a paragraph, Loc.Anchor its @ID, Attrs["sp.field"]="Source"
OuSAdresser, QuiPeutMAider with PivotLocal, RessourceWeb@URL section; Attrs["sp.pivot"] on its heading; a paragraph whose text is the URL (Attrs["attribute"]="URL")
Image, Infographie, ImageIllustration image block: id from @LienPublication, alt from TexteDeRemplacement; Legende and Infographie/Titre captions; Credits, Description paragraphs
Public, Langue, Cout, Calendrier, Etapes, Documents, ... (R files) their blocks carry Attrs["sp.field"] = the element name
ItemArbo, ItemMenu, DatesModImp/Item (index files) a list_item at its depth with its Titre, anchor @ID, sp.type, sp.date_modified, sp.file, sp.date_important
Redirections with Redirection, RedirectionExterne one table in file order: a header row from, to, toUrl, date, audience, type, corpus (the profile's text, marked Attrs["header_source"]="profile"; the table is Origin: native, as every row and cell is the file's), then a row per element from its attributes (cells), an absent attribute an empty cell; Attrs["attribute"] lists the attributes. Each row has its element's place in the file in Table.RowLocs; the table's Loc covers the root element

On the public corpus (Service-Public, 4,194 dev and 1,362 held-out files), every element has a rule: xml.unmapped_elements is empty on both sets. The only unmapped attributes on dev are Colonne@largeur and Colonne@type (3,558 each, column layout); held-out has 2,728 unmapped attributes in all.

Keys: sp.*. Tested: Publication schema 3.5 (the dev and held-out sets above); not tested: earlier schema versions.

Drafting a profile from an XSD

gofastxml -draft-profile schema.xsd [-o draft.json]

or gofastxml.DraftProfile(ctx, path), which returns a *Draft (the JSON, the schema files read, and warnings), reads an XML Schema (XSD 1.0) and writes a draft profile for a person to finish. It is not a profile: every rule carries "review": true, and LoadProfile refuses a file that still has one (a draft from -draft-profile, not a profile). Check each rule, change or delete it, and remove the mark; then the file loads.

What it reads, with this module's own XML reader: global and local elements, named and anonymous complex types, simple types, complexContent and simpleContent extension (the base's content and attributes are added) and restriction, group and attributeGroup references, sequence, choice, all, mixed, ref, substitution groups (the members stand wherever the head may; an abstract head does not), any, attributes with their types (xs:ID, xs:IDREF, xs:anyURI and types derived from them, xml:lang, xml:id), the target namespace, elementFormDefault and form, chameleon includes. xs:include, xs:import and xs:redefine are followed on disk only, at locations relative to the file that names them. A location that is a URL, an absolute path, missing, or naming no file is not opened: nothing is fetched. A file is opened only after os.Stat finds it a regular file (a FIFO, device or directory is never opened); symbolic links are followed, and must lead to a regular file. The context is consulted before each file. Such parts are listed in the draft's notes.not_read, references to components they would have held in notes.missing_components, and the call returns an input.external_reference warning. Reading is bounded (512 files, includes 32 deep, 32 MiB in all, 400,000 schema elements, 100,000 element declarations); what a bound leaves out is a limit.exceeded warning and a note. Type cycles (section in section) are normal and followed once.

Not read: DTDs and RELAX NG schemas (use the XSD edition of a schema where there is one); identity constraints, facets and default values; XSD 1.1 (assertions, open content, override); xs:notation; xsi:type in documents.

What it writes, for each element the schema declares, from its content model first and its name only to break a tie:

Shape Suggestion
Elements only, a text first child (or a title-named one), and it nests or repeats section, the child as heading with "level": "nesting"
Mixed content among elements-only content, or text content beside such siblings block, paragraph
Mixed or text content inside mixed content inline; with an xs:anyURI attribute, link
One child, repeated list, the child as item
A repeated child whose children repeat (three levels), or CALS/XHTML names table, row, cell
Empty with an xs:anyURI attribute image with that attribute as id
An xs:ID attribute anchor on the rule
Elements only, nothing more said transparent

Where one local element name has declarations that would get different suggestions, the draft writes one rule per parent (Parent/name). Elements with no suggestion are listed under notes.undecided with their content, parents, children and attributes; they are read generically. Each rule's "note" says why it was suggested; rests on the name marks a suggestion the name decided (title, titre, para, list, item, table, row, cell, fn, img and the like, in English and French). match is drafted from the global elements no other element contains and the target namespace. Metadata, labels, notes, parts and stored content are not drafted. Name guessing happens only here, never during extraction. The output is the same for the same schema.

On the public schemas (go test -run TestDraft -v, with ../.testdata/schemas), the draft's rule kinds against the hand-written profiles, element by local name: Service-Public Publication.xsd same kind 55, different 60, no rule 2 of 117; Akoma Ntoso 3.0 179, 56, 14 of 249; JATS Publishing 1.3 150, 117, 13 of 280. Each draft is made in under 20 ms.

How XML maps without a profile

One part of kind body, number 1.

  • Text elements. An element that directly contains character data other than white space is a text element. It gives one paragraph block, Origin: inferred, whose text is its own character data with that of all its descendants, in document order. CDATA sections and entity replacements are character data.
  • Containers. An element that holds only elements (and white space) is a container and gives no block of its own.
  • Inline elements. A text element inside another text element is part of the outer block: <p>Hello <b>world</b></p> is one paragraph, Hello world. This holds also when the outer element's own text comes after its children (<p><b>Bold</b> tail</p>): the children's blocks are taken into it. To keep memory bounded, blocks wait for such a decision only up to 4 MiB or 8,192 blocks; past that, the open elements still undecided are taken for containers, and one that has text after that point gives a block from there on, with limit.exceeded. Nothing is lost either way; only where block boundaries fall differs.
  • White space. Runs of XML white space (space, tab, line feed, carriage return) collapse to one space, and the text is trimmed. Under xml:space="preserve" the white space is kept as written, line breaks as \n and tabs included, and only the ends are trimmed; xml:space="default" turns it off again for a subtree. Other space characters (a no-break space) are content and kept, though not at the ends of a block.
  • Attrs["element"] on each block made from an element is the path of element names from the root as written in the file, prefixes included, joined with / (Publication/Texte/Paragraphe), so a consumer can tell <name> from <address>.
  • Nothing is guessed from names. No headings, lists, tables or titles are inferred from element names; a profile says what names mean.
  • Text outside the root element (which a well-formed file does not have) is a paragraph of its own with no element path.
  • Locations. Every block has Loc.Offset and Loc.Length: for a paragraph the element's span from its start tag to its end tag, for an attribute note its element's start tag, for a comment the comment. Offsets are bytes of the file, in every encoding (a byte order mark included): for a file not in UTF-8 the decoder keeps a checkpoint per 32 KiB and the offsets are mapped back after reading, re-reading only the stretches they fall in. A block read from an entity's replacement has the location of the reference. A paragraph or comment split by the block bound has the span of each piece's text; the pieces are in order and do not overlap. Loc.Anchor is the block's element's xml:id, else its id, else that of an element inside it.
  • Ligatures U+FB00 to U+FB06 are written as their letters; other characters are kept as the file has them, invisible ones included.
Attributes

Each attribute with a value that is not empty (after white space is collapsed) is a note block of type attribute (core's NoteAttribute), Origin: inferred, with the attribute's name as written under Attrs["attribute"] and its element's path under Attrs["element"]. It stands where the element starts: an element's attribute notes come before the element's own paragraph. Notes of an inline element's attributes come right after the paragraph they are inside. With AttributesNone there are none.

These attributes are not notes:

Attribute Goes to
xmlns, xmlns:* nowhere: they declare namespaces, which are resolved
xml:space how white space is read
xml:lang on the root element Metadata.Language (elsewhere, or when longer than 64 bytes, it is a note)
xsi:schemaLocation, xsi:noNamespaceSchemaLocation on the root Metadata.Extra (elsewhere they are notes); never loaded
Comments

A comment is a note block marked Attrs["stored"] = "markup_comment", as go-fast-html writes HTML comments, at its place. A comment inside a text element follows that element's paragraph. Line breaks in a comment are kept; its ends are trimmed. The renderers leave comments out unless RenderOptions.Stored (or StoredKinds with markup_comment) asks for them.

Metadata
Field From
Language xml:lang on the root element, up to 64 bytes
Extra["root"], Extra["root_namespace"] the root element's name as written and its namespace URI
Extra["schema_location"], Extra["no_namespace_schema_location"] xsi:schemaLocation (white space collapsed), xsi:noNamespaceSchemaLocation on the root
Extra["doctype"], Extra["doctype_public"], Extra["doctype_system"] the document type declaration's name and external identifiers
Extra["encoding"] the encoding the file was read in

Encodings

The encoding is decided as XML 1.0 (section 4.3.3 and appendix F) says: a byte order mark (UTF-8, UTF-16LE, UTF-16BE), else UTF-16 recognised from the first bytes of the XML declaration, else the encoding the declaration names, else UTF-8. A declaration that names UTF-16 in a file that reads as ASCII is read as UTF-8; a byte order mark wins over a declaration that disagrees.

Extractor.Charset (the command's -charset NAME) gives the encoding from outside the file, as a MIME charset label does (windows-1251, shift_jis): it is used whatever the declaration names or the bytes suggest; a byte order mark still overrides it, as with go-fast-html's option of the same name. A label that names no encoding decoded here is ErrUnsupported before anything is read (go-fast-html ignores it with a warning).

UTF-8, UTF-16 and windows-1252 are decoded natively, and US-ASCII and ISO-8859-1 as windows-1252, as the WHATWG Encoding standard and browsers do (the bytes 0x80 to 0x9F are then the characters windows-1252 has there, not C1 controls). Every other label is looked up in a copy of go-fast-html's internal/charset (the other single-byte encodings, Shift_JIS, EUC-JP, GBK, GB18030, EUC-KR, Big5). A declared encoding not among them (ISO-2022-JP, UTF-7, EBCDIC), and UTF-32, is ErrUnsupported.

A file that declares no encoding and whose first MiB is not valid UTF-8 is read in the encoding recognised from its bytes where go-fast-html's detection is certain, with text.encoding_detected naming it; otherwise it is ErrUnsupported. Bytes that are not a character in the encoding are written as U+FFFD and counted in text.unmapped_glyphs, in UTF-8 files too.

Entities and document type declarations

  • Never loaded. No external DTD subset, external entity, parameter entity, schema, stylesheet or XInclude is read or fetched. The ones that would make a conforming processor load something are reported with input.external_reference, once per kind with a count: an external DTD subset, a reference to an external entity (or to an external parameter entity in the internal subset), an xml-stylesheet instruction, an XInclude include element. Schema location hints are only recorded in Metadata.Extra.
  • The internal subset is read for general entity declarations only. Element, attribute-list (default attribute values included) and notation declarations are passed over; parameter entities are never expanded.
  • Internal entities are expanded where referenced, their replacement read as markup (an entity may hold elements), in text and in attribute values. Nesting is bounded (MaxEntityDepth, 16), and so are the bytes all expansions produce together: EntityExpansionBase (1 MiB) plus EntityExpansionRatio (8) times the input size, and never more than the decode budget (Limits.MaxDecodedBytes, shared with embedding documents) has left. A reference past either bound is kept as written, and so is every expanding reference after the first one refused; the part carries limit.exceeded. A billion-laughs file and the quadratic-blowup variant finish in milliseconds. A reference to an entity inside its own replacement is kept as written and counted in input.repaired.
  • The five predefined entities and character references always work. A character reference to a control character other than tab, line feed and carriage return is removed with text.control_characters_removed; one to a surrogate or beyond U+10FFFF is U+FFFD.
  • Undeclared entities (&eacute; in a DocBook 4 or JATS file whose DTD is not loaded): if the file has an external DTD subset and the name is one of the 252 character entities of HTML 4 (the names that mean the same in every vocabulary), the character is written and the part carries xml.entities_assumed (info) with the count. Otherwise the reference stays in the text as written (&name;) and the part carries xml.entity_undeclared (warning) with the count. A reference to an external entity also stays as written.

Damaged input

The reader is lenient and counts what it mends in one input.repaired (warning) on the document: an end tag that matches an element further out closes the elements inside it; one that matches nothing is dropped; elements open at the end are closed; a < or & that starts no markup is text; an attribute without a value is passed over and one without quotes runs to white space or >; a quoted value that runs into a < lacks its closing quote and ends at the first > (which ends the tag) or else before the <, and so does a public or system identifier in a document type declaration; a tag or document type declaration cut off by the next < ends there; a comment or CDATA section open at the end runs to it; a second root element and text outside the root are read as they come; a second document type declaration, or one after the root element starts, is not read. Input with no element at all is ErrMalformed.

Processing instructions other than the XML declaration and xml-stylesheet are not text; they are counted in one input.unread_content (warning).

Warnings

Code Severity Where What happened
limit.exceeded error part; document for Limits.MaxParts a bound cut the output; the message says which and how much
text.unmapped_glyphs warning part byte sequences not valid in the encoding were written as U+FFFD
text.control_characters_removed info part control characters other than tab and line feed were removed
text.encoding_detected warning document no encoding declared, not UTF-8; read in the encoding named
input.repaired warning document damaged markup read leniently, with the count
input.external_reference warning document one per kind of reference a conforming processor would load, with the count; nothing was loaded
input.unread_content warning document; part processing instructions, with the count (document); MathML in a profile's formula rule, kept as plain text, with the count (part)
xml.entities_assumed info part undeclared entities read as the HTML 4 character entities of those names, with the count
xml.entity_undeclared warning part undeclared entities kept as written, with the count
part.no_text warning part the document has no block
part.failed error part reading stopped on an I/O error; what was read is kept
xml.unmapped_elements info part profile reading: elements with no rule read generically, names with counts
xml.unmapped_attributes info part profile reading: attributes of mapped elements no rule reads, names with counts
xml.skipped_elements info part profile reading: elements left out by a user profile's skip rules
structure.heading_level_clamped info part profile reading: headings nested deeper than 6 have level 6 and their level in Attrs["level"]

Part warnings are on the last part. DraftProfile returns its own warnings in Draft.Warnings: input.external_reference (warning; schema parts not read because they are not on disk) and limit.exceeded (error; a bound on schema reading cut the draft). No other code is raised.

Errors

ErrMalformed (no element, or a negative size), ErrUnsupported (an encoding not read), ErrLimitExceeded (larger than Limits.MaxInputBytes, or a tag over MaxTagBytes before any element), ErrNoPartsSelected (Options.Parts excludes part 1), and the context's error when it is cancelled.

Limits

Core's limits: Limits.MaxInputBytes (default 4 GiB, ErrLimitExceeded); Limits.MaxDecodedBytes (default 1 GiB), which here bounds entity expansion; Limits.MaxTextBytes (default 256 MiB): every block takes BlockCost plus its text plus its element path from it, and when it is spent the part is cut there with limit.exceeded saying how much input was left unread.

Fixed bounds of this library; those named are exported constants. Each cut is limit.exceeded (error) on the part, once per bound with a count:

Bound Value What happens past it
MaxDepth 512 deeper elements are read as part of their ancestor: their text is kept, their tags and attributes are not read. Element nesting never recurses on the Go stack
MaxAttributes 1,024 per element later attributes are not read
MaxTagBytes 16 MiB reading stops at a larger tag, processing instruction, document type declaration or character reference
MaxNameBytes 256 an element or attribute name in Attrs is cut
MaxPathBytes 4 KiB (256 × 16) Attrs["element"] is cut at a /
MaxValueBytes 1 MiB an attribute value (decoded, entities expanded), and so its note or anchor, is cut; all the values of one tag together are held to MaxTagBytes
(namespace declarations) 16 MiB in scope a declaration past it is not read
MaxBlockBytes 1 MiB a longer text or comment is split into blocks marked Continued, between the pieces text is read in (at most 64 KiB each, cut after white space where there is some); white space at a cut is dropped, as at the ends of any block; no warning is raised
MaxEntityDepth, EntityExpansionBase, EntityExpansionRatio 16; 1 MiB + 8 × input see Entities
blocks waiting for a block 8,192 blocks or 4 MiB notes (attribute notes, comments) waiting for the block they stand in: the block's text so far is given as a piece and the notes follow; blocks waiting for an element not yet known to be a text element: see generic reading
table columns (profiles) 1,024 cells past it are left out
rows one cell spans (profiles) 65,535 the span is cut
spans in one block (profiles) 100,000 later spans are left out (the text stays)
a cell, label or value read from a child (profiles) 1 MiB (MaxBlockBytes) the text is cut there
parts started by part rules 100,000, and Limits.MaxParts the content stays in the last part; parts past Limits.MaxParts are left out

The xml.unmapped_* and xml.skipped_elements warnings list at most 100 names, and count at most 1,000 distinct names, the rest together; this bounds the warning's message, not the output, and raises nothing.

With a profile, a block takes from Limits.MaxTextBytes its text, its Marker, its Attrs keys and values, its span addresses and, for a table, CellCost plus the text of each cell; a value a child gives later (Attrs, an image's alt text) is taken when it comes. The records of links in the stored part count the same way.

The context is checked every 1,024 tokens, and every 1,024 constructs passed over without a token (end tags that match nothing, elements past MaxDepth). Output is deterministic.

What the file declares

XML itself declares nothing about display: whether an element is shown is up to each schema and its stylesheet. Generic reading therefore sets no Display reason. A profile may name a schema's switch that declares content not shown (display: an attribute and its values, giving hidden or no_ink, PROFILE_FORMAT.md section 3); the built-in profiles have no such rule, so no block read with them carries a Display reason. Conditions (Service-Public Condition, S1000D applicability, DocBook condition) are recorded, never evaluated, and every case is extracted.

Nothing about visibility is judged: no colour, size, position or overlap is looked at, and characters are kept as the file has them, invisible ones included. Unshown data (comments, attributes, stored data, metadata) is read as data, never run, evaluated or followed, and bounded by the limits.

Nothing fetched or run

No external DTD subset, external entity, parameter entity, schema (xsi:schemaLocation), stylesheet (xml-stylesheet) or XInclude include is read, fetched or applied; the four kinds a conforming processor would load are reported with input.external_reference (below), and schema locations are recorded in Metadata.Extra. No XSLT, script or query in a document is run. A profile is data: nothing in it is run. -draft-profile (DraftProfile) follows xs:include, xs:import and xs:redefine on disk only, never a URL (Drafting a profile).

Not read

  • Validation. Documents are not validated against an XSD, a DTD or any other schema; default attribute values a DTD declares are not applied (so DITA's @class is usually absent).
  • XInclude. An xi:include is not resolved: its href stays as text or an attribute note, with input.external_reference.
  • XSLT. xml-stylesheet instructions are reported with input.external_reference, not applied; no stylesheet is run.
  • XBRL facts. No profile reads XBRL: an instance is read generically, each fact's text a paragraph and its contextRef, unitRef and decimals attribute notes; contexts, units and the taxonomy are not interpreted.
  • Drafting from RELAX NG or a DTD. -draft-profile reads XSD 1.0 only.
  • MathML is kept as plain text, not converted (with input.unread_content where a profile's formula rule reads it).
  • Processing instructions as text, and external anything (see Nothing fetched or run).

Tests

go test ./... runs everything; -short skips the large worst cases (a million attributes, a 64 MiB text node, a 200 MiB generated stream, a 17 MiB tag). An oracle test compares the character data with encoding/xml's on 500 generated documents. TestCorpus reads GOFAST_CORPUS or ../.testdata/corpus and runs every file of dev/xml*/ and heldout/xml*/ (counts only for held-out files); it skips when the corpus is absent. Fuzz targets: FuzzExtract and FuzzProfile (a profile with every rule kind) (.), FuzzLoad and FuzzSelector (internal/profile), FuzzLoad (internal/xsd), FuzzDraft (internal/draft), FuzzScanner (internal/xmlscan), FuzzParse (internal/dtd), FuzzCharset and FuzzDoubleByte (internal/charset).

Each built-in profile has a corpus test (Test<Profile>Corpus: every dev and held-out file, aggregate counts only for held-out) and an oracle test (Test<Profile>Oracles) over its dev files: the body's text in order is the file's character data in order, apart from the listed exceptions (metadata, attribute-borne text such as TEI pb@n and Formex QUOT.START@CODE), each of which must be met in the dev set; and every block's text is in the source at its offsets. TestCorpusRowLocs checks every row location of every table of every dev file of the XML corpora.

Profiles: TestProfile* read small synthetic documents with one synthetic profile per shape of the format (headings by nesting, dispatch on attributes, CALS and rows tables with spans, labels, out-of-line footnotes, marks from conditions, parts, metadata, links, images, values from attributes); each checks Validate and that every block's text is at its offsets. TestServicePublicCorpus reads every Service-Public file of the dev and held-out sets (aggregate counts only for held-out) and fails on a panic, an error, a Validate failure or a file not read with the profile; it logs the unmapped elements and attributes. TestServicePublicOracles checks every dev file against two oracles: the body's block and cell text, in order, is the file's character data in order (white space aside) apart from a listed set of elements (metadata, the Titre that TitreRiche repeats, values read into Attrs, image alt text), each of which must occur; and every block's text is in the source at its Loc.Offset/Length.

TestOutputDigests (run with -args -digests=FILE, kept outside the repository) records a digest of the JSON of every dev file of every xml-* corpus, read with the profile chosen and generically, on its first run, and fails on any difference on later runs; it also checks that ExtractBytes gives what ExtractFile gives.

Speed

Measured on a Ryzen 9 9900X (a shared machine), Go 1.27.3, one thread (-cpu 1), two to three runs each; user CPU (user-s/op) is reported by the batch benchmark because wall time is noisy there.

  • BenchmarkBatch reads the whole Service-Public dev batch (4,194 files, 106 MB, one of 25 MB), one document at a time, the result dropped: file through ExtractFile with the profile chosen by the root element, 87 to 98 MB/s across runs (1.0 to 1.14 s user CPU, 920 MB allocated and 5.8 million allocations per batch); bytes through ExtractBytes, each file read into a reused buffer first, 103 to 106 MB/s; generic through ExtractFile with generic reading, about 75 MB/s; encodingxml tokenizes the same files with encoding/xml's RawToken (tokens only, no document), about 108 MB/s.
  • BenchmarkServicePublic reads a fixed list of 200 of those files from memory: about 91 MB/s and 993 allocations per file with the profile, 93 MB/s and 1,272 generically.
  • BenchmarkExtract reads a synthetic 10 MiB document generically, about 85 MB/s; BenchmarkEncodingXML tokenizes the same bytes with encoding/xml, about 115 MB/s.

Provenance

See NOTICE. The scanner is adapted from go-fast-odf's (itself from go-fast-ooxml); internal/charset and internal/entity are copies of go-fast-html's, with their tables' provenance.

Documentation

Overview

Package gofastxml extracts text from XML documents of any schema into go-fast-core's shared document model.

The document is read as a stream: besides the document returned, memory is bounded by fixed bounds (one tag, one block, the blocks waiting for an undecided element), not by the file. Nothing a file refers to is loaded or run: no external DTD, entity, schema, stylesheet or XInclude. See README.md for how XML maps to the model.

Index

Constants

View Source
const (
	// DefaultMaxInputBytes bounds the size of the file.
	DefaultMaxInputBytes = 4 << 30
	// DefaultMaxDecodedBytes bounds the bytes entity expansion may
	// produce, with the documents a caller embeds this one in.
	DefaultMaxDecodedBytes = 1 << 30
)

Defaults for the limits of gofastcore.Options.Limits, used where a limit is zero. A negative limit means no limit.

View Source
const (
	// MaxDepth bounds element nesting. Elements deeper are read as part
	// of their ancestor: their text is kept, their tags and attributes
	// are not read.
	MaxDepth = xmlscan.DefaultMaxDepth
	// MaxAttributes bounds the attributes read from one element.
	MaxAttributes = xmlscan.DefaultMaxAttrs
	// MaxTagBytes bounds one tag, processing instruction or document type
	// declaration. Reading stops at a larger one.
	MaxTagBytes = xmlscan.DefaultMaxTokenBytes
	// MaxEntityDepth bounds how deeply entity replacements nest.
	MaxEntityDepth = xmlscan.DefaultMaxEntityDepth
	// EntityExpansionBase and EntityExpansionRatio bound the bytes entity
	// expansion may produce in one document: EntityExpansionBase plus
	// EntityExpansionRatio times the size of the input, and never more
	// than the decode budget (Limits.MaxDecodedBytes) has left.
	EntityExpansionBase  = 1 << 20
	EntityExpansionRatio = 8
	// MaxBlockBytes bounds the text of one block; a longer text is split
	// into blocks marked Continued.
	MaxBlockBytes = generic.MaxBlockBytes
	// MaxNameBytes bounds an element or attribute name in Attrs.
	MaxNameBytes = generic.MaxNameBytes
	// MaxPathBytes bounds Attrs["element"].
	MaxPathBytes = generic.MaxPathBytes
	// MaxValueBytes bounds an attribute note and an anchor.
	MaxValueBytes = generic.MaxValueBytes
)

Fixed bounds. What one cuts is reported with limit.exceeded.

View Source
const (
	// WarnEntitiesAssumed (info): references to entities the file does
	// not declare, in a file with an external DTD subset, were read as
	// the characters HTML 4 gives those names (its 252 character
	// entities; other names stay as written).
	WarnEntitiesAssumed = "xml.entities_assumed"
	// WarnEntityUndeclared (warning): references to entities the file
	// does not declare were kept in the text as written.
	WarnEntityUndeclared = "xml.entity_undeclared"
)

Warning codes of this library. Of the codes shared by all go-fast libraries it raises text.unmapped_glyphs, text.control_characters_removed, text.encoding_detected, input.repaired, input.unread_content, input.external_reference, limit.exceeded, part.no_text and, with a profile, structure.heading_level_clamped. Profile reading adds WarnUnmappedElements, WarnUnmappedAttributes and WarnSkippedElements.

View Source
const (
	// AttrElement is the path of element names from the root, as
	// written, joined with "/", on every block made from an element.
	AttrElement = generic.AttrElement
	// AttrAttribute is the name of the attribute an attribute note holds.
	AttrAttribute = generic.AttrAttribute

	ExtraRoot                      = "root"
	ExtraRootNamespace             = "root_namespace"
	ExtraSchemaLocation            = "schema_location"
	ExtraNoNamespaceSchemaLocation = "no_namespace_schema_location"
	ExtraDoctype                   = "doctype"
	ExtraDoctypePublic             = "doctype_public"
	ExtraDoctypeSystem             = "doctype_system"
	ExtraEncoding                  = "encoding"
)

Keys of Block.Attrs and Metadata.Extra this library writes.

View Source
const (
	// WarnUnmappedElements: elements the profile has no rule for were read
	// generically; the message names them with counts.
	WarnUnmappedElements = engine.WarnUnmappedElements
	// WarnUnmappedAttributes: attributes of elements the profile maps that
	// no rule reads; the message names them (element@attribute) with
	// counts. Their values are not in the output.
	WarnUnmappedAttributes = engine.WarnUnmappedAttributes
	// WarnSkippedElements: elements a user profile's skip rules left out.
	WarnSkippedElements = engine.WarnSkipped
)

Warning codes of profile reading (all info).

View Source
const AttrAnchorParent = engine.AttrAnchorParent

AttrAnchorParent is the Attrs key of the identifier of the nearest enclosing element a profile names with "anchor".

View Source
const AttrHeaderSource = engine.AttrHeaderSource

AttrHeaderSource is "profile" on a table whose header row is the fixed text of the profile's "header", not text of the file.

View Source
const ExtraProfile = "xml.profile"

ExtraProfile is the Metadata.Extra key naming the profile used.

View Source
const ProfileNone = "none"

ProfileNone is the name that forces generic reading.

Variables

This section is empty.

Functions

func Extract

func Extract(ctx context.Context, r io.ReaderAt, size int64, opts *gofastcore.Options) (*gofastcore.Document, error)

Extract reads an XML document with default settings. It is (&Extractor{}).Extract.

func ExtractBytes

func ExtractBytes(ctx context.Context, data []byte, opts *gofastcore.Options) (*gofastcore.Document, error)

ExtractBytes extracts a document held in memory with default settings.

func ExtractFile

func ExtractFile(ctx context.Context, path string, opts *gofastcore.Options) (*gofastcore.Document, error)

ExtractFile opens the named file and extracts it with default settings.

func Formats

func Formats() []string

Formats lists the formats this library extracts.

Types

type Attributes

type Attributes uint8

Attributes says what becomes of attributes.

const (
	// AttributesNotes, the default, makes each attribute with a value a
	// note of type attribute at its element's place.
	AttributesNotes Attributes = iota
	// AttributesNone leaves attribute notes out, for data-style XML.
	AttributesNone
)

type Draft

type Draft struct {
	// JSON is the draft, in the profile format (PROFILE_FORMAT.md).
	JSON []byte
	// Files are the schema documents read, the named one first.
	Files []string
	// Warnings say what of the schema was not read: a location that is a
	// URL or names no file on disk (input.external_reference), a limit
	// that left documents out (limit.exceeded). The draft's notes say the
	// same.
	Warnings []gofastcore.Warning
}

Draft is a draft profile made from an XML Schema by DraftProfile, for a person to finish. It is not a profile: each rule carries "review": true, which LoadProfile refuses.

func DraftProfile

func DraftProfile(ctx context.Context, path string) (*Draft, error)

DraftProfile reads the XML Schema (XSD) at path, with the documents it includes, imports and redefines that are on disk relative to it, and writes a draft profile from what the schema declares. Nothing is fetched. DTDs and RELAX NG schemas are not read. The error is for the named file only: one that cannot be read or is not a schema.

type Extractor

type Extractor struct {
	// Attributes says what becomes of attributes in generic reading.
	Attributes Attributes
	// Profiles are tried, in order, before the built-in profiles: the
	// first whose "match" the root element meets is used.
	Profiles []*Profile
	// Profile forces the profile of that name, from Profiles or the
	// built-in ones, whatever the root element; ProfileNone ("none")
	// forces generic reading. Empty means choose by the root element.
	Profile string
	// Charset is the character encoding of the input, given from outside
	// it, as a label such as the charset parameter of a MIME Content-Type
	// gives it ("windows-1251", "shift_jis"). It is used whatever the XML
	// declaration names or the bytes suggest; a byte order mark still
	// overrides it, as with go-fast-html's Charset. A label that names no
	// encoding this library decodes is an error (ErrUnsupported) before
	// anything is read. Empty means none.
	Charset string
}

Extractor extracts XML documents. The zero value is ready to use and safe for concurrent use.

func (*Extractor) Extract

func (e *Extractor) Extract(ctx context.Context, r io.ReaderAt, size int64, opts *gofastcore.Options) (*gofastcore.Document, error)

Extract reads size bytes from r and returns the document: one part of kind body, number 1. An error wraps one of the gofastcore errors:

  • ErrMalformed: the input has no element, or size is negative.
  • ErrUnsupported: the input is in an encoding this library does not read: one it declares, UTF-32 or EBCDIC, or, with none declared and the bytes not UTF-8, one that could not be recognised.
  • ErrLimitExceeded: the input is larger than Limits.MaxInputBytes, or a tag before any element is larger than MaxTagBytes.
  • ErrNoPartsSelected: Options.Parts does not select part 1.

func (*Extractor) ExtractFile

func (e *Extractor) ExtractFile(ctx context.Context, path string, opts *gofastcore.Options) (*gofastcore.Document, error)

ExtractFile opens the named file and extracts it. The file name is recorded as the document's source name unless opts.Name is set.

func (*Extractor) Formats

func (e *Extractor) Formats() []string

Formats lists the formats this library extracts.

type Profile

type Profile struct {
	// contains filtered or unexported fields
}

Profile maps the elements of one XML schema to the shared model. See PROFILE_FORMAT.md. A Profile is immutable and safe for concurrent use.

func BuiltinProfile

func BuiltinProfile(name string) *Profile

BuiltinProfile returns the built-in profile of that name, or nil.

func BuiltinProfiles

func BuiltinProfiles() []*Profile

BuiltinProfiles returns the built-in profiles in the order they are tried.

func LoadProfile

func LoadProfile(r io.Reader) (*Profile, error)

LoadProfile reads and validates a profile. An error names the rule it is about.

func (*Profile) Name

func (p *Profile) Name() string

Name returns the profile's name, as Metadata.Extra["xml.profile"] gives it.

func (*Profile) Title

func (p *Profile) Title() string

Title returns the profile's description.

Directories

Path Synopsis
cmd
gofastxml command
Command gofastxml extracts XML documents into the shared document model of the go-fast libraries and prints them as JSON, Markdown or plain text.
Command gofastxml extracts XML documents into the shared document model of the go-fast libraries and prints them as JSON, Markdown or plain text.
internal
charset
Package charset decides the character encoding of an HTML document and decodes it to UTF-8, keeping a map from offsets in the decoded text back to offsets in the source bytes.
Package charset decides the character encoding of an HTML document and decodes it to UTF-8, keeping a map from offsets in the decoded text back to offsets in the source bytes.
draft
Package draft writes a draft profile from what an XML Schema declares, for a person to finish.
Package draft writes a draft profile from what an XML Schema declares, for a person to finish.
dtd
Package dtd reads a document type declaration for what an XML reader that loads nothing needs from it: the external subset's identifiers and the general entities the internal subset declares.
Package dtd reads a document type declaration for what an XML reader that loads nothing needs from it: the external subset's identifiers and the general entities the internal subset declares.
engine
Package engine reads a document with a profile (PROFILE_FORMAT.md): it consumes the element events of package xmlread, as the generic reader does, and makes the blocks the profile's rules say.
Package engine reads a document with a profile (PROFILE_FORMAT.md): it consumes the element events of package xmlread, as the generic reader does, and makes the blocks the profile's rules say.
entity
Package entity resolves HTML character references: the named references of the WHATWG HTML standard (section 13.5, embedded from entities.txt) and the numeric ones with the standard's replacement rules.
Package entity resolves HTML character references: the named references of the WHATWG HTML standard (section 13.5, embedded from entities.txt) and the numeric ones with the standard's replacement rules.
generic
Package generic builds blocks from XML with no knowledge of its schema (README, "How XML maps without a profile"), and holds the part writer every consumer of element events shares.
Package generic builds blocks from XML with no knowledge of its schema (README, "How XML maps without a profile"), and holds the part writer every consumer of element events shares.
profile
Package profile loads and validates profiles (PROFILE_FORMAT.md): JSON files that map the elements of one XML schema to the shared model.
Package profile loads and validates profiles (PROFILE_FORMAT.md): JSON files that map the elements of one XML schema to the shared model.
xmlenc
Package xmlenc decides the character encoding of an XML document and decodes it to UTF-8 as a stream.
Package xmlenc decides the character encoding of an XML document and decodes it to UTF-8 as a stream.
xmlread
Package xmlread turns the scanner's tokens into element events for a consumer that builds blocks, and keeps what every consumer needs: the stack of open elements with their names, namespaces, attributes, xml:space and offsets; the document type declaration and its entities; and the counts of what the file holds that is reported rather than read (external references, processing instructions, repairs, cuts).
Package xmlread turns the scanner's tokens into element events for a consumer that builds blocks, and keeps what every consumer needs: the stack of open elements with their names, namespaces, attributes, xml:space and offsets; the document type declaration and its entities; and the counts of what the file holds that is reported rather than read (external references, processing instructions, repairs, cuts).
xmlscan
Package xmlscan is a streaming pull tokenizer for XML.
Package xmlscan is a streaming pull tokenizer for XML.
xsd
Package xsd reads an XML Schema (XSD 1.0) as data, with this module's own XML reader, far enough to say what elements a schema declares and what each may contain: global and local elements, named and anonymous types, derivation by extension and restriction, groups and attribute groups, substitution groups, wildcards and the types of attributes.
Package xsd reads an XML Schema (XSD 1.0) as data, with this module's own XML reader, far enough to say what elements a schema declares and what each may contain: global and local elements, named and anonymous types, derivation by extension and restriction, groups and attribute groups, substitution groups, wildcards and the types of attributes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL