【问题标题】:xpath selection on elements with namespaces具有命名空间的元素的 xpath 选择
【发布时间】:2010-10-16 08:51:27
【问题描述】:

这是一篇简单但有效的 Docbook 文章:

<?xml version="1.0" encoding="utf-8"?>
<article xmlns="http://docbook.org/ns/docbook" version="5.0">
<title>I Am Title</title>
<para>I am content.</para>
</article>

这是一个样式表,如果我删除上面的 xmlns 属性,则选择 title,如果我把它留在里面,则不会:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:apply-templates select="article"/>
    </xsl:template>
    <xsl:template match="article">
      <p><xsl:value-of select="title"/></p>
    </xsl:template>
    <xsl:template match="text()"/>
</xsl:stylesheet>

如果 XPath 具有命名空间属性,我如何让 XPath 选择 titlearticle

【问题讨论】:

标签: xslt xpath namespaces


【解决方案1】:

您需要为命名空间添加别名并在 XPath 中使用该别名

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:a="http://docbook.org/ns/docbook"
   exclude-result-prefixes="a"
   >
<xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:apply-templates select="a:article"/>
    </xsl:template>
    <xsl:template match="a:article">
      <p><xsl:value-of select="a:title"/></p>
    </xsl:template>
    <xsl:template match="text()"/>
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2010-09-11
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    相关资源
    最近更新 更多