Skip to content

Instantly share code, notes, and snippets.

@gimsieke
Created October 6, 2012 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gimsieke/3846098 to your computer and use it in GitHub Desktop.
Save gimsieke/3846098 to your computer and use it in GitHub Desktop.
IDML madness
<!-- Remove a Br at the end of a story, a footnote, or a table cell (with no content after it).
These Brs have been inserted in the previous pass, after each paragraph, and they should
be removed as they insert additional and unwanted empty lines.
Ok, the pattern's predicate looks weird. Suggestions on how to simplify it? -->
<xsl:template mode="xml2idml:storify_content-n-cleanup"
match="Br[for $scoper in ancestor::*[idml2xml:is-scope-origin(.)][1]
(: a let statement might come in handy here – if only for the sake of syntactic sweetness :)
return
current() is (
$scoper//*[name() = $idml2xml:idml-scope-terminal-names]
[idml2xml:same-scope(., $scoper)]
)[last()]
]" />
<!-- The following variables and functions have been imported
from idml2xml/xslt/common-functions.xsl – the other direction -->
<xsl:variable
name="idml2xml:idml-content-element-names"
select="('TextVariableInstance', 'Content', 'Rectangle', 'PageReference', 'idml2xml:genAnchor', 'TextFrame')"
as="xs:string+" />
<xsl:variable
name="idml2xml:idml-scope-terminal-names"
select="($idml2xml:idml-content-element-names, 'Br', 'idml2xml:genFrame', 'Footnote', 'Table', 'Story', 'XmlStory', 'Cell', 'CharacterStyleRange')"
as="xs:string+" />
<!-- Tests whether $elt is immediately contained in $ancestor-elt
(that is, in the same Story, in the same Cell, etc.).
Or phrased differently: whether $elt is in a paragraph
where $ancestor-elt's ParagraphStyleRange is still in force.
This function works with IDML input and with extracted XML,
provided that @aid:table and @idml2xml:story are conserved
in the extracted XML.
-->
<xsl:function name="idml2xml:same-scope" as="xs:boolean">
<xsl:param name="elt" as="element(*)" />
<xsl:param name="ancestor-elt" as="element(*)" />
<xsl:sequence select="not($elt/ancestor::*[idml2xml:is-scope-origin(.)]
[some $a in ancestor::* satisfies ($a is $ancestor-elt)])" />
</xsl:function>
<xsl:function name="idml2xml:is-scope-origin" as="xs:boolean">
<xsl:param name="elt" as="element(*)" />
<xsl:sequence select=" $elt/self::Cell
or $elt/@aid:table eq 'cell'
or $elt/self::Footnote
or $elt/self::Story
or $elt/self::XmlStory
or $elt/@idml2xml:story
" />
</xsl:function>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment