Project

Profile

Help

Support #5016 ยป xsidoc.xsl

Octavian Nadolu, 2021-06-02 09:18

 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

<xsl:variable name="input">
<doc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hl7-org:v3">
<doc2>
<comp xsi:type="IVL_TS" value="2"/>
</doc2>
<doc2>
<comp xsi:type="IVL_TS" value="1"/>
</doc2>
<doc2>
<comp xsi:type="IVL_TS" value="3"/>
</doc2>
</doc>
</xsl:variable>

<xsl:template match="/">
<xsl:apply-templates select="$input/*:doc"/>
</xsl:template>

<xsl:template match="/*:doc">
<xsl:variable name="var" as="element()*">
<xsl:copy-of select="*:doc2"/>
</xsl:variable>

<xsl:variable name="varSorted" as="element()*">
<xsl:for-each select="*:doc2">
<xsl:sort data-type="number" select="*:comp/@value"/>
<!-- tested this with xsl:sequence, but it does not respect the sorted order, it takes the input order from the input xml -->
<!-- the xslt2 perform-sort function has the same result (probably for same reason, since it uses sequence as well) -->
<!-- so we are using copy-of here to preserve order, even though it is known to perform worse -->
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:variable>

<xsl:variable name="varSortedWithSequence" as="element()*">
<xsl:for-each select="*:doc2">
<xsl:sort data-type="number" select="*:comp/@value"/>
<!-- the sorted order is not respected by the sequence select below -->
<xsl:sequence select="."/>
</xsl:for-each>
</xsl:variable>

<test>
<select-on-original>
<xsl:comment> content as expected </xsl:comment>
<xsl:copy-of select=".//*:comp[namespace-uri-from-QName(resolve-QName(@xsi:type, .)) = 'urn:hl7-org:v3']"/>
</select-on-original>
<select-on-variable>
<xsl:comment> empty but that is unexpected (namespace predicate problem)</xsl:comment>
<xsl:copy-of select="$var//*:comp[namespace-uri-from-QName(resolve-QName(@xsi:type, .)) = 'urn:hl7-org:v3']"/>
</select-on-variable>
<select-on-sorted-variable>
<xsl:comment> empty but that is unexpected (namespace predicate problem) </xsl:comment>
<xsl:copy-of select="$varSorted//*:comp[namespace-uri-from-QName(resolve-QName(@xsi:type, .)) = 'urn:hl7-org:v3']"/>
</select-on-sorted-variable>
<select-on-sorted-variable>
<xsl:comment> output is sorted (without namespace predicate)</xsl:comment>
<xsl:copy-of select="$varSorted//*:comp"/>
</select-on-sorted-variable>
<select-on-sorted-variableWithSequence>
<xsl:comment>output is not sorted unfortunately (namespace predicate works fine) </xsl:comment>
<xsl:copy-of select="$varSortedWithSequence//*:comp[namespace-uri-from-QName(resolve-QName(@xsi:type, .)) = 'urn:hl7-org:v3']"/>
</select-on-sorted-variableWithSequence>
</test>

</xsl:template>
</xsl:stylesheet>
    (1-1/1)