Validate empty tags using xquery
JpNov 3 2010 — edited Nov 3 2010Hi All,
I have a question regarding xquery flwor exrpession. My scenarion is I need to validate an xml input file wether one of its elements is empty so I created an xquery to validate. However, I receive an error when I try to make an input file that has a repeating element.
Below is my xquery for validating empty tags:
declare namespace cred="http://www.pldt.com.ph/eai/service/component/CreditService/";
declare namespace com="http://www.pldt.com.ph/eai/Common";
declare variable $inputdata as element(cred:PushQuote) external;
let $QuoteNumber := data($inputdata/PushQuote/com:QuoteNumber)
let $AccountNumber := data($inputdata/PushQuote/com:CustomerAccountNumber)
let $QuoteLineItemList := $inputdata/PushQuote/com:LineItemList
for $data in $inputdata
return
if (fn:string-length($QuoteNumber) > 0 and
fn:string-length($AccountNumber) > 0)
then
for $quoteLineItem in $QuoteLineItemList
return
if (fn:string-length(data($quoteLineItem/com:QuoteLineItem/com:LineNumber)) > 0 and
fn:string-length(data($quoteLineItem/com:QuoteLineItem/com:ProductCatalogId)) > 0 and
fn:string-length(data($quoteLineItem/com:QuoteLineItem/com:Product)) > 0 and
fn:string-length(data($quoteLineItem/com:QuoteLineItem/com:Quantity)) > 0 and
fn:string-length(data($quoteLineItem/com:QuoteLineItem/com:Mrc)) > 0 and
fn:string-length(data($quoteLineItem/com:QuoteLineItem/com:Currency)) > 0 and
fn:string-length(data($quoteLineItem/com:QuoteLineItem/com:LastUpdateDate)) > 0 and
fn:string-length(data($quoteLineItem/com:QuoteLineItem/com:LastUpdatedBy)))
then
"0"
else
"1"
else
"1"
Below is my input xml document:
<cred:PushQuote xmlns:com="http://www.pldt.com.ph/eai/Common" xmlns:cred="http://www.pldt.com.ph/eai/service/component/CreditService/">
<PushQuote>
<com:QuoteNumber>ZXCV-233</com:QuoteNumber>
<com:CustomerAccountNumber>1213654889</com:CustomerAccountNumber>
<!--Optional:-->
<com:LineItemList>
*<!--1 or more repetitions:-->*
<com:QuoteLineItem>
<com:LineNumber>3</com:LineNumber>
<com:ProductCatalogId>string</com:ProductCatalogId>
<com:Product>asdf</com:Product>
<com:Quantity>string</com:Quantity>
<com:Mrc>3</com:Mrc>
<com:Currency>USD</com:Currency>
<com:LastUpdateDate>string</com:LastUpdateDate>
<com:LastUpdatedBy>string</com:LastUpdatedBy>
</com:QuoteLineItem>
<com:QuoteLineItem>
<com:LineNumber>3</com:LineNumber>
<com:ProductCatalogId>string</com:ProductCatalogId>
<com:Product>1234</com:Product>
<com:Quantity>string</com:Quantity>
<com:Mrc>3</com:Mrc>
<com:Currency>USD</com:Currency>
<com:LastUpdateDate>string</com:LastUpdateDate>
<com:LastUpdatedBy>string</com:LastUpdatedBy>
</com:QuoteLineItem>
<com:QuoteLineItem>
<com:LineNumber>3</com:LineNumber>
<com:ProductCatalogId>string</com:ProductCatalogId>
<com:Product></com:Product>
<com:Quantity>string</com:Quantity>
<com:Mrc>3</com:Mrc>
<com:Currency>USD</com:Currency>
<com:LastUpdateDate>string</com:LastUpdateDate>
<com:LastUpdatedBy>string</com:LastUpdatedBy>
</com:QuoteLineItem>
</com:LineItemList>
</PushQuote>
</cred:PushQuote>