<?xml version="1.0" encoding="UTF-8"?>
<!--
Template derived pattern
===========================================
ID: 2.16.840.1.113883.3.4424.13.10.2.21
Name: [2] Dokument powiązany dla P1
Description: Bazowy dla platformy P1 szablon struktury CDA dla dokumentu powiązanego.
-->
<pattern xmlns="http://purl.oclc.org/dsdl/schematron"
         id="template-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000">
   <title>[2] Dokument powiązany dla P1</title>
   <!--
Template derived rules for ID: 2.16.840.1.113883.3.4424.13.10.2.21
Context: *[hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']]
Item: (plCdaP1BaseRelatedDocument)
-->

   <!--
Template derived rules for ID: 2.16.840.1.113883.3.4424.13.10.2.21
Context: *[hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']]/hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']
Item: (plCdaP1BaseRelatedDocument)
-->

   <rule context="*[hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']]/hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']"
         id="tmp-">
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="@typeCode">(plCdaP1BaseRelatedDocument): atrybut @typeCode MUSI wystąpić.</assert>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="not(@typeCode) or (string-length(@typeCode)&gt;0 and not(matches(@typeCode,'\s')))">(plCdaP1BaseRelatedDocument): Atrybut @typeCode MUSI być typu 'cs'</assert>
      <let name="theAttValue"
           value="distinct-values(tokenize(normalize-space(@typeCode),' '))"/>
      <let name="theAttCheck"
           value="distinct-values(for $code in tokenize(@typeCode,' ') return if ($code=('RPLC','APND')) then ($code) else ())"/>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="not(@typeCode) or count($theAttValue) = count($theAttCheck)">(plCdaP1BaseRelatedDocument): Wartość dla typeCode MUSI wynosić 'kod RPLC lub kod APND'.</assert>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="count(hl7:templateId[@root='2.16.840.1.113883.3.4424.13.10.2.21'])&gt;=1 and not(hl7:templateId[@root='2.16.840.1.113883.3.4424.13.10.2.21']/@nullFlavor)">(plCdaP1BaseRelatedDocument): element hl7:templateId[@root='2.16.840.1.113883.3.4424.13.10.2.21'] jest mandatory [min 1x].</assert>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="count(hl7:templateId[@root='2.16.840.1.113883.3.4424.13.10.2.21'])&lt;=1">(plCdaP1BaseRelatedDocument): element hl7:templateId[@root='2.16.840.1.113883.3.4424.13.10.2.21'] występuje zbyt często [max 1x].</assert>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="count(hl7:parentDocument)&gt;=1 ">(plCdaP1BaseRelatedDocument): element hl7:parentDocument jest required [min 1x].</assert>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="count(hl7:parentDocument)&lt;=1">(plCdaP1BaseRelatedDocument): element hl7:parentDocument występuje zbyt często [max 1x].</assert>
   </rule>
   <!--
Template derived rules for ID: 2.16.840.1.113883.3.4424.13.10.2.21
Context: *[hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']]/hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']/hl7:templateId[@root='2.16.840.1.113883.3.4424.13.10.2.21']
Item: (plCdaP1BaseRelatedDocument)
-->

   <rule context="*[hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']]/hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']/hl7:templateId[@root='2.16.840.1.113883.3.4424.13.10.2.21']"
         id="tmp-">
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="(local-name-from-QName(resolve-QName(@xsi:type,.))='II' and namespace-uri-from-QName(resolve-QName(@xsi:type,.))='urn:hl7-org:v3') or not(@xsi:type)">(plCdaP1BaseRelatedDocument): Jeśli występuje instrukcja @xsi:type, to MUSI mieć wartość "{urn:hl7-org:v3}:II", znaleziono "{<value-of select="namespace-uri-from-QName(resolve-QName(@xsi:type,.))"/>}:<value-of select="local-name-from-QName(resolve-QName(@xsi:type,.))"/>"</assert>
      <extends rule="II"/>
      <let name="theValue" value="@value"/>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="string(@root)='2.16.840.1.113883.3.4424.13.10.2.21'">(plCdaP1BaseRelatedDocument): Wartość dla @root MUSI wynosić '2.16.840.1.113883.3.4424.13.10.2.21'.</assert>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="not(@root) or matches(@root,'^[0-2](\.(0|[1-9][0-9]*))*$')">(plCdaP1BaseRelatedDocument): Atrybut @root MUSI być typu 'oid'</assert>
   </rule>
   <!--
Template derived rules for ID: 2.16.840.1.113883.3.4424.13.10.2.21
Context: *[hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']]/hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']/hl7:parentDocument
Item: (plCdaP1BaseRelatedDocument)
-->

   <rule context="*[hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']]/hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']/hl7:parentDocument"
         id="tmp-">
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="string(@classCode)='DOCCLIN' or not(@classCode)">(plCdaP1BaseRelatedDocument): Wartość dla @classCode MUSI wynosić 'DOCCLIN'.</assert>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="string(@moodCode)='EVN' or not(@moodCode)">(plCdaP1BaseRelatedDocument): Wartość dla @moodCode MUSI wynosić 'EVN'.</assert>
      <report role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="hl7:id[@root=/hl7:ClinicalDocument/hl7:id/@root and @extension=/hl7:ClinicalDocument/hl7:id/@extension]">(plCdaP1BaseRelatedDocument): Idetyfikator dokumentu jest taki sam jak identyfikator dokumentu powiązanego wskazujcego na jego poprzednią wersję.</report>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="hl7:setId[@root=/hl7:ClinicalDocument/hl7:setId/@root and @extension=/hl7:ClinicalDocument/hl7:setId/@extension]">(plCdaP1BaseRelatedDocument): Identyfikator grupy wersji dokumentów (setId) powienien być identyczny dla dokumentu i dokumentu powiązanego wskazującego na jego poprzednią wersję.</assert>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="(xs:integer(hl7:versionNumber/@value)+1)=xs:integer(/hl7:ClinicalDocument/hl7:versionNumber/@value)">(plCdaP1BaseRelatedDocument): Dokument powiązany powienien wskazywać poprzednią wersję dokumentu.</assert>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="count(hl7:id)&gt;=1 and not(hl7:id/@nullFlavor)">(plCdaP1BaseRelatedDocument): element hl7:id jest mandatory [min 1x].</assert>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="count(hl7:id)&lt;=1">(plCdaP1BaseRelatedDocument): element hl7:id występuje zbyt często [max 1x].</assert>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="count(hl7:setId)&gt;=1 and not(hl7:setId/@nullFlavor)">(plCdaP1BaseRelatedDocument): element hl7:setId jest mandatory [min 1x].</assert>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="count(hl7:setId)&lt;=1">(plCdaP1BaseRelatedDocument): element hl7:setId występuje zbyt często [max 1x].</assert>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="count(hl7:versionNumber)&gt;=1 and not(hl7:versionNumber/@nullFlavor)">(plCdaP1BaseRelatedDocument): element hl7:versionNumber jest mandatory [min 1x].</assert>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="count(hl7:versionNumber)&lt;=1">(plCdaP1BaseRelatedDocument): element hl7:versionNumber występuje zbyt często [max 1x].</assert>
   </rule>
   <!--
Template derived rules for ID: 2.16.840.1.113883.3.4424.13.10.2.21
Context: *[hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']]/hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']/hl7:parentDocument/hl7:id
Item: (plCdaP1BaseRelatedDocument)
-->

   <rule context="*[hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']]/hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']/hl7:parentDocument/hl7:id"
         id="tmp-">
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="(local-name-from-QName(resolve-QName(@xsi:type,.))='II' and namespace-uri-from-QName(resolve-QName(@xsi:type,.))='urn:hl7-org:v3') or not(@xsi:type)">(plCdaP1BaseRelatedDocument): Jeśli występuje instrukcja @xsi:type, to MUSI mieć wartość "{urn:hl7-org:v3}:II", znaleziono "{<value-of select="namespace-uri-from-QName(resolve-QName(@xsi:type,.))"/>}:<value-of select="local-name-from-QName(resolve-QName(@xsi:type,.))"/>"</assert>
      <extends rule="II"/>
      <let name="theValue" value="@value"/>
   </rule>
   <!--
Template derived rules for ID: 2.16.840.1.113883.3.4424.13.10.2.21
Context: *[hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']]/hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']/hl7:parentDocument/hl7:setId
Item: (plCdaP1BaseRelatedDocument)
-->

   <rule context="*[hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']]/hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']/hl7:parentDocument/hl7:setId"
         id="tmp-">
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="(local-name-from-QName(resolve-QName(@xsi:type,.))='II' and namespace-uri-from-QName(resolve-QName(@xsi:type,.))='urn:hl7-org:v3') or not(@xsi:type)">(plCdaP1BaseRelatedDocument): Jeśli występuje instrukcja @xsi:type, to MUSI mieć wartość "{urn:hl7-org:v3}:II", znaleziono "{<value-of select="namespace-uri-from-QName(resolve-QName(@xsi:type,.))"/>}:<value-of select="local-name-from-QName(resolve-QName(@xsi:type,.))"/>"</assert>
      <extends rule="II"/>
      <let name="theValue" value="@value"/>
   </rule>
   <!--
Template derived rules for ID: 2.16.840.1.113883.3.4424.13.10.2.21
Context: *[hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']]/hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']/hl7:parentDocument/hl7:versionNumber
Item: (plCdaP1BaseRelatedDocument)
-->

   <rule context="*[hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']]/hl7:relatedDocument[hl7:templateId/@root='2.16.840.1.113883.3.4424.13.10.2.21']/hl7:parentDocument/hl7:versionNumber"
         id="tmp-">
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="(local-name-from-QName(resolve-QName(@xsi:type,.))='INT' and namespace-uri-from-QName(resolve-QName(@xsi:type,.))='urn:hl7-org:v3') or not(@xsi:type)">(plCdaP1BaseRelatedDocument): Jeśli występuje instrukcja @xsi:type, to MUSI mieć wartość "{urn:hl7-org:v3}:INT", znaleziono "{<value-of select="namespace-uri-from-QName(resolve-QName(@xsi:type,.))"/>}:<value-of select="local-name-from-QName(resolve-QName(@xsi:type,.))"/>"</assert>
      <extends rule="INT"/>
      <let name="theValue" value="@value"/>
      <let name="digitok"
           value="matches(string($theValue), '^-?[1-9][0-9]*$|^+?[0-9]*$')"/>
      <assert role="error"
              see="https://www.cez.gov.pl/HL7POL-1.3.2/plcda-html-1.3.2/plcda-html/plcda-html-develop/tmp-2.16.840.1.113883.3.4424.13.10.2.21-2018-06-30T000000.html"
              test="$digitok or @nullFlavor">(plCdaP1BaseRelatedDocument): @value nie jest poprawną liczbą typu INT <value-of select="$theValue"/>
      </assert>
   </rule>
</pattern>
