XQuery评论中的奇怪行为

时间:2011-02-17 22:40:48

标签: xquery sedna

我有一组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/)



编辑:以下是我刚才写的另一个显示相同问题的查询。这似乎可以重现,但如果它是语法错误相关,它可能只是我再现语法错误。我也将保留旧的,所以这个问题的未来观众不会混淆。

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>

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

1 个答案:

答案 0 :(得分:1)

问题在于以下几行:

for $formName in /ns0:formName

在Sedna中(记得Sedna是数据库,而不是XQuery处理器)在查询执行之前无法定义默认上下文项(请参阅XQuery 1.0, 2.1.2 Dynamic Context)。因此,它不知道如何评估./ns0:formName(相当于简单/ns0:formName) - 它只是不知道.在这种情况下的含义。

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

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

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

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