XSLT 1.0 如何将 < > 转换为 Adob​​e InDesign 命名空间

XSLT 1.0 How to convert &lt; &gt; to Adobe InDesign namespace

InDesign 支持特殊格式属性,这让我可以在 XML 文件中嵌入对段落、字符、table 或单元格样式的引用。当 XML 文件被导入和布局时,InDesign 使用引用的样式来格式化该元素。但是如何转换 &lt;&gt; 等来帮助命名空间。

这是我的输入:

 <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>

我需要这样的输出:

<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>

我需要转换:

&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">

我制作了我的 xslt 1.0 样式表,它适用于正常的 xml 元素,例如 <em> end <strong>。如何改进它以适应我的输入?

<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>

最简单的解决方案是连续执行两个 XSL 转换:第一个转换将执行:

<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>

并将结果保存到文件中。然后,第二个转换会将您的样式表应用于生成的文件。

另一种方法是在递归命名模板中使用字符串函数解析转义 XML 的痛苦过程。