【问题标题】:schema-attribute throwing in-scope attribute declaration error模式属性抛出范围内属性声明错误
【发布时间】:2014-09-09 05:48:19
【问题描述】:

我正在使用 XSLT 2.0 http://www.w3.org/TR/xslt20。我的 XSL 文件中有一个声明:

   <xsl:template match="schema-attribute(attr)">
            <match>schema-attribute(attr):</match>
    </xsl:template>

我正在尝试将模式属性与 attr 匹配。我的 xml 文档中有多个 xml 元素,属性为 attr。

xml 文档:

<?xml version="1.0" encoding="UTF-8" ?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noschemaLocation="http://www.w3.org/2001/XMLSchema-instance">

  <e1>def</AAA>
  <e1 attr="1">there</AAA>
  <e1 attr="2">hey</AAA>
</test>

架构

<?xml version="1.0" encoding="UTF-8" ?>

<x:schema xmlns:x="http://www.w3.org/2001/XMLSchema">

    <x:element name="test" />

    <x:element name="e1" type="etype" />
    <x:complexType name="etype">
            <x:simpleContent>
                <x:extension base="x:string">
                    <x:attribute name="attr" type="x:string" use="optional"></x:attribute>
                </x:extension>
            </x:simpleContent>
    </x:complexType>

xsl:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0" >
<xsl:template match="schema-attribute(attr)">
            <match>matched</match>
    </xsl:template>

</xsl:stylesheet>

我收到以下错误:

序列类型表达式引用未在范围内属性声明中定义的架构属性“attr”。

我不知道错误的原因及其含义。我试图谷歌但没有找到解决方案。同样, attr 也是 xml 中的一个属性。

  • 在此特定上下文中,范围内的属性声明意味着什么?
  • 另外,如何使用上面的 xml 文件使用架构属性。

【问题讨论】:

  • 您的 xml 仍然没有意义。 xsi:schemaLocation 应该是命名空间/模式对的序列。由于您的文档不使用命名空间,因此您可能需要xsi:noNamespaceSchemaLocation
  • 好的,我做了这些更改。但是如何在这个 xml 文档中使用模式属性?
  • 您是否只是想弄清楚如何匹配attr 属性?如果是这样,您的模板的匹配表达式将是 &lt;xsl:template match="@attr"&gt;

标签: xml xslt xsd schema xslt-2.0


【解决方案1】:

如果我可以尝试比 Michael Sperberg-McQueen 更简短的回答:schema-attribute() 测试很少有用。它们仅匹配已针对模式中的全局(顶级)属性声明验证的属性,并且很少有模式实际上使用全局属性声明。大多数和你一样,在本地声明中定义属性。

错误消息意味着您的架构没有名为“attr”的全局属性声明。

您问:“另外,我如何使用上面的 xml 文件使用 schema-attribute()。”。这不是一个好问题。 schema-attribute() 可以解决的问题很少。我怀疑你的问题不是其中之一。告诉我们您要做什么,我们可以告诉您如何解决它。选择工具来匹配问题,而不是问题来匹配工具。

【讨论】:

  • 您能告诉我 1 个问题,如您所说,架构属性可能有用吗?只有少数情况下有用?
  • 我无法从规范中理解什么是架构属性?如果你能至少告诉我这意味着什么?那么我可能会提出与我的场景匹配的更好的问题
  • schema-attribute(A) 匹配一个属性节点,如果 (1) 它被命名为“A”,(2) 在名为“A”的模式中有一个全局属性声明,(3)属性节点已针对模式进行了验证,并且 (4) 属性节点的类型与全局属性声明中定义的类型一致。
  • 关于你的第一个问题,我不认为我曾经在愤怒中使用过 schema-attribute(),但理论上它对于处理全局属性(如 xml:space)很有用,其中模板带有 match="schema-attribute(xml:space)" 的规则不会匹配任何名为 xml:space 的属性,而只会匹配一个已针对 xml:space 的模式定义进行模式验证的属性。
猜你喜欢
  • 2019-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多