> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/appendixes/contrib/xml2/xml2-functions.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# F.50.2. 函式說明

## F.50.2. 函式說明 [#](#xml2-functions) <a href="#xml2-functions" id="xml2-functions"></a>

[表 F.37](#xml2-functions-table) 列出此模組提供的函式。這些函式提供簡單直接的 XML 剖析與 XPath 查詢。

### 表 F.37. `xml2` 函式 [#](#xml2-functions-table) <a href="#xml2-functions-table" id="xml2-functions-table"></a>

<table data-header-hidden><thead><tr><th></th></tr></thead><tbody><tr><td><p>函式</p><p>說明</p></td></tr><tr><td><p><code>xml_valid</code> ( <em><code>document</code></em> <code>text</code> ) → <code>boolean</code></p><p>剖析給定的文件，若該文件是格式正確（well-formed）的 XML 則傳回 true。（注意：這是標準 PostgreSQL 函式 <code>xml_is_well_formed()</code> 的別名。名稱 <code>xml_valid()</code> 在技術上並不正確，因為在 XML 中，有效（validity）與格式正確（well-formedness）的意義不同。）</p></td></tr><tr><td><p><code>xpath_string</code> ( <em><code>document</code></em> <code>text</code>, <em><code>query</code></em> <code>text</code> ) → <code>text</code></p><p>對提供的文件執行 XPath 查詢，並將結果轉換為 <code>text</code>。</p></td></tr><tr><td><p><code>xpath_number</code> ( <em><code>document</code></em> <code>text</code>, <em><code>query</code></em> <code>text</code> ) → <code>real</code></p><p>對提供的文件執行 XPath 查詢，並將結果轉換為 <code>real</code>。</p></td></tr><tr><td><p><code>xpath_bool</code> ( <em><code>document</code></em> <code>text</code>, <em><code>query</code></em> <code>text</code> ) → <code>boolean</code></p><p>對提供的文件執行 XPath 查詢，並將結果轉換為 <code>boolean</code>。</p></td></tr><tr><td><p><code>xpath_nodeset</code> ( <em><code>document</code></em> <code>text</code>, <em><code>query</code></em> <code>text</code>, <em><code>toptag</code></em> <code>text</code>, <em><code>itemtag</code></em> <code>text</code> ) → <code>text</code></p><p>對文件執行查詢，並以 XML 標籤包裝結果。若結果為多值，輸出看起來會像這樣：</p><pre><code>&#x3C;toptag>
&#x3C;itemtag>Value 1 which could be an XML fragment&#x3C;/itemtag>
&#x3C;itemtag>Value 2....&#x3C;/itemtag>
&#x3C;/toptag>
</code></pre><p>若 <em><code>toptag</code></em> 或 <em><code>itemtag</code></em> 為空字串，則會省略相應的標籤。</p></td></tr><tr><td><p><code>xpath_nodeset</code> ( <em><code>document</code></em> <code>text</code>, <em><code>query</code></em> <code>text</code>, <em><code>itemtag</code></em> <code>text</code> ) → <code>text</code></p><p>與 <code>xpath_nodeset(document, query, toptag, itemtag)</code> 相同，但結果會省略 <em><code>toptag</code></em>。</p></td></tr><tr><td><p><code>xpath_nodeset</code> ( <em><code>document</code></em> <code>text</code>, <em><code>query</code></em> <code>text</code> ) → <code>text</code></p><p>與 <code>xpath_nodeset(document, query, toptag, itemtag)</code> 相同，但結果會省略兩個標籤。</p></td></tr><tr><td><p><code>xpath_list</code> ( <em><code>document</code></em> <code>text</code>, <em><code>query</code></em> <code>text</code>, <em><code>separator</code></em> <code>text</code> ) → <code>text</code></p><p>對文件執行查詢，並傳回以指定分隔符號分隔的多個值，例如 <code>Value 1,Value 2,Value 3</code>（當 <em><code>separator</code></em> 為 <code>,</code> 時）。</p></td></tr><tr><td><p><code>xpath_list</code> ( <em><code>document</code></em> <code>text</code>, <em><code>query</code></em> <code>text</code> ) → <code>text</code></p><p>這是上述函式的包裝函式，使用 <code>,</code> 作為分隔符號。</p></td></tr></tbody></table>

<br>

[返回 F.50. xml2 — XPath 查詢與 XSLT 功能](https://docs.postgresql.tw/19/appendixes/contrib/xml2.md#xml2)

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/xml2.html)（原文版本：18.6；核對日期：2026-10-22）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/appendixes/contrib/xml2/xml2-functions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
