0

我有一组 XQuery 转换,我正在对存储在 Sedna 数据库中的文件运行这些转换。它们都具有大致以下格式:

declare namespace ns0 = "http://www.someuri.com/foo.xsd";
declare namespace ns1 = "http://www.someuri.com/bar.xsd";

(:Declare a few functions like the following:)
declare function local:to-xs-boolean(
    $string as xs:string?
)
as xs:boolean? {
    if (fn:upper-case($string) = 'Y') then
        xs:boolean('true')
    else
        if (fn:upper-case($string) = 'N') then
            xs:boolean('false')
        (:if it isn't Y or N then attempt a normal cast - this will fail if it
        it isn't any of 'true', 'false', 1, or 0 :)
        else
            if ($string != '') then
                xs:boolean($string)
            else
                ()
        (:endif:)
    (:endif:)
};

(:Omitted several other functions:)

(:Start the main program:)

(: { :)
for $formName in /ns0:formName
return
        <ns1:newFormName>
            {
                let $address := $formName/ns0:Address
                return
                    <NewAddress>{
                        (:Omitted code and elements unrelated to this question:)
                    }</NewAddress>
            }
            (:Omitted code and elements unrelated to this question:)
        </ns1:newFormName>

现在这是我的问题。你看到'for'正上方的那条线,上面写着'(:{:)'?它应该是一条评论,但由于某种原因,它对我的​​查询的正确功能至关重要。如果我完全删除它(或从评论中删除'{'),我会得到

SEDNA Message: ERROR XPDY0002
It is a dynamic error if evaluation of an expression relies on some part of the dynamic context that has not been assigned a value.

如果我取消注释它(所以该行只是读取'{')我得到

SEDNA Message: ERROR XPST0003
It is a static error if an expression is not a valid instance of the grammar defined in A.1 EBNF.
Details: at (393:1), syntax error, unexpected {
         at (798:33), syntax error, unexpected end of file, expecting "," or }

如果我同时取消注释并在文件末尾添加匹配的“}”,我会得到

SEDNA Message: ERROR XPST0003
It is a static error if an expression is not a valid instance of the grammar defined in A.1 EBNF.
Details: at (393:1), syntax error, unexpected {

如果我在该评论中添加其他文本(如'(:text foo bar baz()blah {:)'),只要我将'{'留在那里,它就会继续工作。

有没有人以前见过这种情况或知道可能是什么原因造成的?这并不是一个真正的关键问题(我可以确保在我的所有转换中都有'(:{:)'),但这真的让我很好奇。因此,感谢您的帮助,甚至只是让我感到困惑。

哦,另一个快速说明 - 我不知道它是否有任何区别,但我正在使用 Charles Foster 的 Sedna API (http://www.cfoster.net/sedna/) 从 Java 中运行查询



编辑:这是我刚刚写的一个不同的查询,它显示了同样的问题。这对我来说似乎可以重现,但如果它与语法错误相关,它可能只是我重现了语法错误。我也会留下旧的,所以这个问题的未来观众不会感到困惑。

declare namespace ns0 = "http://www.someuri.com/InitialSchema.xsd";
declare namespace ns1 = "http://www.someuri.com/FinalSchema.xsd";

declare function local:to-address(
    $recipient as xs:string?,
    $line1 as xs:string?,
    $line2 as xs:string?,
    $line3 as xs:string?,
    $city as xs:string?,
    $provinceOrState as xs:string?,
    $country as xs:string?,
    $postalOrZIP as xs:string?
)
as element() {
    if (fn:upper-case($country) = 'CA' or fn:upper-case($country) = 'US') then
        if (fn:upper-case($country) = 'CA') then
            <CanadianAddress>
                <City>{ $city }</City>
                <Country>{ $country }</Country>
                <PostalCode>{ $postalOrZIP }</PostalCode>
                <Province>{ $provinceOrState }</Province>
                <Recipient>{ fn:normalize-space($recipient) }</Recipient>
                <StreetAddress>{ $line1 }</StreetAddress>
                <StreetAddress>{ $line2 }</StreetAddress>
                <StreetAddress>{ $line3 }</StreetAddress>
                <StreetAddress/>
            </CanadianAddress>
        else
            <USAddress>
                <City>{ $city }</City>
                <Country>{ $country }</Country>
                <ZipCode>{ $postalOrZIP }</ZipCode>
                <State>{ $provinceOrState }</State>
                <Recipient>{ fn:normalize-space($recipient) }</Recipient>
                <StreetAddress>{ $line1 }</StreetAddress>
                <StreetAddress>{ $line2 }</StreetAddress>
                <StreetAddress>{ $line3 }</StreetAddress>
            </USAddress>        
        (:endif:)
    else
        if ($country != '') then        
            <InternationalAddress>
                <City>{ $city }</City>
                <Country>{ $country }</Country>
                <PostalCode>{ $postalOrZIP }</PostalCode>
                <Recipient>{ fn:normalize-space($recipient) }</Recipient>
                <StreetAddress>{ $line1 }</StreetAddress>
                <StreetAddress>{ $line2 }</StreetAddress>
                <StreetAddress>{ $line3 }</StreetAddress>
            </InternationalAddress>
        else
            <CanadianAddress>
                <City>{ $city }</City>
                <Country>{ $country }</Country>
                <PostalCode>{ $postalOrZIP }</PostalCode>
                <Province>{ $provinceOrState }</Province>
                <Recipient>{ fn:normalize-space($recipient) }</Recipient>
                <StreetAddress>{ $line1 }</StreetAddress>
                <StreetAddress>{ $line2 }</StreetAddress>
                <StreetAddress>{ $line3 }</StreetAddress>
                <StreetAddress/>
            </CanadianAddress>
        (:endif:)
    (:endif:)
};


(:{:)
for $addressForm1 in /ns0:AddressForm
let $token := xs:integer(data($addressForm1/ns0:submissionID))
return
        <ns1:NewAddressForm>
            <SubmissionID>{ data($addressForm1/ns0:submissionID) }</SubmissionID>
            {
                let $currentAddress := $addressForm1/ns0:currentAddress
                return
                    <NewAddress>{
                        local:to-address(
                            concat(
                                $addressForm1/ns0:fullName/ns0:firstName,
                                ' ',
                                substring(data($addressForm1/ns0:fullName/ns0:middleName), 1, 1),
                                ' ',
                                $addressForm1/ns0:fullName/ns0:lastName
                            ),
                            data($currentAddress/ns0:line1),
                            data($currentAddress/ns0:line2),
                            data($currentAddress/ns0:line3),
                            data($currentAddress/ns0:city),
                            data($currentAddress/ns0:provinceOrState),
                            data($currentAddress/ns0:country),
                            data($currentAddress/ns0:postalOrZipCode)
                        )
                    }</NewAddress>
            }
        </ns1:NewAddressForm>

这是新查询的一些示例数据

<?xml version="1.0"?>
<ns0:AddressForm xmlns:ns0="http://www.someuri.com/InitialSchema.xsd">
    <ns0:submissionID>23774</ns0:submissionID>
    <ns0:fullName>
        <ns0:firstName>First</ns0:firstName>
        <ns0:middleName>Middle</ns0:middleName>
        <ns0:lastName>Last</ns0:lastName>
    </ns0:fullName>
    <ns0:currentAddress>
        <ns0:line1>Line 1</ns0:line1>
        <ns0:line2>Line 2</ns0:line2>
        <ns0:line3>Line 3</ns0:line3>
        <ns0:city>City</ns0:city>
        <ns0:provinceOrState>Province</ns0:provinceOrState>
        <ns0:postalOrZipCode>H0H 0H0</ns0:postalOrZipCode>
        <ns0:country>CA</ns0:country>
    </ns0:currentAddress>
</ns0:AddressForm>

如果这是一个语法错误,那么有人会向我指出它在哪一行吗?

4

1 回答 1

1

问题是以下行:

for $formName in /ns0:formName

在 Sedna(请记住 Sedna 是数据库,而不是 XQuery 处理器)中,无法在查询执行之前定义默认上下文项(请参阅XQuery 1.0、2.1.2 动态上下文)。所以它不知道如何评估./ns0:formName(相当于 simple /ns0:formName) - 它只是不知道.在这种情况下意味着什么。

您应该将要处理的文档加载到数据库中,然后使用doc()以下函数访问它:

for $formName in doc('forms')/ns0:formName

据我所知,您还可以尝试Charles Fosters 的Sedna 的 XQJ API(它应该比 XML:DB API 更好),它支持定义上下文项。

顺便说一句,如果您对 Sedna 的 XML:DB 或 XQJ 有任何疑问,最好直接在 sedna-discussion 列表中询问。查尔斯很有可能会回答你。

于 2011-02-19T11:54:04.133 回答