Search code examples
xmlxslt-1.0adobe-indesign

XSLT 1.0 How to convert < > to Adobe InDesign namespace


InDesign supports special formatting attributes, which let me embed a reference to a paragraph, character, table, or cell style in an XML file. When the XML file is imported and laid out, InDesign uses the referenced style to format that element. But how to convert < and > etc. to aid namespace.

This is my input:

 <article>
    <para>&lt;em&gt;Eheniendel il &lt;strong&gt;evel&lt;/strong&gt;&lt;/em&gt;mos illanit atatur &lt;strong&gt;reptatiat&lt;/strong&gt;</para>
    <para>Os veles qui ne voluptaquam, quid qui &lt;strong&gt;rehendi&lt;/strong&gt;.</para>
    <para>Berchicide &lt;strong&gt;reperumet&lt;/strong&gt; ilicitatin &lt;em&gt;cus&lt;/em&gt;</para>
    <para>Dellabores ant. &lt;em&gt;Arte&lt;/em&gt; Dandae si &lt;strong&gt;rectur&lt;/strong&gt;?</para>
    <para>Am, voloribus doluptatem aut, &lt;em&gt;cor&lt;/em&gt; aut &lt;em&gt;conse&lt;/em&gt;</para>

I need such output:

<article>
    <para>
        <em aid:cstyle="italic">Eheniendel il <em aid:cstyle="bold-italic">evel</em>
        </em>mos illanit atatur <em aid:cstyle="bold">reptatiat</em>
    </para>
    <para>Os veles qui ne voluptaquam, quid qui <em aid:cstyle="bold">rehendi</em>.</para>
    <para>Berchicide <em aid:cstyle="bold">reperumet</em> ilicitatin <em aid:cstyle="italic">cus</em>
    </para>
    <para>Dellabores ant. <em aid:cstyle="italic">Arte</em> Dandae si <em aid:cstyle="bold">rectur</em>?</para>
    <para>Am, voloribus doluptatem aut, <em aid:cstyle="italic">cor</em> aut <em aid:cstyle="italic">conse</em>
    </para>
</article>

I need to convert:

&lt;em&gt; to <em aid:csytle = "italic">
&lt;strong&gt; to <em aid:csytle = "bold">
&lt;strong&gt;&lt;em&gt; or &lt;em&gt;&lt;strong&gt; to  <em aid:csytle = "bold-italic">

I made my xslt 1.0 stylesheet which works with normal xml elements like <em> end <strong>. How to improve it to my input?

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/"
xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/">

<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
</xsl:template>

<xsl:template match="/root">
    <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/"
        xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/">
        <xsl:apply-templates select="@* | node()"/>
    </root>
</xsl:template>

<xsl:template match="strong">
     <xsl:element name="em">  
        <xsl:attribute name="aid:cstyle">
            <xsl:value-of select="'bold'"/>
        </xsl:attribute>
     <xsl:apply-templates/>   
    </xsl:element>
</xsl:template>

<xsl:template match="em">
    <xsl:element name="em">  
        <xsl:attribute name="aid:cstyle">
            <xsl:value-of select="'italic'"/>
        </xsl:attribute>
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

<xsl:template match="em[../../strong]|strong[../../em]">
    <xsl:element name="em">
        <xsl:attribute name="aid:cstyle">
            <xsl:value-of select="'bold-italic'"/>
        </xsl:attribute>
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>


Solution

  • The easiest solution would be to perform two XSL transformations in series: the first transformation would do:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="para">
        <xsl:copy>
            <xsl:value-of select="." disable-output-escaping="yes"/>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    and save the result to a file. The second transformation would then apply your stylesheet to the resulting file.

    The alternative is a painful process of parsing the escaped XML using string functions in a recursive named template.