pub struct Value<'a>(/* private fields */);Expand description
The result of one evaluation.
A Value is a thin wrapper around the engine’s own
XPathValue: nothing is atomized, copied or
flattened until a method asks for it. The accessors are the ones a host
needs — the nodes, the atomic values, a boolean, a string, a number, an
order-by key — and each says exactly when it refuses.
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>1</b><b>2</b></a>")?;
let bs = c.eval("//b", &[], Some(doc.root()), Vec::new())?;
assert_eq!(bs.len(), 2);
assert_eq!(bs.nodes()?.len(), 2);
let total = c.eval("sum(//b)", &[], Some(doc.root()), Vec::new())?;
assert_eq!(total.number()?, 3.0);
assert_eq!(total.string()?, "3");Implementations§
Source§impl<'a> Value<'a>
impl<'a> Value<'a>
Sourcepub fn nodes(&self) -> Result<Vec<Nav<'a>>, ComposeError>
pub fn nodes(&self) -> Result<Vec<Nav<'a>>, ComposeError>
The node items, in order.
An atomic item is ComposeError::NotANode: dropping it silently
would hide a mistake in the expression.
pipe::nodes is the streaming
counterpart.
use bumpalo::Bump;
use xsd_schema::compose::{ComposeError, Composer};
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("<a><b/></a>")?;
let nodes = c.eval("//b", &[], Some(doc.root()), Vec::new())?.nodes()?;
assert_eq!(nodes[0].local_name(), "b");
// A string is not a node.
let atomic = c.eval("'x'", &[], None, Vec::new())?;
assert!(matches!(atomic.nodes(), Err(ComposeError::NotANode)));Sourcepub fn atomics(&self) -> Result<Vec<XmlValue>, ComposeError>
pub fn atomics(&self) -> Result<Vec<XmlValue>, ComposeError>
Every item atomized, in order.
A node is atomized with
atomize_node, so an untyped
element yields its xs:untypedAtomic string value and a typed one its
typed value: one value per member when its type is a list type
(XDM §3.3.1.2). A nilled element contributes nothing.
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>1</b><b>2</b></a>")?;
let atomics = c.eval("//b", &[], Some(doc.root()), Vec::new())?.atomics()?;
let text: Vec<String> = atomics.iter().map(|v| v.to_string_value()).collect();
assert_eq!(text, ["1", "2"]);Sourcepub fn boolean(&self) -> Result<bool, ComposeError>
pub fn boolean(&self) -> Result<bool, ComposeError>
The effective boolean value (XPath 2.0 §2.4.3).
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>x</b></a>")?;
assert!(c.eval("//b", &[], Some(doc.root()), Vec::new())?.boolean()?);
assert!(!c.eval("//zzz", &[], Some(doc.root()), Vec::new())?.boolean()?);Sourcepub fn string(&self) -> Result<String, ComposeError>
pub fn string(&self) -> Result<String, ComposeError>
The string value of the single item, "" for the empty sequence.
More than one item is ComposeError::NotSingleton; use
atomics to join a sequence yourself.
use bumpalo::Bump;
use xsd_schema::compose::{ComposeError, 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>one</b><b>two</b></a>")?;
assert_eq!(c.eval("//b[1]", &[], Some(doc.root()), Vec::new())?.string()?, "one");
assert_eq!(c.eval("()", &[], None, Vec::new())?.string()?, "");
assert!(matches!(
c.eval("//b", &[], Some(doc.root()), Vec::new())?.string(),
Err(ComposeError::NotSingleton { len: 2 }),
));Sourcepub fn number(&self) -> Result<f64, ComposeError>
pub fn number(&self) -> Result<f64, ComposeError>
fn:number() of the single item: NaN when it does not convert, and
NaN for the empty sequence.
More than one item is ComposeError::NotSingleton.
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);
assert_eq!(c.eval("2 + 3", &[], None, Vec::new())?.number()?, 5.0);
assert!(c.eval("'nope'", &[], None, Vec::new())?.number()?.is_nan());
assert!(c.eval("()", &[], None, Vec::new())?.number()?.is_nan());Sourcepub fn key(&self) -> Result<Option<XmlValue>, ComposeError>
pub fn key(&self) -> Result<Option<XmlValue>, ComposeError>
The order by key: the single atomized item, or None for the empty
sequence.
More than one item is XPTY0004, reported as
ComposeError::XPath — an order-by key must be at most one value
(XQuery 1.0 §3.8.3).
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>7</b></a>")?;
let key = c.eval("b", &[], Some(doc.document_element().unwrap()), Vec::new())?.key()?;
assert_eq!(key.unwrap().to_string_value(), "7");
assert!(c.eval("()", &[], None, Vec::new())?.key()?.is_none());Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
How many items the sequence holds.
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);
assert_eq!(c.eval("()", &[], None, Vec::new())?.len(), 0);
assert_eq!(c.eval("1", &[], None, Vec::new())?.len(), 1);
assert_eq!(c.eval("(1, 2, 3)", &[], None, Vec::new())?.len(), 3);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the sequence is empty.
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);
assert!(c.eval("()", &[], None, Vec::new())?.is_empty());
assert!(!c.eval("1", &[], None, Vec::new())?.is_empty());Sourcepub fn iter(&self) -> impl Iterator<Item = &XmlItem<Nav<'a>>> + '_
pub fn iter(&self) -> impl Iterator<Item = &XmlItem<Nav<'a>>> + '_
Borrows the items, in order.
A one-item sequence is one item here: the engine stores a singleton without a backing vector, and this iterator yields it all the same.
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);
assert_eq!(c.eval("1", &[], None, Vec::new())?.iter().count(), 1);
assert_eq!(c.eval("(1, 2, 3)", &[], None, Vec::new())?.iter().count(), 3);Sourcepub fn into_inner(self) -> XPathValue<Nav<'a>>
pub fn into_inner(self) -> XPathValue<Nav<'a>>
The wrapped engine value.
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 sequence = c.eval("(1, 2)", &[], None, Vec::new())?.into_inner();
assert_eq!(sequence.into_vec().len(), 2);Sourcepub fn inner(&self) -> &XPathValue<Nav<'a>>
pub fn inner(&self) -> &XPathValue<Nav<'a>>
Borrows the wrapped engine value.
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 value = c.eval("(1, 2)", &[], None, Vec::new())?;
assert_eq!(value.inner().len(), 2);
// …and the `Value` is still usable afterwards.
assert_eq!(value.len(), 2);Trait Implementations§
Source§fn from(value: XPathValue<Nav<'a>>) -> Self
fn from(value: XPathValue<Nav<'a>>) -> Self
Source§impl<'a> IntoContent<'a> for Value<'a>
impl<'a> IntoContent<'a> for Value<'a>
Source§fn into_content(self) -> Content<'a>
fn into_content(self) -> Content<'a>
self into one content item.Source§impl<'a> IntoContent<'a> for &Value<'a>
impl<'a> IntoContent<'a> for &Value<'a>
Source§fn into_content(self) -> Content<'a>
fn into_content(self) -> Content<'a>
self into one content item.Source§impl<'a> IntoXPathValue<'a> for Value<'a>
impl<'a> IntoXPathValue<'a> for Value<'a>
Source§fn into_xpath_value(self) -> XPathValue<Nav<'a>>
fn into_xpath_value(self) -> XPathValue<Nav<'a>>
self into a bindable sequence.Source§impl<'a> IntoXPathValue<'a> for &Value<'a>
impl<'a> IntoXPathValue<'a> for &Value<'a>
Source§fn into_xpath_value(self) -> XPathValue<Nav<'a>>
fn into_xpath_value(self) -> XPathValue<Nav<'a>>
self into a bindable sequence.