|
| 1 | +# AcuteML DOM/Xpath API |
| 2 | + |
| 3 | +AcuteML provides a DOM/Xpath API that you can use to do lower level XML/HTML manipulation if needed. |
| 4 | + |
| 5 | +You can use AcuteML utilities to define custom type constructors from scratch or to override `@aml` defined constructors. |
| 6 | + |
| 7 | +Notice that if you don't use `@aml`, you should include `aml::Node` as one of your fields. |
| 8 | + |
| 9 | +Functions to use for custom html/xml constructor: |
| 10 | +- [createnode](@ref): function to create a node/document |
| 11 | +- [addnode!](@ref) : To add nodes (single or a vector of nodes) as a child of given a node/document. |
| 12 | +Use these functions, to make a method that calculates the `aml` inside the function and returns all of the fields. |
| 13 | + |
| 14 | +Functions to use for custom html/xml extractor: |
| 15 | +- [findfirst](@ref): to find the first node based on the given node name |
| 16 | +- [findall](@ref): to find all of the nodes based on the given node name |
| 17 | +- [findcontent](@ref) : to get the content of a node based on the given name |
| 18 | +Use these functions, to make a method that gets the `aml::Node` and calculates and returns all of the fields. |
| 19 | + |
| 20 | +Functions to support mutability: |
| 21 | +- [updatecontent!](@ref): Finds all the elements with the address of string in the node, and updates the content. |
| 22 | + |
| 23 | +## Making a Type and constructor from scratch using AcuteML Backend |
| 24 | + |
| 25 | +# Example: |
| 26 | +In this example we define `Identity` with custom constructors: |
| 27 | +```julia |
| 28 | +using AcuteML |
| 29 | + |
| 30 | +mutable struct Identity |
| 31 | + pitch::UN{Pitch} |
| 32 | + rest::UN{Rest} |
| 33 | + unpitched::UN{Unpitched} |
| 34 | + aml::Node |
| 35 | +end |
| 36 | + |
| 37 | +function Identity(;pitch = nothing, rest = nothing, unpitched = nothing) |
| 38 | + |
| 39 | + # This constructor only allows one the fields to exist - similar to choice element in XS |
| 40 | + |
| 41 | + aml = createnode(AbsNormal, "identity") |
| 42 | + |
| 43 | + if pitch != nothing |
| 44 | + addnode!(aml, "pitch", pitch, AbsNormal) |
| 45 | + elseif rest != nothing |
| 46 | + addnode!(aml, "rest", rest, AbsNormal) |
| 47 | + elseif unpitched != nothing |
| 48 | + addnode!(aml, "unpitched", unpitched, AbsNormal) |
| 49 | + else |
| 50 | + error("one of the pitch, rest or unpitched should be given") |
| 51 | + end |
| 52 | + |
| 53 | + return Identity(pitch, rest, unpitched, aml) |
| 54 | +end |
| 55 | + |
| 56 | +function Identity(;aml) |
| 57 | + |
| 58 | + pitch = findcontent(Pitch, "pitch", aml, AbsNormal) |
| 59 | + rest = findcontent(Rest, "rest", aml, AbsNormal) |
| 60 | + unpitched = findcontent(Unpitched, "unpitched", aml, AbsNormal) |
| 61 | + |
| 62 | + return Identity(pitch, rest, unpitched, aml) |
| 63 | +end |
| 64 | +``` |
0 commit comments