选择处理指令的 XPath 表达式是什么? [英] What is the XPath expression to select a Processing instruction?

查看:22
本文介绍了选择处理指令的 XPath 表达式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 XML 中使用 xsl:stylesheet 处理指令.无论如何可以使用 XPath 选择这个 PI 吗?如果是这样怎么办?

I'm using xsl:stylesheet Processing Instruction in my XML. Is there anyway to select this PI using XPath ? If so how ?

推荐答案

一般来说,可以使用 processing-instruction() 节点测试来选择处理指令.

In general, a processing instruction can be selected using the processing-instruction() node test.

更具体地说,可以将所需 PI 节点的名称(目标)作为参数包含在内.

More specifically, one can include as argument the name (target) of the wanted PI node.

使用:

/processing-instruction('xml-stylesheet')

这将选择在全局级别定义的名称为 xsl-stylesheet 的任何处理指令(顶部元素的同级).

This selects any processing instruction with name xsl-stylesheet that is defined at the global level (is sibling of the top element).

请注意,xsl:stylesheet 是 PI 的无效 PI 目标.冒号 ':' 用于从本地名称分隔命名空间前缀——但是处理指令 target 不能属于命名空间.根据 W3c XPath 规范:

Do note that xsl:stylesheet is an invalid PI target for a PI. A colon ':' is used to delimit a namespace prefix from the local name -- however a processing instruction target cannot belong to a namespace. As per the W3c XPath Specification:

"处理指令有一个扩展名:本地部分是处理指令的目标;命名空间 URI 为空."

也根据 W3C 文档:"Associating Style Sheets with XML documents 1.0",将样式表关联到 XML 文档的 PI 的目标必须是:"xml-stylesheet" -- 而不是 "xsl:stylesheet""xsl-stylesheet" .

Also according to the W3C document: "Associating Style Sheets with XML documents 1.0", the target of the PI that associates a stylesheet to an XML document must be: "xml-stylesheet" -- not "xsl:stylesheet" or "xsl-stylesheet" .

这是一个完整的例子:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
     <xsl:copy-of select="/processing-instruction('xml-stylesheet')"/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于以下 XML 文档时:

<?xml-stylesheet type="text/xsl" href="test"?>
<Books>
    <Book name="MyBook" />
</Books>

计算 XPath 表达式并输出选定的 PI 节点:

<?xml-stylesheet type="text/xsl" href="test"?>

这篇关于选择处理指令的 XPath 表达式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆