<
xsl:stylesheet xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform" xmlns:html=
"http://www.w3.org/1999/xhtml" xmlns:xs=
"http://www.w3.org/2001/XMLSchema" xmlns:pf=
"http://www.daisy.org/ns/pipeline/functions" xmlns:d=
"http://www.daisy.org/ns/pipeline/data" version=
"2.0" xpath-default-namespace=
"" exclude-result-prefixes=
"#all">
<
xsl:import href=
"http://www.daisy.org/pipeline/modules/html-utils/library.xsl"/>
<
xsl:import href=
"http://www.daisy.org/pipeline/modules/common-utils/library.xsl"/>
<
xsl:include href=
"http://www.daisy.org/pipeline/modules/common-utils/generate-id.xsl"/>
<
xsl:variable name=
"smil" select=
"collection()[1]/*"/>
<
xsl:variable name=
"html" select=
"collection()[position()>2]/*"/>
<
xsl:variable name=
"page-list" select=
"collection()[2]"/>
<
xsl:variable name=
"smil-base-uri" select=
"pf:base-uri($smil)"/>
<
xsl:key name=
"id" match=
"*[@id]" use=
"@id"/>
<
xsl:key name=
"absolute-id" match=
"*[@id]" use=
"concat(pf:normalize-uri(pf:html-base-uri(.)),'#',@id)"/>
<
xsl:key name=
"absolute-src" match=
"text" use=
"pf:normalize-uri(pf:resolve-uri(@src,.))"/>
<
xsl:variable name=
"referenced-html-elements" as=
"element()*">
<
xsl:for-each select=
"//text">
<
xsl:variable name=
"absolute-src" select=
"pf:normalize-uri(pf:resolve-uri(@src,.))"/>
<
xsl:variable name=
"referenced-element" as=
"element()*" select=
"$html/key('absolute-id',$absolute-src)"/>
<
xsl:if test=
"count($referenced-element)=0">
<
xsl:message terminate=
"yes" select=
"concat('SMIL "',replace($smil-base-uri,'^.*/([^/]+)^','$1'), '" references a non-existing element "',$absolute-src,'"')"/>
</
xsl:if>
<
xsl:if test=
"count($referenced-element)>1">
<
xsl:message select=
"concat('Reference "',@src,'" in SMIL "', replace($smil-base-uri,'^.*/([^/]+)^','$1'),'" is ambiguous')"/>
</
xsl:if>
<
xsl:sequence select=
"$referenced-element[1]"/>
</
xsl:for-each>
</
xsl:variable>
<
xsl:variable name=
"page-number-elements" as=
"element()*">
<
xsl:for-each select=
"$html">
<
xsl:variable name=
"content-doc" select=
"."/>
<
xsl:variable name=
"content-doc-uri" select=
"pf:normalize-uri(base-uri(.))"/>
<
xsl:for-each select=
"$page-list//d:file[pf:normalize-uri(resolve-uri(@href,base-uri(.)))=$content-doc-uri]/d:anchor">
<
xsl:sequence select=
"key('id',@id,$content-doc)"/>
</
xsl:for-each>
</
xsl:for-each>
</
xsl:variable>
<
xsl:template match=
"/smil/body/seq" priority=
"1">
<
xsl:call-template name=
"pf:next-match-with-generated-ids">
<
xsl:with-param name=
"prefix" select=
"'par_'"/>
<
xsl:with-param name=
"for-elements" select=
"($html//*[self::html:h1 or self::html:h2 or self::html:h3 or self::html:h4 or self::html:h5 or self::html:h6], $page-number-elements) except $referenced-html-elements"/>
<
xsl:with-param name=
"in-use" select=
"(//*/@id, //*/@id[matches(.,'^(text|audio)_')]/replace(.,'^(text|audio)','par'))"/>
</
xsl:call-template>
</
xsl:template>
<
xsl:template match=
"/smil/body/seq">
<
xsl:variable name=
"seq" select=
"."/>
<
xsl:variable name=
"seq-base-uri" select=
"pf:base-uri(.)"/>
<
xsl:variable name=
"missing-pars" as=
"element()*">
<
xsl:for-each select=
"$html">
<
xsl:variable name=
"html-base-uri" select=
"pf:html-base-uri(.)"/>
<
xsl:variable name=
"heading-and-pagenum-elements" as=
"element()*" select=
"(//*[self::html:h1 or self::html:h2 or self::html:h3 or self::html:h4 or self::html:h5 or self::html:h6], //* intersect $page-number-elements)"/>
<
xsl:for-each select=
"$heading-and-pagenum-elements">
<
xsl:variable name=
"id" as=
"xs:string" select=
"@id"/>
<
xsl:choose>
<
xsl:when test=
"$referenced-html-elements intersect .">
</
xsl:when>
<
xsl:when test=
"$referenced-html-elements/descendant::* intersect . or $heading-and-pagenum-elements/descendant::* intersect .">
<
xsl:choose>
<
xsl:when test=
". intersect $page-number-elements">
<
xsl:message select=
"concat( 'SMIL references an element that contains a page number. ', 'NCC will point to the containing element instead of the page number.')"/>
<
seq textref=
"{pf:relativize-uri(concat($html-base-uri,'#',$id),$seq-base-uri)}">
<
xsl:attribute name=
"contained-in">
<
xsl:variable name=
"contained-in" as=
"attribute(id)">
<
xsl:for-each select=
"($heading-and-pagenum-elements,$referenced-html-elements) [descendant::* intersect current()][1]">
<
xsl:choose>
<
xsl:when test=
"$referenced-html-elements intersect .">
<
xsl:variable name=
"absolute-id" select=
"concat(pf:normalize-uri(pf:html-base-uri(.)),'#',@id)"/>
<
xsl:sequence select=
"$smil/key('absolute-src',$absolute-id)/parent::par/@id"/>
</
xsl:when>
<
xsl:otherwise>
<
xsl:call-template name=
"pf:generate-id"/>
</
xsl:otherwise>
</
xsl:choose>
</
xsl:for-each>
</
xsl:variable>
<
xsl:sequence select=
"string($contained-in)"/>
</
xsl:attribute>
</
seq>
</
xsl:when>
<
xsl:otherwise>
<
xsl:message terminate=
"yes">
<
xsl:text>Text/audio synchronization too coarse. SMIL references an ancestor of the heading with id </
xsl:text>
<
xsl:value-of select=
"@id"/>
</
xsl:message>
</
xsl:otherwise>
</
xsl:choose>
</
xsl:when>
<
xsl:otherwise>
<
xsl:variable name=
"par-id" as=
"xs:string">
<
xsl:call-template name=
"pf:generate-id"/>
</
xsl:variable>
<
xsl:variable name=
"text-id" as=
"xs:string" select=
"replace($par-id,'par_','text_')"/>
<
xsl:choose>
<
xsl:when test=
"$referenced-html-elements/ancestor::* intersect .">
<
xsl:variable name=
"segments" as=
"element()*" select=
"$referenced-html-elements[ancestor::* intersect current()]"/>
<
xsl:choose>
<
xsl:when test=
"replace(string-join($segments/string(.),''),'[\s\p{Z}]+','') =replace(string(.),'[\s\p{Z}]+','')">
<
xsl:variable name=
"smil-segments" as=
"element()*" select=
"for $s in $segments return for $id in $s/concat(pf:normalize-uri(pf:html-base-uri(.)),'#',@id) return $smil//text[pf:normalize-uri(pf:resolve-uri(@src,.))=$id]/parent::*"/>
<
xsl:variable name=
"audio-segments" as=
"element()*" select=
"$smil-segments/audio"/>
<
xsl:choose>
<
xsl:when test=
"every $i in 1 to count($audio-segments) - 1 satisfies ($audio-segments[$i]/@src=$audio-segments[$i + 1]/@src and $audio-segments[$i]/@clip-end=$audio-segments[$i + 1]/@clip-begin)">
<
xsl:variable name=
"audio-id" as=
"xs:string" select=
"replace($par-id,'par_','audio_')"/>
<
par id=
"{$par-id}" endsync=
"last">
<
xsl:if test=
". intersect $page-number-elements">
<
xsl:attribute name=
"system-required" select=
"'pagenumber-on'"/>
</
xsl:if>
<
text id=
"{$text-id}" src=
"{pf:relativize-uri(concat($html-base-uri,'#',$id),$seq-base-uri)}"/>
<
audio id=
"{$audio-id}" src=
"{pf:relativize-uri($audio-segments[1]/pf:resolve-uri(@src,.),$seq-base-uri)}" clip-begin=
"{$audio-segments[1]/@clip-begin}" clip-end=
"{$audio-segments[last()]/@clip-end}"/>
</
par>
</
xsl:when>
<
xsl:otherwise>
<
xsl:call-template name=
"pf:debug">
<
xsl:with-param name=
"msg">SMIL "<
xsl:value-of select=
" replace($smil-base-uri,'^.*/([^/]+)^','$1') "/>" references one or more segments inside a
<
xsl:value-of select=
" if (starts-with(local-name(),'h')) then 'heading' else 'page number'"/>
but the corresponding audio clips can not be combined: <
xsl:value-of select=
" string-join($audio-segments/concat(@src,' (',@clip-begin,'-',@clip-end,')'),', ')"/></
xsl:with-param>
</
xsl:call-template>
<
seq id=
"{$par-id}" textref=
"{pf:relativize-uri(concat($html-base-uri,'#',$id),$seq-base-uri)}">
<
xsl:choose>
<
xsl:when test=
". intersect $page-number-elements">
<
xsl:for-each select=
"$smil-segments">
<
xsl:attribute name=
"system-required" select=
"'pagenumber-on'"/>
<
xsl:sequence select=
"@*|node()"/>
</
xsl:for-each>
</
xsl:when>
<
xsl:otherwise>
<
xsl:sequence select=
"$smil-segments"/>
</
xsl:otherwise>
</
xsl:choose>
</
seq>
</
xsl:otherwise>
</
xsl:choose>
</
xsl:when>
<
xsl:otherwise>
<
xsl:message terminate=
"yes" select=
"concat('SMIL "',replace($smil-base-uri,'^.*/([^/]+)^','$1'), '" references one or more segments inside a ', if (starts-with(local-name(),'h')) then 'heading' else 'page number', ' but these segments do not add up to the complete ', if (starts-with(local-name(),'h')) then 'heading' else 'page number', ': ',string-join($segments/concat($html-base-uri,'#',@id),', '))"/>
</
xsl:otherwise>
</
xsl:choose>
</
xsl:when>
<
xsl:otherwise>
<
par id=
"{$par-id}" endsync=
"last">
<
xsl:if test=
". intersect $page-number-elements">
<
xsl:attribute name=
"system-required" select=
"'pagenumber-on'"/>
</
xsl:if>
<
text id=
"{$text-id}" src=
"{pf:relativize-uri(concat($html-base-uri,'#',$id),$seq-base-uri)}"/>
</
par>
</
xsl:otherwise>
</
xsl:choose>
</
xsl:otherwise>
</
xsl:choose>
</
xsl:for-each>
</
xsl:for-each>
</
xsl:variable>
<
xsl:copy>
<
xsl:apply-templates select=
"@*"/>
<
xsl:apply-templates select=
"(par,$missing-pars)">
<
xsl:sort select=
"index-of($html[pf:base-uri(.)=current()/pf:resolve-uri(substring-before(@textref|text/@src,'#'), (pf:base-uri(.),$seq-base-uri)[1])], $html)"/>
<
xsl:sort select=
"$html[pf:html-base-uri(.)=current()/pf:resolve-uri(substring-before(@textref|text/@src,'#'), (pf:base-uri(.),$seq-base-uri)[1])] //*[@id=substring-after(current()/(@textref|text/@src),'#')][1]/count(preceding::*|ancestor::*)"/>
</
xsl:apply-templates>
</
xsl:copy>
</
xsl:template>
<
xsl:template match=
"/smil/body/seq/par">
<
xsl:variable name=
"absolute-src" select=
"pf:normalize-uri(pf:resolve-uri(text/@src,.))"/>
<
xsl:variable name=
"referenced-element" as=
"element()" select=
"($html/key('absolute-id',$absolute-src))[1]"/>
<
xsl:if test=
"not($referenced-element/ancestor::*[self::html:h1 or self::html:h2 or self::html:h3 or self::html:h4 or self::html:h5 or self::html:h6] or $referenced-element/ancestor::* intersect $page-number-elements)">
<
xsl:next-match/>
</
xsl:if>
</
xsl:template>
<
xsl:template match=
"@*|node()">
<
xsl:copy>
<
xsl:apply-templates select=
"@*|node()"/>
</
xsl:copy>
</
xsl:template>
</
xsl:stylesheet>