From aea3309502fed947446af8122adbbb68a0fc4559 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Mon, 18 Aug 2025 20:42:43 +0200 Subject: [PATCH] Use fixed column name for `query xml` output (#16461) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a breaking change. Refs https://discord.com/channels/601130461678272522/615329862395101194/1406413134402551869 Depends on (and includes) nushell/nushell#16459, which should be merged first Partially addresses nushell/nushell#15717 ## Motivation and technical details Variable column names are hard to work with. What's worse, currently Nushell truncates the column name, making it even more unpredictable. The only reliable way to deal with that is to rename the results column like this: ```nushell .... | query xml "//item/title|//title" | rename string_value | ... ``` so why would we make our users jump through these hoops? To provide some parallels, `query db` does not add the query into its output, and neither does `get`. The choice of the column name comes from XPath spec: https://www.w3.org/TR/xpath-10/#dt-string-value, adapted to Nushell conventions ## Release notes summary - What our users need to know `query xml` outputs the results (for nodeset results) in a column with a fixed name. Before: ```nushell open -r tests/fixtures/formats/jt.xml | query xml '//channel/title|//item/title' # => ╭───┬───────────────────────────────────────────╮ # => │ # │ //channel/title|/... │ # => ├───┼───────────────────────────────────────────┤ # => │ 0 │ JT │ # => │ 1 │ Creating crossplatform Rust terminal apps │ # => ╰───┴───────────────────────────────────────────╯ ``` Now: ```nushell open -r tests/fixtures/formats/jt.xml | query xml '//channel/title|//item/title' # => ╭───┬───────────────────────────────────────────╮ # => │ # │ string_value │ # => ├───┼───────────────────────────────────────────┤ # => │ 0 │ JT │ # => │ 1 │ Creating crossplatform Rust terminal apps │ # => ╰───┴───────────────────────────────────────────╯ ``` ## Tasks after submitting - [ ] Update the [documentation](https://github.com/nushell/nushell.github.io) --- crates/nu_plugin_query/src/query_xml.rs | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/crates/nu_plugin_query/src/query_xml.rs b/crates/nu_plugin_query/src/query_xml.rs index 953720853d..75332764f4 100644 --- a/crates/nu_plugin_query/src/query_xml.rs +++ b/crates/nu_plugin_query/src/query_xml.rs @@ -89,19 +89,10 @@ pub fn execute_xpath_query( Ok(sxd_xpath::Value::Number(n)) => Ok(Value::float(n, call.head)), Ok(sxd_xpath::Value::String(s)) => Ok(Value::string(s, call.head)), Ok(sxd_xpath::Value::Nodeset(ns)) => { - // Some xpath statements can be long, so let's truncate it with ellipsis - let mut key = query_string.clone(); - if query_string.len() >= 20 { - key.truncate(17); - key += "..."; - } else { - key = query_string.to_string(); - }; - let mut records: Vec = vec![]; for n in ns.document_order() { records.push(Value::record( - record! {key.clone() => Value::string(n.string_value(), call.head)}, + record! {"string_value" => Value::string(n.string_value(), call.head)}, call.head, )); } @@ -205,7 +196,7 @@ mod tests { let expected = Value::list( vec![Value::test_record(record! { - "//qp:b/text()" => Value::string("yay", Span::test_data()), + "string_value" => Value::string("yay", Span::test_data()), })], Span::test_data(), ); @@ -293,7 +284,7 @@ mod tests { let expected = Value::list( vec![Value::test_record(record! { - "/elt" => Value::string("hello", Span::test_data()), + "string_value" => Value::string("hello", Span::test_data()), })], Span::test_data(), );