是否可以使用 XPath 引用 XML 文件的序言内容?
Is it possible to reference the prolog contents of an XML file with XPath?
我希望能够使用 XPath return 来自 XML 文件的 href="stylesheet.xsl"
值,但我似乎无法找出引用其中属性的表达式?-tags 如果可能的话。示例文件:
<?xml-stylesheet type="text/xsl" href="https://foo.bar" ?>
<test>
<test2>
</test2>
</test>
目标是找出给定 XML 文件引用的样式表,因此在本例中我想 return "https://foo.bar"
作为结果集。有什么想法吗?
您可以通过 processing-instruction()
节点测试在序言中访问 Processing Instruction (PI),就像在 XML 文档的其余部分访问它一样。
旁注: XML declaration,
<?xml version="1.0" encoding="UTF-8"?>
只能出现在序言中,看起来像 PI,不是 PI,不能通过 XPath 访问。
例如,
//processing-instruction('xml-stylesheet')
将return
type="text/xsl" href="https://foo.bar"
然后您可以使用字符串处理函数(根据 XPath 版本的不同而有所不同)来提取您需要的字符串片段。
例如,如果您只能使用 XPath 1.0,那么
"substring-before(
substring-after(//processing-instruction('xml-stylesheet')[1],
'href="'),
'"')"
(写成好像要嵌入到 XSLT 中 — 根据其他托管语言的需要修改引号)将 return
https://foo.bar
另见
- What is the XPath expression to select a Processing instruction?
- Can I use XPath to get the content of a processing instruction in XML?
我希望能够使用 XPath return 来自 XML 文件的 href="stylesheet.xsl"
值,但我似乎无法找出引用其中属性的表达式?-tags 如果可能的话。示例文件:
<?xml-stylesheet type="text/xsl" href="https://foo.bar" ?>
<test>
<test2>
</test2>
</test>
目标是找出给定 XML 文件引用的样式表,因此在本例中我想 return "https://foo.bar"
作为结果集。有什么想法吗?
您可以通过 processing-instruction()
节点测试在序言中访问 Processing Instruction (PI),就像在 XML 文档的其余部分访问它一样。
旁注: XML declaration,
<?xml version="1.0" encoding="UTF-8"?>
只能出现在序言中,看起来像 PI,不是 PI,不能通过 XPath 访问。
例如,
//processing-instruction('xml-stylesheet')
将return
type="text/xsl" href="https://foo.bar"
然后您可以使用字符串处理函数(根据 XPath 版本的不同而有所不同)来提取您需要的字符串片段。
例如,如果您只能使用 XPath 1.0,那么
"substring-before(
substring-after(//processing-instruction('xml-stylesheet')[1],
'href="'),
'"')"
(写成好像要嵌入到 XSLT 中 — 根据其他托管语言的需要修改引号)将 return
https://foo.bar
另见
- What is the XPath expression to select a Processing instruction?
- Can I use XPath to get the content of a processing instruction in XML?