Group nodes together based on condition and validate using Schematron - xslt

I am working on writing rules using Schematron to validate data below. The requirement is to verify whether a patient has at least one encounter in the past 12 months. If there are multiple encounters per patient, use the last encounter.
<root>
<entry>
<resource>
<resourceType>Encounter</resourceType>
<subject>
<id>Patient/12345</id>
</subject>
<encounterDate>2018-04-10T10:00:00</encounterDate>
</resource>
</entry>
<entry>
<resource>
<resourceType>Encounter</resourceType>
<subject>
<id>Patient/abcde</id>
</subject>
<encounterDate>2020-04-10T10:00:00</encounterDate>
</resource>
</entry>
<entry>
<resource>
<resourceType>Encounter</resourceType>
<subject>
<id>Patient/abcde</id>
</subject>
<encounterDate>2019-05-10T10:00:00</encounterDate>
</resource>
</entry>
</root>
The above data should pass the validation because the latest encounter is less than a year ago.
What I want to know is, if I write a template that groups encounters together by patient id, is there a way to pass that template to the rule context? If not, is there any other way of doing it?
I am completely new to both xslt and Schematron and here is what I have so far:
<schema xmlns="http://purl.oclc.org/dsdl/schematron" >
<pattern>
<key name="patientId" match="entry" use="/resouce/subject/id/text()"/>
<template name="dateByPatient" match="entry">
<root>
<for-each select="resource/subject/id">
<patient >
<for-each select="key('patientId',text())">
<effectiveDateTime><value-of select="./resource/encounterDate"/></effectiveDateTime>
</for-each>
</patient>
</for-each>
</root>
</template>
<let name="template">
<dateByPatient/>
</let>
<let name="latest">
<root>
<for-each select="$template/root/patient">
<patient >
<sort select="effectiveDateTime" order="descending" />
<if test="position() = 1">
<effectiveDateTime><value-of select="effectiveDateTime" /></effectiveDateTime>
</if>
</patient>
</for-each>
</root>
</let>
<rule context="$latest/root/patient/effectiveDateTime">
<let name="days" value="days-from-duration(fn:current-dateTime() - xs:dateTime(text()))" />
<assert test="days-from-duration(fn:current-dateTime() - xs:dateTime(text())) < 365">
Encounter date more than a year : <value-of select="$days" /> days
</assert>
</rule>
</pattern>
</schema>

With XSLT 3 underlying you could use
<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt3"
xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
<sch:ns prefix="map" uri="http://www.w3.org/2005/xpath-functions/map"/>
<sch:pattern>
<sch:rule context="root">
<sch:let name="groups"
value="let $encounter-resources := entry/resource[resourceType = 'Encounter']
return map:merge(
$encounter-resources
!
map {
data(subject/id) : xs:dateTime(encounterDate)
},
map { 'duplicates' : 'combine' }
)"/>
<sch:assert
test="every $patient in map:keys($groups)
satisfies
(current-dateTime() - max($groups($patient)))
lt xs:dayTimeDuration('P365D')">At least one patient with latest encounter more than a year ago.</sch:assert>
</sch:rule>
</sch:pattern>
</sch:schema>
Or to output more detailed information and to only process resources with type Encounter:
<?xml version="1.0" encoding="UTF-8"?>
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt3"
xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
<sch:ns prefix="map" uri="http://www.w3.org/2005/xpath-functions/map"/>
<sch:pattern>
<sch:rule context="root">
<sch:let name="groups"
value="let $encounter-resources := entry/resource[resourceType = 'Encounter']
return map:merge(
$encounter-resources
!
map {
data(subject/id) : xs:dateTime(encounterDate)
},
map { 'duplicates' : 'combine' }
)"/>
<sch:let name="failing-patients"
value="map:keys($groups)[(current-dateTime() - max($groups(.))) gt xs:dayTimeDuration('P365D')]"/>
<sch:report
test="exists($failing-patients)">Patients <sch:value-of select="$failing-patients"/> with latest encounter more than a year ago.</sch:report>
</sch:rule>
</sch:pattern>
</sch:schema>
I don't think you can mix Schematron and XSLT as freely as your code tries, you would need to set up an XProc pipeline to use p:xslt to group the original input and then a validation step to validate with Schematron.
As for your problems to run the second sample with node-schematron, it uses an XPath implementation that doesn't support the XPath 3.1 sort function it seems, node-schematron also fails to handle maps as intermediary results of a Schematron variable, so only stuffing all into one variable expression seems to do; two examples work:
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt3"
xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
<sch:ns prefix="map" uri="http://www.w3.org/2005/xpath-functions/map"/>
<sch:pattern>
<sch:rule context="root">
<sch:let name="failing-patients"
value="let $encounter-resources := entry/resource[resourceType = 'Encounter'],
$groups := map:merge(
$encounter-resources
!
map {
data(subject/id) : xs:dateTime(encounterDate)
},
map { 'duplicates' : 'combine' }
)
return map:keys($groups)[(current-dateTime() - max($groups(.))) gt xs:dayTimeDuration('P365D')]"/>
<sch:report
test="exists($failing-patients)">Patients <sch:value-of select="$failing-patients"/> with latest encounter more than a year ago.</sch:report>
</sch:rule>
</sch:pattern>
</sch:schema>
or
<sch:schema xmlns:sch="http://purl.oclc.org/dsdl/schematron" queryBinding="xslt3"
xmlns:sqf="http://www.schematron-quickfix.com/validator/process">
<sch:ns prefix="map" uri="http://www.w3.org/2005/xpath-functions/map"/>
<sch:pattern>
<sch:rule context="root">
<sch:let name="failing-patients"
value="let
$encounter-resources := entry/resource[resourceType = 'Encounter'],
$groups := fold-left(
$encounter-resources,
map{},
function($m, $e) {
map:put(
$m,
data($e/subject/id),
max((xs:dateTime($e/encounterDate), map:get($m, data($e/subject/id))))
)
})
return map:keys($groups)[(current-dateTime() - $groups(.)) gt xs:dayTimeDuration('P365D')]"/>
<sch:report test="exists($failing-patients)">Patients <sch:value-of
select="$failing-patients"/> with latest encounter more than a year
ago.</sch:report>
</sch:rule>
</sch:pattern>
</sch:schema>
If you need an assertion that fails then replace the sch:report with
<sch:assert
test="empty($failing-patients)">Patients <sch:value-of select="$failing-patients"/> with latest encounter more than a year ago.</sch:assert>

Related

MapForce - Add dayTimeDuration to dayTimeDuration

I am trying to use mapforce to generate an xslt 2.0 file. The mapping is adding 2 dayTimeDuration elements, doing so results in the following error;
No match for core.add(xs:dayTimeDuration, xs:dayTimeDuration). Check argument types.
Supported: +(xs:double, xs:double) -> xs:double
I thought that xslt 2.0 supported adding 2 dayTimeDurations. Is there a way of doing this using mapforce?
Cheers
Stew
Had almost the same problem, first tried to add functx-library but saw it creates absolute path in the generated xslt2-code, which isn't very good.
Well, turns out you can implement that function, but first you have to do some modifications...
Find your Mapforce installation directory, and MapForceLibraries -subdirectory. From that open the "core.mff", and find
<group name="math functions">
<component name="add" growable="true" growablebasename="value">
<sources>
<datapoint name="value1" type="xs:decimal"/>
<datapoint name="value2" type="xs:decimal"/>
</sources>
<targets>
<datapoint name="result" type="xs:decimal"/>
</targets>
As you can seem the "sources" and "targets" elements seems to define the in- and out data types. As it is, they have only implemented "add"-function for "xs:decimal". You can copy/paste this component, then rename it and give new in- out- data types, in your case they are both "xs:dayTimeDuration". Note that there are implementations for each supported language, but you can omit those that are not needed. Here's
what should work:
<component name="addDayTimeDuration" growable="true" growablebasename="value">
<sources>
<datapoint name="value1" type="xs:dayTimeDuration"/>
<datapoint name="value2" type="xs:dayTimeDuration"/>
</sources>
<targets>
<datapoint name="result" type="xs:dayTimeDuration"/>
</targets>
<implementations>
<implementation language="xslt">
<operator value="+"/>
</implementation>
<implementation language="xslt2">
<operator value="+"/>
</implementation>
<implementation language="builtin">
<function name="Core_Add"/>
</implementation>
</implementations>
<description>
<short>result = value1 + value2</short>
<long>Result is the dayTimeDuration value of adding value1 and value2.</long>
</description>
</component>
Your new function should now appear in the "math functions" and should be good to use.
After contacting Altova (the makers of MapForce);
While XPath 2 does offer a subtract-dayTimeDurations operation, this is not presently offered as a function inside MapForce.

remove namespaces of multiple tags

i've just learned some xslt langauge and i have a problem. I'm trying to remove this namespace without success.
Original xml:
<?xml version="1.0" encoding="UTF-8"?>
<file_information xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.chellodmc.com/files/dmc_media_delivery.xsd" xsi:schemaLocation="http://www.chellodmc.com/file0073/dmc_media_delivery.xsd http://www.chellodmc.com/files/dmc_media_delivery.xsd">
<asset_data soa:23946923649236489>
<upn>TEST_Sintec_HD</upn>
<title>NLD_NGC_C_Man United - Chelsea - Sunday #17.00</title>
<version>High Definition</version>
<duration>00:30</duration>
<tc_in>23:00:00:00</tc_in>
<tc_out>23:00:30:00</tc_out>
<aspect_ratio>16X9</aspect_ratio>
<segment>
<sequence>1</sequence>
<tc_in>23:00:00:00</tc_in>
<tc_out>23:00:30:00</tc_out>
<comment></comment>
</segment>
</asset_data>
</file_information>
Expected output xml:
<?xml version="1.0" encoding="UTF-8"?>
<file_information>
<asset_data>
<upn>TEST_Sintec_HD</upn>
<title>NLD_NGC_C_Man United - Chelsea - Sunday #17.00</title>
<version>High Definition</version>
<duration>00:30</duration>
<tc_in>23:00:00:00</tc_in>
<tc_out>23:00:30:00</tc_out>
<aspect_ratio>16X9</aspect_ratio>
<segment>
<sequence>1</sequence>
<tc_in>23:00:00:00</tc_in>
<tc_out>23:00:30:00</tc_out>
<comment></comment>
</segment>
</asset_data>
</file_information>
what would be your suggestion?
Many thanks!

Unable to use splitterChannel with advanced xml configuration with poco

I would like to configure an advance logger using poco and its configuration file.
I create a config.xml file like that :
<?xml version="1.0" ?>
<Application>
<logging>
<channels>
<c1>
<class>ColorConsoleChannel</class>
<formatter>
<class>PatternFormatter</class>
<pattern>%Y-%m-%d %H:%M:%S : %s : [%p] : %t</pattern>
</formatter>
<traceColor>lightBlue</traceColor>
<debugColor>blue</debugColor>
<informationColor>green</informationColor>
<noticeColor>green</noticeColor>
<warningColor>yellow</warningColor>
<errorColor>red</errorColor>
<criticalColor>lightMagenta</criticalColor>
<fatalColor>lightMagenta</fatalColor>
</c1>
<c2>
<class>FileChannel</class>
<path>logs/traceApplication.log</path>
<rotation>1 M</rotation>
<archive>number</archive>
<purgeCount>5</purgeCount>
<formatter>
<class>PatternFormatter</class>
<pattern>%Y-%m-%d %H:%M:%S : %T : [%p] : %t</pattern>
</formatter>
</c2>
</channels>
<loggers>
<consoleLogger>
<channel>c1</channel>
<level>information</level>
</consoleLogger>
<traceFileLogger>
<channel>c2</channel>
<level>trace</level>
</traceFileLogger>
</loggers>
<channels>
<cSplitter>
<class>SplitterChannel</class>
<channels>consoleLogger,traceFileLogger,mainFileLogger</channels>
</cSplitter>
</channels>
<loggers>
<root>
<channel>cSplitter</channel>
<level>trace</level>
</root>
</loggers>
</logging>
</Application>
I use a Poco::Util::ServerApplication class and in the initialize method I put :
void CBS2AudioVideo::initialize(Poco::Util::Application& self)
{
loadConfiguration("config.xml");
Poco::Util::ServerApplication::initialize(self);
}
Before adding the splitterChannel my logging works well but with it, it doesn't any more.
I got the error message :
Not found: logging channel: consoleLogger
My goal is to have only one root logger and when I use it, it log in information level into the console and in trace level into the file.
When I set channels in channels.cSplitter.channels it works but all channels are logged to the same level. And if I take the logging configuration slide (http://pocoproject.org/slides/185-LoggingConfiguration.pdf) they use loggers and not channels in the logging.channels.splitter.channels attribute area. So I think it is possible. More over the Logger class inherit from Channel too.
Someone has already done this kind of work or have an idea ?
I've finally found the solution.
I post it here if it interest someone.
This file works well.
<?xml version="1.0" ?>
<Application>
<logging>
<channels>
<cScreen>
<class>ColorConsoleChannel</class>
<formatter>
<class>PatternFormatter</class>
<pattern>%H:%M:%S : %T : [%p] : %t</pattern>
</formatter>
<traceColor>lightBlue</traceColor>
<debugColor>blue</debugColor>
<informationColor>white</informationColor>
<noticeColor>green</noticeColor>
<warningColor>yellow</warningColor>
<errorColor>red</errorColor>
<criticalColor>lightMagenta</criticalColor>
<fatalColor>lightMagenta</fatalColor>
</cScreen>
<cFile>
<class>FileChannel</class>
<path>logs/application.log</path>
<rotation>1 M</rotation>
<archive>number</archive>
<purgeCount>5</purgeCount>
<formatter>
<class>PatternFormatter</class>
<pattern>%H:%M:%S : %T : [%p] : %t</pattern>
</formatter>
</cFile>
</channels>
<loggers>
<root>
<name></name>
<channel>cFile</channel>
<level>trace</level>
</root>
<main>
<name>main</name>
<channel>cScreen</channel>
<level>trace</level>
</main>
</loggers>
</logging>
To log on the screen and in the file make :
Poco::Logger::get("main").trace(msg);
To log only into file make
Poco::Logger::get("").trace(msg);

SyncML bug with my custom server for the Replace command to the client

I develop a syncml server and I do not synchronize from the server to the client (nokia e71) for modified contacts. All the rest works except when I make a command replace towards the client for an existing localuid. The client returns me then the status 415 for this command (type or format of the datum is not corresponding) while the customer accepts the same datum for an addition (by a command add or replaces).
Has anybody already met this problem ?
Here are messages sent between the client and the server:
Server message with Replace command:
<?xml version="1.0" ?>
<!DOCTYPE SyncML
PUBLIC "-//SYNCML//DTD SyncML 1.2//EN"
"http://www.openmobilealliance.org/tech/DTD/OMA-TS-SyncML_RepPro_DTD-V1_2.dtd">
<SyncML xmlns="SYNCML:SYNCML1.2">
<SyncHdr>
<VerDTD>1.2</VerDTD><VerProto>SyncML/1.2</VerProto><SessionID>235</SessionID><MsgID>3</MsgID> <Target><LocURI>IMEI:358240030276208</LocURI></Target> <Source><LocURI>http://192.168.8.20:50000</LocURI></Source> <Meta>
<MaxMsgSize xmlns="syncml:metinf">1000000</MaxMsgSize><MaxObjSize xmlns="syncml:metinf">4000000</MaxObjSize>
</Meta>
</SyncHdr>
<SyncBody>
<Status>
<CmdID>1</CmdID><MsgRef>3</MsgRef><CmdRef>0</CmdRef><Cmd>SyncHdr</Cmd> <TargetRef>http://192.168.8.20:50000</TargetRef> <SourceRef>IMEI:358240030276208</SourceRef> <Data>200</Data>
</Status> <Status>
<CmdID>2</CmdID><MsgRef>3</MsgRef><CmdRef>3</CmdRef><Cmd>Sync</Cmd> <SourceRef>./C:Contacts.cdb</SourceRef> <TargetRef>./card</TargetRef> <Data>200</Data>
</Status> <Sync>
<CmdID>3</CmdID> <Source><LocURI>./card</LocURI></Source> <Target><LocURI>./C:Contacts.cdb</LocURI></Target><NumberOfChanges>1</NumberOfChanges> <Replace>
<CmdID>4</CmdID> <Meta><Type xmlns="syncml:metinf">text/x-vcard</Type> </Meta> <Item> <Target><LocURI>69</LocURI></Target> <Data>
<![CDATA[BEGIN:VCARD VERSION:2.1 N:Smith;Change;;; FN:Change Smith END:VCARD]]>
</Data> </Item>
</Replace>
</Sync> <Final/>
</SyncBody>
</SyncML>
Client Message with a replace command status:
<?xml version="1.0" ?>
<!DOCTYPE SyncML
PUBLIC '-//SYNCML//DTD SyncML 1.2//EN'
'http://www.openmobilealliance.org/tech/DTD/OMA-TS-SyncML_RepPro_DTD-V1_2.dtd'>
<SyncML xmlns="SYNCML:SYNCML1.2">
<SyncHdr>
<VerDTD>
1.2
</VerDTD>
<VerProto>
SyncML/1.2
</VerProto>
<SessionID>
235
</SessionID>
<MsgID>
4
</MsgID>
<Target>
<LocURI>
http://192.168.8.20:50000
</LocURI>
</Target>
<Source>
<LocURI>
IMEI:358240030276208
</LocURI>
<LocName>
test1
</LocName>
</Source>
<Meta>
<MaxMsgSize xmlns="syncml:metinf">
65535
</MaxMsgSize>
</Meta>
</SyncHdr>
<SyncBody>
<Status>
<CmdID>
1
</CmdID>
<MsgRef>
3
</MsgRef>
<CmdRef>
0
</CmdRef>
<Cmd>
SyncHdr
</Cmd>
<TargetRef>
IMEI:358240030276208
</TargetRef>
<SourceRef>
http://192.168.8.20:50000
</SourceRef>
<Data>
200
</Data>
</Status>
<Status>
<CmdID>
2
</CmdID>
<MsgRef>
3
</MsgRef>
<CmdRef>
3
</CmdRef>
<Cmd>
Sync
</Cmd>
<TargetRef>
./C:Contacts.cdb
</TargetRef>
<SourceRef>
./card
</SourceRef>
<Data>
200
</Data>
</Status>
<Status>
<CmdID>
3
</CmdID>
<MsgRef>
3
</MsgRef>
<CmdRef>
4
</CmdRef>
<Cmd>
Replace
</Cmd>
<TargetRef>
69
</TargetRef>
<Data>
415
</Data>
</Status>
<Final/>
</SyncBody>
</SyncML>

xslt test if a variable value is contained in a node set

I have the following two files:
<?xml version="1.0" encoding="utf-8" ?>
<!-- D E F A U L T H O S P I T A L P O L I C Y -->
<xas DefaultPolicy="open" DefaultSubjectsFile="subjects.xss">
<rule id="R1" access="deny" object="record" subject="roles/*[name()!='Staff']"/>
<rule id="R2" access="deny" object="diagnosis" subject="roles//Nurse"/>
<rule id="R3" access="grant" object="record[#id=$user]" subject="roles/member[#id=$user]"/>
</xas>
and the other xml file called subjects.xss is:
<?xml version="1.0" encoding="utf-8" ?>
<subjects>
<users>
<member id="dupont" password="4A-4E-E9-17-5D-CE-2C-DD-43-43-1D-F1-3F-5D-94-71">
<name>Pierre Dupont</name>
</member>
<member id="durand" password="3A-B6-1B-E8-C0-1F-CD-34-DF-C4-5E-BA-02-3C-04-61">
<name>Jacqueline Durand</name>
</member>
</users>
<roles>
<Staff>
<Doctor>
<member idref="dupont"/>
</Doctor>
<Nurse>
<member idref="durand"/>
</Nurse>
</Staff>
</roles>
</subjects>
I am writing an xsl sheet which will read the subject value for each rule in policy.xas and if the currently logged in user (accessible as variable "user" in the stylesheet) is contained in that subject value (say roles//Nurse), then do something.
I am not being able to test whether the currently logged in user ($user which is equal to say "durand") is contained in roles//Nurse in the subjects file (which is a different xml file). Hope that clarifies my question. Any ideas? Thanks in advance.
I suspect your $user variable holds a member node, correct? In which case the test would be:-
/roles/Nurse[member/idref=$user/#id]
BTW, using tag names to carry data such as "Nurse" and "Doctor" is not a good practice. You are effectively saying that each new role is a new type. Better would be:-
<roles>
<role>
<name>Nurse</name>
<member idref="durand" />
</role>
...
</roles>
Your test would be:-
/roles/role[name='Nurse' and member/idref=$user/#id]