<
xsl:stylesheet xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform" xmlns:xs=
"http://www.w3.org/2001/XMLSchema" xmlns:d=
"http://www.daisy.org/ns/pipeline/data" xmlns:c=
"http://www.w3.org/ns/xproc-step" xmlns:f=
"functions" xmlns:pf=
"http://www.daisy.org/ns/pipeline/functions" xmlns:pxi=
"http://www.daisy.org/ns/pipeline/xproc/internal" version=
"2.0" exclude-result-prefixes=
"xs">
<
xsl:include href=
"http://www.daisy.org/pipeline/modules/common-utils/generate-id.xsl"/>
<
xsl:param name=
"output-ns"/>
<
xsl:param name=
"output-sentence-tag"/>
<
xsl:param name=
"sentence-attr" select=
"''"/>
<
xsl:param name=
"sentence-attr-val" select=
"''"/>
<
xsl:param name=
"exclusive-sentence-tag" select=
"'true'"/>
<
xsl:param name=
"exclusive-word-tag" select=
"'true'"/>
<
xsl:param name=
"output-subsentence-tag"/>
<
xsl:param name=
"output-word-tag"/>
<
xsl:param name=
"word-attr" select=
"''"/>
<
xsl:param name=
"word-attr-val" select=
"''"/>
<
xsl:variable name=
"options" as=
"element(c:param-set)" select=
"collection()[2]/*"/>
<
xsl:variable name=
"tmp-word-tag" as=
"xs:QName" select=
"f:param-value-as-QName($options/c:param[@name='tmp-word-tag'])"/>
<
xsl:variable name=
"tmp-sentence-tag" as=
"xs:QName" select=
"f:param-value-as-QName($options/c:param[@name='tmp-sentence-tag'])"/>
<
xsl:function name=
"f:param-value-as-QName" as=
"xs:QName">
<
xsl:param name=
"param" as=
"element(c:param)"/>
<
xsl:sequence select=
"resolve-QName($param/@value,$param)"/>
</
xsl:function>
<
xsl:key name=
"sentence-for-element" match=
"d:sentence" use=
"@element"/>
<
xsl:variable name=
"sentence-ids" as=
"document-node(element(d:sentences))">
<
xsl:variable name=
"sentence-elements" as=
"document-node(element(d:sentences))">
<
xsl:document>
<
d:sentences>
<
xsl:apply-templates select=
"/*" mode=
"sentence-elements"/>
</
d:sentences>
</
xsl:document>
</
xsl:variable>
<
xsl:document>
<
xsl:apply-templates mode=
"sentence-ids" select=
"$sentence-elements/*"/>
</
xsl:document>
</
xsl:variable>
<
xsl:template match=
"*" mode=
"sentence-elements" priority=
"1">
<
xsl:apply-templates select=
"*" mode=
"#current"/>
</
xsl:template>
<
xsl:template match=
"*[local-name()=local-name-from-QName($tmp-sentence-tag) and namespace-uri()=namespace-uri-from-QName($tmp-sentence-tag)]" mode=
"sentence-elements" priority=
"2">
<
d:sentence element=
"{generate-id(.)}">
<
xsl:copy-of select=
"@xml:lang"/>
</
d:sentence>
</
xsl:template>
<
xsl:template mode=
"sentence-elements" priority=
"3" match=
"*[@pxi:special-sentence or ($exclusive-sentence-tag = 'true' and local-name() = $output-sentence-tag)]">
<
d:sentence element=
"{generate-id(.)}" recycled=
"1"/>
</
xsl:template>
<
xsl:template match=
"d:sentences" mode=
"sentence-ids" priority=
"1">
<
xsl:call-template name=
"pf:next-match-with-generated-ids">
<
xsl:with-param name=
"prefix" select=
"'id_'"/>
<
xsl:with-param name=
"for-elements" select=
"*"/>
<
xsl:with-param name=
"in-use" select=
"collection()[1]//*/@id"/>
</
xsl:call-template>
</
xsl:template>
<
xsl:template match=
"d:sentences" mode=
"sentence-ids">
<
xsl:copy>
<
xsl:apply-templates mode=
"#current" select=
"@*|node()"/>
</
xsl:copy>
</
xsl:template>
<
xsl:template match=
"d:sentence" mode=
"sentence-ids">
<
xsl:copy>
<
xsl:call-template name=
"pf:generate-id"/>
<
xsl:sequence select=
"@*|node()"/>
</
xsl:copy>
</
xsl:template>
<
xsl:template match=
"/" priority=
"2">
<
xsl:apply-templates select=
"node()"/>
<
xsl:result-document href=
"{concat('sids',generate-id(),'.xml')}" method=
"xml">
<
xsl:for-each select=
"$sentence-ids/*">
<
xsl:copy>
<
xsl:for-each select=
"*">
<
xsl:copy>
<
xsl:sequence select=
"@* except @element"/>
</
xsl:copy>
</
xsl:for-each>
</
xsl:copy>
</
xsl:for-each>
</
xsl:result-document>
</
xsl:template>
<
xsl:template match=
"node()" priority=
"1">
<
xsl:variable name=
"entry" select=
"key('sentence-for-element', generate-id(.), $sentence-ids)"/>
<
xsl:choose>
<
xsl:when test=
"$entry and $entry/@recycled">
<
xsl:copy copy-namespaces=
"no">
<
xsl:call-template name=
"copy-namespaces"/>
<
xsl:apply-templates select=
"@*" mode=
"inside-sentence"/>
<
xsl:if test=
"not(@id)">
<
xsl:attribute name=
"id">
<
xsl:value-of select=
"$entry/@id"/>
</
xsl:attribute>
</
xsl:if>
<
xsl:apply-templates select=
"node()" mode=
"inside-sentence">
<
xsl:with-param name=
"can-contain-sentences" select=
"exists(@pxi:can-contain-sentences)"/>
</
xsl:apply-templates>
</
xsl:copy>
</
xsl:when>
<
xsl:when test=
"$entry">
<
xsl:element name=
"{$output-sentence-tag}" namespace=
"{$output-ns}">
<
xsl:attribute name=
"id">
<
xsl:value-of select=
"$entry/@id"/>
</
xsl:attribute>
<
xsl:if test=
"$sentence-attr != ''">
<
xsl:attribute name=
"{$sentence-attr}">
<
xsl:value-of select=
"$sentence-attr-val"/>
</
xsl:attribute>
</
xsl:if>
<
xsl:copy-of select=
"@xml:lang"/>
<
xsl:apply-templates select=
"node()" mode=
"inside-sentence">
<
xsl:with-param name=
"can-contain-sentences" select=
"true()"/>
</
xsl:apply-templates>
</
xsl:element>
</
xsl:when>
<
xsl:when test=
"(local-name()=local-name-from-QName($tmp-word-tag) and namespace-uri()=namespace-uri-from-QName($tmp-word-tag)) or (local-name()=local-name-from-QName($tmp-sentence-tag) and namespace-uri()=namespace-uri-from-QName($tmp-sentence-tag))">
<
xsl:apply-templates select=
"node()"/>
</
xsl:when>
<
xsl:otherwise>
<
xsl:copy copy-namespaces=
"no">
<
xsl:call-template name=
"copy-namespaces"/>
<
xsl:apply-templates select=
"@*|node()"/>
</
xsl:copy>
</
xsl:otherwise>
</
xsl:choose>
</
xsl:template>
<
xsl:template match=
"*[local-name()=local-name-from-QName($tmp-sentence-tag) and namespace-uri()=namespace-uri-from-QName($tmp-sentence-tag)]" mode=
"inside-sentence" priority=
"2">
<
xsl:param name=
"can-contain-sentences" as=
"xs:boolean"/>
<
xsl:apply-templates select=
"node()" mode=
"#current">
<
xsl:with-param name=
"can-contain-sentences" select=
"$can-contain-sentences"/>
</
xsl:apply-templates>
</
xsl:template>
<
xsl:template match=
"*[local-name()=local-name-from-QName($tmp-word-tag) and namespace-uri()=namespace-uri-from-QName($tmp-word-tag)]" mode=
"inside-sentence" priority=
"2">
<
xsl:param name=
"can-contain-sentences" as=
"xs:boolean"/>
<
xsl:choose>
<
xsl:when test=
"$can-contain-sentences">
<
xsl:element name=
"{$output-word-tag}" namespace=
"{$output-ns}">
<
xsl:if test=
"$word-attr != ''">
<
xsl:attribute name=
"{$word-attr}">
<
xsl:value-of select=
"$word-attr-val"/>
</
xsl:attribute>
</
xsl:if>
<
xsl:apply-templates select=
"node()" mode=
"inside-word">
<
xsl:with-param name=
"can-contain-sentences" select=
"exists(@pxi:can-contain-sentences)"/>
</
xsl:apply-templates>
</
xsl:element>
</
xsl:when>
<
xsl:otherwise>
<
xsl:apply-templates select=
"node()" mode=
"#current">
<
xsl:with-param name=
"can-contain-sentences" select=
"exists(@pxi:can-contain-sentences)"/>
</
xsl:apply-templates>
</
xsl:otherwise>
</
xsl:choose>
</
xsl:template>
<
xsl:template match=
"*[$exclusive-sentence-tag='true' and local-name()=$output-sentence-tag]" mode=
"inside-sentence" priority=
"3">
<
xsl:apply-templates select=
"node()" mode=
"#current">
<
xsl:with-param name=
"can-contain-sentences" select=
"true()"/>
</
xsl:apply-templates>
</
xsl:template>
<
xsl:template match=
"*[$exclusive-word-tag='true' and local-name()=$output-word-tag]" mode=
"inside-sentence" priority=
"2">
<
xsl:copy copy-namespaces=
"no">
<
xsl:call-template name=
"copy-namespaces"/>
<
xsl:apply-templates select=
"@*|node()" mode=
"inside-word"/>
</
xsl:copy>
</
xsl:template>
<
xsl:template match=
"node()" mode=
"inside-sentence" priority=
"1">
<
xsl:copy copy-namespaces=
"no">
<
xsl:call-template name=
"copy-namespaces"/>
<
xsl:apply-templates select=
"@*" mode=
"#current"/>
<
xsl:apply-templates select=
"node()" mode=
"#current">
<
xsl:with-param name=
"can-contain-sentences" select=
"exists(@pxi:can-contain-sentences)"/>
</
xsl:apply-templates>
</
xsl:copy>
</
xsl:template>
<
xsl:template match=
"*[local-name()=local-name-from-QName($tmp-word-tag) and namespace-uri()=namespace-uri-from-QName($tmp-word-tag)]" mode=
"inside-word" priority=
"2">
<
xsl:apply-templates select=
"node()" mode=
"#current"/>
</
xsl:template>
<
xsl:template match=
"*[$exclusive-word-tag='true' and local-name()=$output-word-tag]" mode=
"inside-word" priority=
"2">
<
xsl:choose>
<
xsl:when test=
"count(@*) > 0">
<
xsl:element name=
"{$output-subsentence-tag}" namespace=
"{$output-ns}">
<
xsl:apply-templates select=
"@*|node()" mode=
"#current"/>
</
xsl:element>
</
xsl:when>
<
xsl:otherwise>
<
xsl:apply-templates select=
"node()" mode=
"#current"/>
</
xsl:otherwise>
</
xsl:choose>
</
xsl:template>
<
xsl:template match=
"node()" mode=
"inside-word" priority=
"1">
<
xsl:copy copy-namespaces=
"no">
<
xsl:call-template name=
"copy-namespaces"/>
<
xsl:apply-templates select=
"@*|node()" mode=
"#current"/>
</
xsl:copy>
</
xsl:template>
<
xsl:template mode=
"#default inside-sentence inside-word" match=
"@*">
<
xsl:sequence select=
"."/>
</
xsl:template>
<
xsl:template mode=
"#default inside-sentence inside-word" match=
"@pxi:special-sentence| @pxi:can-contain-sentences| @pxi:cannot-be-sentence-child"/>
<
xsl:variable name=
"remove-ns" as=
"xs:string*" select=
"('http://www.daisy.org/ns/pipeline/xproc/internal', distinct-values( for $tag in ($tmp-word-tag,$tmp-sentence-tag) return namespace-uri-from-QName($tag)))"/>
<
xsl:template name=
"copy-namespaces">
<
xsl:for-each select=
"namespace::*[not(.=$remove-ns)]">
<
xsl:sequence select=
"."/>
</
xsl:for-each>
</
xsl:template>
</
xsl:stylesheet>