mirror of
https://github.com/alexandrev/xslt-lab.git
synced 2026-09-18 19:43:16 +00:00
53e90ef86e
Full coverage of XSLT 1.0, 2.0 and 3.0 elements and XPath functions: - 59 XSLT elements (xsl:stylesheet → xsl:use-accumulators) - 170 XPath functions (1.0 node/string/numeric/boolean, 2.0 sequence/ date/QName/string, 3.0 HOF/map/array/JSON/streaming) Each page: description, parameters table, return value, 2 runnable Saxon examples with input XML + stylesheet + output, notes, cross-links. xsltCompletions.js: all 229 entries now have blogSlug for hover links. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2.9 KiB
2.9 KiB
title, description, date, version, versionLabel, category, syntax, tags
| title | description | date | version | versionLabel | category | syntax | tags | ||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| parse-xml() | Parses a well-formed XML string and returns it as a new document node, usable like any other loaded XML document. | 2026-04-18T00:00:00Z | 2.0 | XSLT 2.0 | node function | parse-xml(string) |
|
Description
parse-xml() takes a string containing a well-formed XML document and returns a new document node. The resulting tree can be navigated with XPath expressions just like a document loaded with doc() or document().
If the string is not a well-formed XML document, the function raises a dynamic error. For XML fragments (content without a single root element), use parse-xml-fragment() instead.
parse-xml() is defined in XPath 3.0 but is widely supported in Saxon 9.x and later with XSLT 2.0 stylesheets.
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
string |
xs:string? | Yes | A string containing a complete, well-formed XML document. |
Return value
document-node() — a new document node whose children are the parsed content, or the empty sequence if the argument is the empty sequence.
Examples
Parse XML stored in an element
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
<payload>
<data><![CDATA[<?xml version="1.0"?><items><item id="1">Alpha</item><item id="2">Beta</item></items>]]></data>
</payload>
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/payload">
<xsl:variable name="inner" select="parse-xml(data)"/>
<extracted>
<xsl:copy-of select="$inner/items/item"/>
</extracted>
</xsl:template>
</xsl:stylesheet>
Output:
<extracted>
<item id="1">Alpha</item>
<item id="2">Beta</item>
</extracted>
Count elements in a dynamically built XML string
Stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:variable name="xml-string"><root><a/><b/><c/></root></xsl:variable>
<xsl:variable name="doc" select="parse-xml($xml-string)"/>
<xsl:value-of select="count($doc/root/*)"/>
</xsl:template>
</xsl:stylesheet>
Output:
3
Notes
parse-xml()requires the input to be a complete, well-formed document with a single root element. Useparse-xml-fragment()for fragments.- The resulting document node has no document URI (
document-uri()returns the empty sequence). - Available in Saxon 9.x+ when using
version="2.0"stylesheets; it is formally part of XPath 3.0.