<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.art-decor.org/index.php?action=history&amp;feed=atom&amp;title=Update_snomed_display_names_xquery</id>
	<title>Update snomed display names xquery - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.art-decor.org/index.php?action=history&amp;feed=atom&amp;title=Update_snomed_display_names_xquery"/>
	<link rel="alternate" type="text/html" href="https://wiki.art-decor.org/index.php?title=Update_snomed_display_names_xquery&amp;action=history"/>
	<updated>2026-05-20T06:20:25Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.0</generator>
	<entry>
		<id>https://wiki.art-decor.org/index.php?title=Update_snomed_display_names_xquery&amp;diff=7328&amp;oldid=prev</id>
		<title>Ahenket: Created page with &quot;For updating displayName attributes of terminologyAssociations, valueSet concepts and template vocabulary items with the latest available description of a particular type from...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.art-decor.org/index.php?title=Update_snomed_display_names_xquery&amp;diff=7328&amp;oldid=prev"/>
		<updated>2020-06-17T06:39:10Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;For updating displayName attributes of terminologyAssociations, valueSet concepts and template vocabulary items with the latest available description of a particular type from...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;For updating displayName attributes of terminologyAssociations, valueSet concepts and template vocabulary items with the latest available description of a particular type from SNOMED CT (or country extension). This is useful because SNOMED CT updates in a different pace from projects and especially translations could occur after initial project setup. Operates on 1 project at a time.&lt;br /&gt;
&lt;br /&gt;
Prerequisite: have SNOMED CT data installed at your site&lt;br /&gt;
&lt;br /&gt;
Note: &amp;#039;&amp;#039;&amp;#039;once applied to your project, there is no undo&amp;#039;&amp;#039;&amp;#039; unless you keep a copy of the project as backup.&lt;br /&gt;
&lt;br /&gt;
This xquery will perform the following actions:&lt;br /&gt;
* Based on all elements with @code, @codeSystem = SNOMED CT, and @displayName pick the currently available fsn from the requested refset &amp;lt;code&amp;gt;$refsetlanguage&amp;lt;/code&amp;gt;, and the US English fallback fsn&lt;br /&gt;
* If applying the &amp;lt;code&amp;gt;$update&amp;lt;/code&amp;gt; is requested apply update, and leave a version statement in the project part&lt;br /&gt;
* Return a report of what was done&lt;br /&gt;
&lt;br /&gt;
To run the xquery below on the server:&lt;br /&gt;
{{:run xquery}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xml&amp;quot; enclose=&amp;quot;div&amp;quot;&amp;gt;&lt;br /&gt;
import module namespace get         = &amp;quot;http://art-decor.org/ns/art-decor-settings&amp;quot; at &amp;quot;/db/apps/art/modules/art-decor-settings.xqm&amp;quot;;&lt;br /&gt;
import module namespace art         = &amp;quot;http://art-decor.org/ns/art&amp;quot; at &amp;quot;/db/apps/art/modules/art-decor.xqm&amp;quot;;&lt;br /&gt;
import module namespace snomed      = &amp;quot;http://art-decor.org/ns/terminology/snomed&amp;quot; at &amp;quot;/db/apps/terminology/snomed/api/api-snomed.xqm&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
declare variable $codeSystemSNOMED      := &amp;#039;2.16.840.1.113883.6.96&amp;#039;;&lt;br /&gt;
(:31000146106 - Nederlandse taalreferentieset (foundation metadata concept):)&lt;br /&gt;
declare variable $refsetlanguage        := &amp;#039;31000146106&amp;#039;;&lt;br /&gt;
(:900000000000509007 - United States of America English language reference set:)&lt;br /&gt;
declare variable $refsetlanguage-us     := &amp;#039;900000000000509007&amp;#039;;&lt;br /&gt;
&lt;br /&gt;
declare function local:getDisplayNameAndStatus($code as xs:string, $codeSystem as xs:string, $defaultLanguage as xs:string) as element()* {&lt;br /&gt;
    if ($codeSystem = $codeSystemSNOMED) then&lt;br /&gt;
        (:let $service-uri        := concat($artDeepLinkTerminology,&amp;#039;snomed/getConcept/&amp;#039;,encode-for-uri($code))&lt;br /&gt;
        let $server-response    := httpclient:get(xs:anyURI($service-uri),false(),$requestHeaders)&lt;br /&gt;
        &lt;br /&gt;
        let $concept        := $server-response/httpclient:body//concept[@conceptId = $code]:)&lt;br /&gt;
        let $concept        := $snomed:colDataBase//@conceptId[. = $code]/parent::concept&lt;br /&gt;
        let $displayNames   := $concept/desc[@active]&lt;br /&gt;
        let $fsn            := $concept/desc[@type = &amp;#039;fsn&amp;#039;][@active][@languageRefsetId = $refsetlanguage-us]&lt;br /&gt;
        let $langfsn        := ($concept/desc[@type = &amp;#039;fsn&amp;#039;][@active][@languageRefsetId = $refsetlanguage], $concept/desc[@type = &amp;#039;pref&amp;#039;][@active][@languageRefsetId = $refsetlanguage])[1]&lt;br /&gt;
        let $statusCode     := if ($concept[@active]) then &amp;#039;active&amp;#039; else if ($concept) then &amp;#039;deprecated&amp;#039; else (&amp;#039;&amp;#039;)&lt;br /&gt;
        let $statusText     := $statusCode&lt;br /&gt;
        return&lt;br /&gt;
            &amp;lt;concept code=&amp;quot;{$code}&amp;quot; codeSystem=&amp;quot;{$codeSystem}&amp;quot; displayName=&amp;quot;{$fsn}&amp;quot; statusCode=&amp;quot;{$statusCode}&amp;quot; statusText=&amp;quot;{$statusText}&amp;quot;&amp;gt;&lt;br /&gt;
            {&lt;br /&gt;
                if (empty($langfsn)) then () else attribute langfsn {$langfsn}&lt;br /&gt;
            }&lt;br /&gt;
            &amp;lt;/concept&amp;gt;&lt;br /&gt;
    else ()&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
declare function local:getReturnLines($terms as element()*, $namemap as item()) as element()* {&lt;br /&gt;
    for $term in $terms[not(ancestor::valueSet)]&lt;br /&gt;
    let $replacementTerm    := map:get($namemap, $term/@code)/@langfsn&lt;br /&gt;
    return&lt;br /&gt;
        element {name($term)} {&lt;br /&gt;
            $term/@*,&lt;br /&gt;
            if (empty($replacementTerm)) then () else attribute replaceBy {$replacementTerm},&lt;br /&gt;
            art:getConcept($term/@conceptId, $term/@conceptFlexibility)&lt;br /&gt;
        }&lt;br /&gt;
    ,&lt;br /&gt;
    for $termsByVS in $terms[ancestor::valueSet]&lt;br /&gt;
    let $vsid   := $termsByVS/ancestor::valueSet/@id&lt;br /&gt;
    let $vsed   := $termsByVS/ancestor::valueSet/@effectiveDate&lt;br /&gt;
    group by $vsid, $vsed&lt;br /&gt;
    return&lt;br /&gt;
        &amp;lt;valueSet&amp;gt;&lt;br /&gt;
        {&lt;br /&gt;
            $termsByVS/ancestor::valueSet/@*,&lt;br /&gt;
            for $term in $termsByVS&lt;br /&gt;
            let $replacementTerm    := map:get($namemap, $term/@code)/@langfsn&lt;br /&gt;
            return&lt;br /&gt;
                element {name($term)} {&lt;br /&gt;
                    $term/@*,&lt;br /&gt;
                    if (empty($replacementTerm)) then () else attribute replaceBy {$replacementTerm},&lt;br /&gt;
                    $term/node()&lt;br /&gt;
                }&lt;br /&gt;
        }&lt;br /&gt;
        &amp;lt;/valueSet&amp;gt;&lt;br /&gt;
};&lt;br /&gt;
declare function local:getReleaseNote($snomedConcepts as element()*, $resultConcepts as element()*) as element() {&lt;br /&gt;
    if ($refsetlanguage = &amp;#039;31000146106&amp;#039;) then&lt;br /&gt;
    &amp;lt;version date=&amp;quot;{substring(string(current-dateTime()), 1, 19)}&amp;quot; by=&amp;quot;{get:strCurrentUserName()}&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;desc language=&amp;quot;nl-NL&amp;quot;&amp;gt;SNOMED CT concept weergavenamen bijgewerkt met Nederlandse termen voor zover beschikbaar. Totaal: {count($resultConcepts[self::terminologyAssociation] | $resultConcepts[self::valueSet]/(concept | exception | include))}&lt;br /&gt;
            &amp;lt;ul&amp;gt;&lt;br /&gt;
                &amp;lt;li&amp;gt;Terminologiekoppelingen: {count($resultConcepts[self::terminologyAssociation])}&amp;lt;/li&amp;gt;&lt;br /&gt;
                &amp;lt;li&amp;gt;Waardelijstconcepten {count($resultConcepts[self::valueSet]/(concept | exception | include))} in {count($resultConcepts[self::valueSet])} waardelijsten&amp;lt;/li&amp;gt;&lt;br /&gt;
                {&lt;br /&gt;
                    let $others := $resultConcepts[not(self::valueSet | self::terminologyAssociation)]&lt;br /&gt;
                    return&lt;br /&gt;
                        if (empty($others)) then () else &amp;lt;li&amp;gt;Anders: {count($others)}&amp;lt;/li&amp;gt;&lt;br /&gt;
                }&lt;br /&gt;
            &amp;lt;/ul&amp;gt;&lt;br /&gt;
        &amp;lt;/desc&amp;gt;&lt;br /&gt;
    &amp;lt;/version&amp;gt;&lt;br /&gt;
    else (&lt;br /&gt;
    &amp;lt;version date=&amp;quot;{substring(string(current-dateTime()), 1, 19)}&amp;quot; by=&amp;quot;{get:strCurrentUserName()}&amp;quot;&amp;gt;&lt;br /&gt;
        &amp;lt;desc language=&amp;quot;nl-NL&amp;quot;&amp;gt;SNOMED CT concept display names updated with currently available description. Total: {count($resultConcepts[self::terminologyAssociation] | $resultConcepts[self::valueSet]/(concept | exception | include))}&lt;br /&gt;
            &amp;lt;ul&amp;gt;&lt;br /&gt;
                &amp;lt;li&amp;gt;Terminology associations: {count($resultConcepts[self::terminologyAssociation])}&amp;lt;/li&amp;gt;&lt;br /&gt;
                &amp;lt;li&amp;gt;Vaslue set concepts {count($resultConcepts[self::valueSet]/(concept | exception | include))} in {count($resultConcepts[self::valueSet])} value sets&amp;lt;/li&amp;gt;&lt;br /&gt;
                {&lt;br /&gt;
                    let $others := $resultConcepts[not(self::valueSet | self::terminologyAssociation)]&lt;br /&gt;
                    return&lt;br /&gt;
                        if (empty($others)) then () else &amp;lt;li&amp;gt;Other: {count($others)}&amp;lt;/li&amp;gt;&lt;br /&gt;
                }&lt;br /&gt;
            &amp;lt;/ul&amp;gt;&lt;br /&gt;
        &amp;lt;/desc&amp;gt;&lt;br /&gt;
    &amp;lt;/version&amp;gt;&lt;br /&gt;
    )&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
let $projectPrefix              := if (request:exists()) then request:get-parameter(&amp;#039;prefix&amp;#039;, ())               else &amp;#039;demo1-&amp;#039;&lt;br /&gt;
let $update                     := if (request:exists()) then request:get-parameter(&amp;#039;update&amp;#039;, &amp;#039;false&amp;#039;) = &amp;#039;true&amp;#039; else false()&lt;br /&gt;
&lt;br /&gt;
let $decor                      := if (empty($projectPrefix)) then () else collection(&amp;#039;/db/apps/decor/data&amp;#039;)/decor[project/@prefix = $projectPrefix]&lt;br /&gt;
let $snomedConcepts             := if (empty($projectPrefix)) then () else collection(&amp;#039;/db/apps/decor/data&amp;#039;)/decor[project/@prefix = $projectPrefix]//*[@code][@displayName][@codeSystem = $codeSystemSNOMED][not(ancestor::template)]&lt;br /&gt;
let $namemap                    := map:new(&lt;br /&gt;
        for $term in $snomedConcepts&lt;br /&gt;
        let $conceptId  := $term/@code&lt;br /&gt;
        group by $conceptId&lt;br /&gt;
        return&lt;br /&gt;
            map:entry($conceptId, local:getDisplayNameAndStatus($conceptId, $term[1]/@codeSystem, &amp;#039;nl-NL&amp;#039;))&lt;br /&gt;
    )&lt;br /&gt;
let $resultConcepts             := if (empty($snomedConcepts)) then () else local:getReturnLines($snomedConcepts, $namemap)&lt;br /&gt;
let $versionOrRelease           := if (empty($snomedConcepts)) then () else local:getReleaseNote($snomedConcepts, $resultConcepts)&lt;br /&gt;
&lt;br /&gt;
let $updateLines                := &lt;br /&gt;
    if ($update) then &lt;br /&gt;
        for $term in $snomedConcepts[@displayName]&lt;br /&gt;
        let $replacementTerm    := map:get($namemap, $term/@code)/@langfsn&lt;br /&gt;
        return&lt;br /&gt;
            if (empty($replacementTerm))    then () else &lt;br /&gt;
            if ($term[@displayName = $replacementTerm]) then () else (&lt;br /&gt;
                update value $term/@displayName with $replacementTerm&lt;br /&gt;
            )&lt;br /&gt;
    else ()&lt;br /&gt;
let $updateVersion              := &lt;br /&gt;
    if ($update) then &lt;br /&gt;
        if (empty($versionOrRelease))       then ( (: don&amp;#039;t have anything to write:) )          else &lt;br /&gt;
        if (empty($decor))                  then ( (: don&amp;#039;t have a project to write into :) )   else&lt;br /&gt;
        if ($decor/project[version | release]) then&lt;br /&gt;
            update insert $versionOrRelease preceding $decor/project/(release | version)[1]&lt;br /&gt;
        else (&lt;br /&gt;
            update insert $versionOrRelease into $decor/project&lt;br /&gt;
        )&lt;br /&gt;
    else ()&lt;br /&gt;
&lt;br /&gt;
return (&lt;br /&gt;
    &amp;lt;project prefix=&amp;quot;{$projectPrefix}&amp;quot; update=&amp;quot;{$update}&amp;quot;&amp;gt;&lt;br /&gt;
        {$versionOrRelease}&lt;br /&gt;
        &amp;lt;x&amp;gt;{$resultConcepts}&amp;lt;/x&amp;gt;&lt;br /&gt;
    &amp;lt;/project&amp;gt;&lt;br /&gt;
)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
[[category:xqueries]]&lt;/div&gt;</summary>
		<author><name>Ahenket</name></author>
		
	</entry>
</feed>