xml - Copy any referenced figures to where they are referenced with xsl -


i looking way copy referenced figure/node referenced.

<chapter id="intro">   <title>introduction</title>   <para>welcome our new product. 1 of    new features <xref linkend="some-figure"/>.     other new features include ...   </para>   <para>grab <xref linkend="some-figure"/> , pull here too.</para> </chapter> <chapter>   <title>some other chapter</title>   <para>this chapter contains figure!    <figure id="some-figure"><title>my figure...</title><mediaobject>...</mediaobject></figure>    </para> </chapter> 

can turned into:

<chapter id="intro">   <title>introduction</title>   <para><figure><title>my figure...</title><mediaobject>...</mediaobject></figure>   welcome our new product. 1 of    new features <xref linkend="some-figure"/>.     other new features include ...   </para>   <para><figure><title>my figure...</title><mediaobject>...</mediaobject></figure>   grab <xref linkend="some-figure"/> , pull here too.   </para> </chapter> <chapter>   <title>some other chapter</title>   <para>this chapter contains figure!    <figure id="some-figure"><title>my figure...</title><mediaobject>...</mediaobject></figure>    </para> </chapter> 

updating references point copied figures icing on cake, information regarding , way @ nodes copied referenced.

so given para contains xref, want copy linked figures (minus id attribute) start of para content. define key fast access figure elements id:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">    <xsl:key name="figurebyid" match="figure" use="@id" />    <xsl:template match="@*|node()">     <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>   </xsl:template>    <xsl:template match="para[.//xref]">     <xsl:copy>       <xsl:apply-templates select="@*" /><!-- may not necessary -->       <!-- copy in referenced figures -->       <xsl:apply-templates select="key('figurebyid', .//xref/@linkend)"                            mode="noid"/>       <!-- , continue child nodes normal -->       <xsl:apply-templates select="node()" />     </xsl:copy>   </xsl:template>    <!-- special almost-identity template remove id attribute -->   <xsl:template match="node()" mode="noid">     <xsl:copy>       <xsl:apply-templates select="@*[local-name() != 'id']|node()"/>     </xsl:copy>   </xsl:template> </xsl:stylesheet> 

this makes use of nice feature of key function if pass node set second argument (the key values up) result union of node sets result looking each key value in turn. key('figurebyid', xref/@linkend) gives figure elements id matches any of xref elements in para, if same figure referenced more once 1 copy of figure inserted.


updating references point copied figures icing on cake

you achieve rewriting ids of copied figures include generate-id() of target paragraph, , use same transformation on xref linkends. this:

  <xsl:template match="para[.//xref]">     <xsl:copy>       <xsl:apply-templates select="@*" /><!-- may not necessary -->       <!-- copy in referenced figures, modifying ids include our own -->       <xsl:apply-templates select="key('figurebyid', .//xref/@linkend)"                            mode="modify-id">         <xsl:with-param name="targetnode" select="." />       </xsl:apply-templates>       <!-- , continue child nodes normal -->       <xsl:apply-templates select="node()" />     </xsl:copy>   </xsl:template>    <xsl:template match="node()" mode="modify-id">     <xsl:param name="targetnode" />     <xsl:copy>       <xsl:apply-templates select="@*"/>       <xsl:attribute name="id">         <xsl:value-of select="concat(generate-id($targetnode), '-', @id)" />       </xsl:attribute>       <xsl:apply-templates select="node()" />     </xsl:copy>   </xsl:template>    <!-- munge linkend attributes in same way did copied figure ids -->   <xsl:template match="para//xref/@linkend">     <xsl:attribute name="linkend">       <xsl:value-of select="concat(generate-id(ancestor::para[1]), '-', .)" />     </xsl:attribute>   </xsl:template> 

on system, using xsltproc (and wrapping example in root tag make formed), produces

<?xml version="1.0"?> <root> <chapter id="intro">   <title>introduction</title>   <para><figure id="idp1744-some-figure"><title>my figure...</title><mediaobject>...</mediaobject></figure>welcome our new product. 1 of    new features <xref linkend="idp1744-some-figure"/>.     other new features include ...   </para>   <para><figure id="idp2656-some-figure"><title>my figure...</title><mediaobject>...</mediaobject></figure>grab <xref linkend="idp2656-some-figure"/> , pull here too.</para> </chapter> <chapter>   <title>some other chapter</title>   <para>this chapter contains figure!    <figure id="some-figure"><title>my figure...</title><mediaobject>...</mediaobject></figure>    </para> </chapter> </root> 

the exact form of generated ids (the idpnnnn in example) vary processor processor they're guaranteed unique , consistent.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

visual studio - TFS will not accept changes I've made to a Java project -