I left out an important part about how to get the phone value when the field
name is unknown.

Here's is the entire code snippet with some code to display the phone value
using the field name provided by the SPARQL query.

(:------------------------:)

import module namespace sem = "http://marklogic.com/semantics"; at
"/MarkLogic/semantics.xqy";

declare namespace aptp = "http://example.com/aptp";;
declare variable $NS  := "http://example.com/aptp";;

let $results :=
  sem:query-results-serialize(sem:sparql('
    PREFIX dom: <http://example.com/products/>
    PREFIX owl: <http://www.w3.org/2002/07/owl#>
    
    SELECT ?fields
    FROM <http://example.com/field-mappings-1>
    WHERE
    {
      dom:phone owl:equivalentProperty ?fields .
    }',
    (), (), ()
  ))

let $fields :=
$results/*:results/*:result/*:binding[@name="fields"]/*:literal/text()

let $source := "SystemB"
let $phoneNumber := "11-301-555-*"

let $elementValueQueryOrQuery :=
  cts:or-query((
    for $field in $fields
      return
        cts:element-value-query(fn:QName($NS, $field), $phoneNumber)
  ))

let $query := cts:and-query((
                  cts:collection-query(("party")),
                  cts:element-value-query(fn:QName($NS, "Source"), $source),
                  $elementValueQueryOrQuery
                ))

let $results      := cts:search(fn:doc(), $query)

let $result       := $results[1]/aptp:Party/aptp:Person
let $resultFields := xs:string($result/*/fn:node-name(.))

 (:
   Get the "phone" field name that was in the result and use it to get the
phone value.

   The next line returns the intersection of fields list and resultFields
list.
   This will have the phone field name that can be used to get the phone
value.
:)

let $nodeFieldQName := fn:QName($NS, ($fields[.=$resultFields]))[1]

let $phoneValue     := $result/*[node-name(.) eq $nodeFieldQName]/text()

return
  $result/aptp:FullName/text()||": "||$phoneValue


(:------------------------:)



-----Original Message-----
From: Gary Russo [mailto:garyru...@hotmail.com] 
Sent: Tuesday, April 12, 2016 10:15 PM
To: 'MarkLogic Developer Discussion' <general@developer.marklogic.com>
Subject: RE: [MarkLogic Dev General] What SPARQL Query to use to retrieve
phone numbers from documents that have various phone field names?

Thanks Ed

I'm currently following Beverly's suggestion which is owl:equivalentProperty
but I'm going to experiment a bit.

Using rdfs:subClassOf looks good too.

I wonder if using triples to do field mappings is considered a best
practice.

I believe it is because you can load data "as is" but the tradeoff may be a
performance hit from having the 2-part query.



-----Original Message-----
From: general-boun...@developer.marklogic.com
[mailto:general-boun...@developer.marklogic.com] On Behalf Of Ed Outhwaite
Sent: Monday, April 11, 2016 4:03 PM
To: MarkLogic Developer Discussion
Subject: Re: [MarkLogic Dev General] What SPARQL Query to use to retrieve
phone numbers from documents that have various phone field names?

Hi Gary,

If Beverley's response doesn't quite hit the spot for you, I'd generally go
with subclassing:

PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX fibo: <http://example.com/fibo/>
PREFIX dom:  <http://example.com/products/> PREFIX rdfs:
<http://www.w3.org/2000/01/rdf-schema#>

INSERT DATA
{
  GRAPH <http://example.com/field-mappings-1>
  {
    fibo:FullPhoneNumber rdfs:subClassOf dom:phone .
    fibo:CUPHONE         rdfs:subClassOf dom:phone .
    fibo:Telephone       rdfs:subClassOf dom:phone .
    fibo:TEL-TYPE-1      rdfs:subClassOf dom:phone .
  }
}



Then to query, either use the subClassOf.rules with inferencing { ?who
dom:phone ?number } , or:

PREFIX dom: <http://example.com/products/> PREFIX rdfs:
<http://www.w3.org/2000/01/rdf-schema#>
SELECT ?who ?number
FROM <http://example.com/field-mappings-1>
WHERE
{
  ?who ?link ?number .
  ?link rdfs:subClassOf dom:phone .
}



Regards,

  Ed



On 4/9/16, 10:28 PM, "Gary Russo" <garyru...@hotmail.com> wrote:

>What Combination SPARQL Query should be used to query phone numbers 
>from documents where field names vary?
>
>I'd like to used triples to do the field mapping.
>
>The following triples are may be used to map the various phone number 
>fields to dom:phone.
>
>PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
>PREFIX fibo: <http://example.com/fibo/> PREFIX dom:  
><http://example.com/products/>
>
>INSERT DATA
>{
>  GRAPH <http://example.com/field-mappings-1>
>  {
>    fibo:FullPhoneNumber rdf:type dom:phone .
>    fibo:CUPHONE         rdf:type dom:phone .
>    fibo:Telephone       rdf:type dom:phone .
>    fibo:TEL-TYPE-1      rdf:type dom:phone .
>  }
>}
>
>
>Here are the 3 XML documents that use different field names for a 
>telephone number.
>
>/party/1001.xml
>
><fibo:Party xmlns:aptp="http://example.com/fibo";>
>    <fibo:Source>SystemA</fibo:Source>
>    <fibo:Customer>Xerox</fibo:Customer>
>    <fibo:Created>2016-02-26</fibo:Created>
>    <fibo:LastModified>2016-02-26</fibo:LastModified>
>    <fibo:Person>
>        <fibo:FullName>Grace Hopper</fibo:FullName>
>        <fibo:FullPhoneNumber>11-301-555-1212</fibo:FullPhoneNumber>
>        <fibo:city>Sunnyvale</fibo:city>
>        <fibo:state>California</fibo:state>
>    </fibo:Person>
></fibo:Party>
>
>
>/party/1002.xml
>
><fibo:Party xmlns:aptp="http://example.com/fibo";>
>    <fibo:Source>SystemB</fibo:Source>
>    <fibo:Customer>Apple</fibo:Customer>
>    <fibo:Created>2016-02-26</fibo:Created>
>    <fibo:LastModified>2016-02-26</fibo:LastModified>
>    <fibo:Person>
>        <fibo:FullName>Marissa Mayer</fibo:FullName>
>        <fibo:CUPHONE>11-301-555-4444</fibo:CUPHONE>
>        <fibo:city>Los Angeles</fibo:city>
>        <fibo:state>California</fibo:state>
>    </fibo:Person>
></fibo:Party>
>
>
>/party/1003.xml
>
><fibo:Party xmlns:aptp="http://example.com/fibo";>
>    <fibo:Source>SystemC</fibo:Source>
>    <fibo:Customer>Microsoft</fibo:Customer>
>    <fibo:Created>2016-02-26</fibo:Created>
>    <fibo:LastModified>2016-02-26</fibo:LastModified>
>    <fibo:Person>
>        <fibo:FullName>Sheryl Sandberg</fibo:FullName>
>        <fibo:Telephone type="1">301-555-1212</fibo:Telephone>
>        <fibo:city>Redmond</fibo:city>
>        <fibo:state>Washington</fibo:state>
>    </fibo:Person>
></fibo:Party>
>
>
>What query should be used to retrieve the phone numbers for all 3 
>documents using the field mapping triples noted above?
>
>Any thoughts/advice on this is much appreciated.
>
>Regards,
>Gary Russo
>
>
>
>_______________________________________________
>General mailing list
>General@developer.marklogic.com
>Manage your subscription at:
>http://developer.marklogic.com/mailman/listinfo/general

_______________________________________________
General mailing list
General@developer.marklogic.com
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general

_______________________________________________
General mailing list
General@developer.marklogic.com
Manage your subscription at: 
http://developer.marklogic.com/mailman/listinfo/general

Reply via email to