pub struct Doc<'a>(/* private fields */);Expand description
A finished document, held in the composer’s arena.
Doc is a Copy handle: passing it around costs nothing and every
navigator taken from it shares the composer’s lifetime, so a node of one
document can be spliced into another without any lifetime work.
use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::document::SerializeOptions;
use xsd_schema::namespace::NameTable;
use xsd_schema::navigator::DomNavigator;
let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<!--first--><a><b/></a>")?;
assert_eq!(doc.children().len(), 2);
assert_eq!(doc.document_element().unwrap().local_name(), "a");
assert_eq!(doc.to_xml(&SerializeOptions::default())?, "<!--first--><a><b/></a>");Implementations§
Source§impl<'a> Doc<'a>
impl<'a> Doc<'a>
Sourcepub fn root(&self) -> Nav<'a>
pub fn root(&self) -> Nav<'a>
A navigator on the document node.
use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::namespace::NameTable;
let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<a><b/></a>")?;
use xsd_schema::navigator::{DomNavigator, DomNodeType};
assert_eq!(doc.root().node_type(), DomNodeType::Root);Sourcepub fn document_element(&self) -> Option<Nav<'a>>
pub fn document_element(&self) -> Option<Nav<'a>>
The first top-level element, if the document has one.
A document built with
build_sequence may have none, or several;
use children for those.
use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::namespace::NameTable;
let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<!--lead--><a/>")?;
use xsd_schema::navigator::DomNavigator;
assert_eq!(doc.document_element().unwrap().local_name(), "a");Sourcepub fn children(&self) -> Vec<Nav<'a>>
pub fn children(&self) -> Vec<Nav<'a>>
Every top-level node, in document order.
This is how a sequence of elements becomes queryable: build it with
build_sequence, then bind children() to
a variable.
use bumpalo::Bump;
use xsd_schema::compose::{Composer, Content, Form, IntoXPathValue, Name};
use xsd_schema::namespace::NameTable;
let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let mut count = Form::new(Name::local("nbids"));
count.push(Content::Text("2".to_string()));
let mut first = Form::new(Name::local("bid_count"));
first.push(Content::Element(count));
let mut count = Form::new(Name::local("nbids"));
count.push(Content::Text("5".to_string()));
let mut second = Form::new(Name::local("bid_count"));
second.push(Content::Element(count));
let seq = c.build_sequence(vec![first, second])?;
let total = c.eval(
"sum($s/nbids)",
&["s"],
None,
vec![("s", seq.children().into_xpath_value())],
)?;
assert_eq!(total.string()?, "7");Sourcepub fn to_xml(&self, opts: &SerializeOptions) -> Result<String, ComposeError>
pub fn to_xml(&self, opts: &SerializeOptions) -> Result<String, ComposeError>
Writes the document as XML.
use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::document::SerializeOptions;
use xsd_schema::namespace::NameTable;
let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<a><b>1</b></a>")?;
// Compact by default; `indent` asks for layout instead.
assert_eq!(doc.to_xml(&SerializeOptions::default())?, "<a><b>1</b></a>");
let formatted = SerializeOptions { indent: Some(2), ..SerializeOptions::default() };
assert_eq!(doc.to_xml(&formatted)?, "<a>\n <b>1</b>\n</a>");Sourcepub fn write_xml<W: Write>(
&self,
writer: W,
opts: &SerializeOptions,
) -> Result<(), ComposeError>
pub fn write_xml<W: Write>( &self, writer: W, opts: &SerializeOptions, ) -> Result<(), ComposeError>
Writes the document as XML into writer, without an intermediate
String.
use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::document::SerializeOptions;
use xsd_schema::namespace::NameTable;
let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<a/>")?;
let mut out: Vec<u8> = Vec::new();
doc.write_xml(&mut out, &SerializeOptions::default())?;
assert_eq!(String::from_utf8(out)?, "<a/>");Sourcepub fn inner(&self) -> &'a BufferDocument<'a>
pub fn inner(&self) -> &'a BufferDocument<'a>
The underlying document.
use bumpalo::Bump;
use xsd_schema::compose::Composer;
use xsd_schema::namespace::NameTable;
let arena = Bump::new();
let names = NameTable::new();
let c = Composer::new(&arena, &names);
let doc = c.load_str("<a/>")?;
// No schema set was configured, so the document is bound to none.
assert!(doc.inner().schema_set().is_none());Trait Implementations§
impl<'a> Copy for Doc<'a>
Source§impl<'a> IntoContextNode<'a> for Doc<'a>
impl<'a> IntoContextNode<'a> for Doc<'a>
Source§fn into_context_node(self) -> Nav<'a>
fn into_context_node(self) -> Nav<'a>
Converts
self into the navigator to focus on.Source§impl<'a> IntoContextNode<'a> for &Doc<'a>
impl<'a> IntoContextNode<'a> for &Doc<'a>
Source§fn into_context_node(self) -> Nav<'a>
fn into_context_node(self) -> Nav<'a>
Converts
self into the navigator to focus on.Source§impl<'a> IntoXPathValue<'a> for Doc<'a>
impl<'a> IntoXPathValue<'a> for Doc<'a>
Source§fn into_xpath_value(self) -> XPathValue<Nav<'a>>
fn into_xpath_value(self) -> XPathValue<Nav<'a>>
Converts
self into a bindable sequence.Source§impl<'a> IntoXPathValue<'a> for &Doc<'a>
impl<'a> IntoXPathValue<'a> for &Doc<'a>
Source§fn into_xpath_value(self) -> XPathValue<Nav<'a>>
fn into_xpath_value(self) -> XPathValue<Nav<'a>>
Converts
self into a bindable sequence.Auto Trait Implementations§
impl<'a> !RefUnwindSafe for Doc<'a>
impl<'a> !Send for Doc<'a>
impl<'a> !Sync for Doc<'a>
impl<'a> !UnwindSafe for Doc<'a>
impl<'a> Forget for Doc<'a>
impl<'a> Freeze for Doc<'a>where
&'a BufferDocument<'a>: Freeze,
impl<'a> Move for Doc<'a>
impl<'a> Unpin for Doc<'a>where
&'a BufferDocument<'a>: Unpin,
impl<'a> UnsafeUnpin for Doc<'a>where
&'a BufferDocument<'a>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more