【问题标题】:Cannot apply XSLT template to xml element无法将 XSLT 模板应用于 xml 元素
【发布时间】:2013-08-06 12:36:03
【问题描述】:

我有以下 XML:

<?xml version="1.0"?>
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml" id="new" cnxml-version="0.7" module-id="new">

<metadata xmlns:md="http://cnx.rice.edu/mdml"
      mdml-version="0.5">

    <md:abstract>By the end of this section, you will be able to:
        <list>
            <item>Discuss the role of homeostasis in healthy functioning</item>
            <item>Contrast negative and positive feedback, giving one physiologic example of each mechanism</item>
        </list>
    </md:abstract>

我需要将模板应用到 md:abstract 元素。我有以下 XSL 代码:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:md="http://cnx.rice.edu/mdml/0.4">

<xsl:output omit-xml-declaration="yes" encoding="ASCII"/>

<xsl:template match="c:document">
<body>
    <xsl:apply-templates select="c:metadata"/>
    <xsl:apply-templates select="c:content"/>
</body>
</xsl:template>

<xsl:template match="c:metadata">
    <xsl:apply-templates select="md:abstract"/>
</xsl:template>

<xsl:template match="md:abstract">
    <xsl:apply-templates select="@*|node()"/>
</xsl:template>

但是,不幸的是,它没有捕获 md:abstract 元素。我想不通,想要是错的。

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    在您的文档中您有xmlns:md="http://cnx.rice.edu/mdml",而在XSLT 中您有xmlns:md="http://cnx.rice.edu/mdml/0.4"。这些不一样。命名空间必须在词法上相同才能匹配(没有别名或超类)。

    我自己也经历过这个,试图管理版本化的命名空间,这是一场噩梦。要么像您的示例那样失败,要么您最终明确检查所有版本。如果它是您的命名空间,请删除该版本。

    如果您正在设计它,您总是可以将版本放在单独的属性中(就像 XSLT 转换所做的那样)。

    【讨论】:

    • 非常感谢,我真的错过了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 2021-08-12
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 2017-08-23
    • 2016-07-18
    相关资源
    最近更新 更多