XQuery/命名空间构造函数
外观
< XQuery
您想在文档中动态创建命名空间。
使用 XQuery 命名空间构造函数。它的格式与元素和属性构造函数非常相似
namespace {prefix} {uri}
declare function local:usage-meta($new-collection as xs:string, $resource as xs:string)
{
let $schema := doc($new-collection || '/' || $resource)//xs:schema
let $targetNamespace := $schema/@targetNamespace/string()
let $metaNamespace := $targetNamespace || '-META'
let $named := $schema/xs:element/@name/string()
let $referenced := $schema//xs:element/@ref/string()
let $top := distinct-values($named[not(.=$referenced)])
let $newSchema :=
element xs:schema {
namespace { "xs" } { "http://www.w3.org/2001/XMLSchema" },
namespace { "" } { $metaNamespace },
namespace { "usage" } { $targetNamespace },
namespace { "vc" } { "http://www.w3.org/2007/XMLSchema-versioning" },
attribute { "targetNamespace" } { $metaNamespace },
attribute { "elementFormDefault" } { "unqualified" },
attribute { "attributeFormDefault" } { "unqualified" },
attribute { "vc:minVersion" } { "1.0" },
attribute { "vc:maxVersion" } { "1.01" },
element xs:import {
attribute { "namespace" } { $targetNamespace },
attribute { "schemaLocation" } { $resource }
},
element xs:element {
attribute { "name" } { "META" },
element xs:complexType {
element xs:choice {
attribute { "minOccurs" } { "0" },
attribute { "maxOccurs" } { "unbounded" },
for $topElement in $top
return
element xs:element {
attribute { "ref" } { "usage:" || $topElement }
}
}
}
}
}
(:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="{$metaNamespace}"
xmlns:usage="{$targetNamespace}"
xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
targetNamespace="{$metaNamespace}"
elementFormDefault="unqualified"
attributeFormDefault="unqualified"
vc:minVersion="1.0" vc:maxVersion="1.01">
<xs:import namespace="{$targetNamespace}" schemaLocation="{$resource}"/>
<xs:element name="META">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
{for $topElement in $top
return <xs:element ref="usage:{$topElement}"/>
}
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
:)
return xmldb:store($new-collection, 'META_' || $resource, $newSchema)
};
此示例由 Loren Cahlandar 于 2013 年 12 月贡献