2011-06-25 2 views
0

XQueryでTEXTCLASSからTEIにtypeswitchを作成しようとしています。非常に難しいプロセスではありませんが、面倒ですので、できるだけ多くのものを自動化しようとしています。インポートされたXQueryモジュールで変数が設定されていない理由(E:XPDY0002)を特定できません

私はOxygen 12とExist 1.4.1を使用しています。

今私はgen.xqmの関数を使って便利なダンディートランスを作成するtransform-tei.xqを実行します。 "convert-attr-default"(その目的はノードの属性を取り、名前/値を小文字に変換することを目的としている)を追加した後、transform-tei.xqは

"E [localhost] XPDY0002 :変数 '$ attr-name'が設定されていません[行58、列18] [行58、列18] "

理由を特定できません。 XQueryは私が自分自身で教えようとした最初の言語であり、O'Reillyの本は素晴らしいですが、それはまだ新しいタイプの学習です。

xquery version '1.0'; 

(: Module: transform-tei.xq 
    Date: 24 06 2011 
    Desc: Creates a module with functions to perform a basic transform to TEI on a specified xml document. 
:) 

declare option exist:serialize "method=text media-type=text/text"; 
import module namespace gen = "http://www.example.com/test/gen" at "xmldb:exist:///db/richmond/test-queries/gen.xqm"; 

let $doc := doc("/db/richmond/xml-for-typeswitch/wwa0005.0001.005.xml") 
let $tags := gen:tags($doc) 

let $config := 
<config> 
    <modulename>text-tei</modulename> 
    <namespace>http://www.example.com/test/text-tei</namespace> 
</config> 
return 
    gen:create-module($tags, $config) 

xquery version '1.0'; 

(: Module: gen.xqm 
    Note: Stolen/modified from http://en.wikibooks.org/wiki/XQuery/Generating_Skeleton_Typeswitch_Transformation_Modules#Generation_Function 
    Date: 24 06 2011 
    Desc: Provides functions to generate a list of all tags in a document and dynamically create a module to perform an identity transformation 
:) 

module namespace gen = "http://www.example.com/test/gen"; 

declare variable $gen:cr := "&#13;"; 

declare function gen:tags($docs as node()*) as xs:string * { 
    for $tag in distinct-values ($docs//*/name(.)) 
    order by $tag 
    return $tag 
}; 

declare function gen:create-module($tags as xs:string*, $config as element(config)) as element(module) { 
let $modulename := $config/modulename/text() 
let $prefix := $config/prefix/text() 
let $pre:= concat($modulename,":",$prefix) 
let $namespace := ($config/namespace,"http://mysite/module")[1]/text() 
return 
<module> 
module namespace {$modulename} = "{$namespace}"; 
(: conversion module generated from a set of tags 

:) 
<function> 
declare function {$pre}convert($nodes as node()*) as item()* {{ {$gen:cr} 
    for $node in $nodes 
    return 
    typeswitch ($node) 
     {for $tag in $tags 
     return 
      <s>case element({$tag}) return {$pre}{replace($tag,":","-")}($node) 
      </s> 
     } 
     default return 
     {$pre}convert-default($node) 
    }}; 
</function> 

<function> 
declare function {$pre}convert-default($node as node()) as item()* {{ {$gen:cr} 
    $node 
    }}; 
</function> 

<function> 
declare function {$pre}convert-attr-default($attr as attribute()*) as item()* {{ {$gen:cr} 
    for $upper-attr in $attr 
    let $attr-name := fn:node-name($upper-attr) 
    let $attr-val := fn:data($upper-attr) 

    return 
    attribute { $attr-name } { $attr-val } 
    }}; 
</function> 

{for $tag in $tags 
return 
    <function> 
declare function {$pre}{replace($tag,":","-")}($node as element({$tag})) as item()* {{ {$gen:cr} 
    element {lower-case($tag)} {{ 
    {$pre}convert-attr-default($node/@*), 
    {$pre}convert($node/node()) 
    }}{$gen:cr} 
}}; 
    </function> 
} 

</module> 
}; 

ありがとう!

答えて

0

一見すると、変数$attr-nameはその参照より3行上に定義されているようです。しかしながら、より詳細な見方は、明らかな定義が、構築されたfunction要素の文字どおりの要素内容にあることを明らかにする。その要素の内容の中から、単一の中かっこが式モードにエスケープされます。ここでは参照があります。

したがって、定義は生成されたクエリにあり、参照は生成クエリにあります。両方を同じスコープに入れるには、中括弧を適切に配置する必要があります。生成された関数を開くには、2つの中カッコがあることに注意してください。これは、要素の中身に1つの中括弧が現れるという表記法です。

関連する問題