【问题标题】:XSLT 1.0 How to convert &lt; &gt; to Adobe InDesign namespaceXSLT 1.0 如何将 < > 转换为 Adob​​e InDesign 命名空间
【发布时间】:2016-01-17 06:57:39
【问题描述】:

InDesign 支持特殊的格式属性,让我可以在 XML 文件中嵌入对段落、字符、表格或单元格样式的引用。导入和布局 XML 文件时,InDesign 使用引用的样式来设置该元素的格式。但是如何将&amp;lt;&amp;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 元素,如 &lt;em&gt; end &lt;strong&gt;。如何根据我的输入改进它?

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

【问题讨论】:

    标签: xml xslt-1.0 adobe-indesign


    【解决方案1】:

    最简单的解决方案是依次执行两个 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,这是一个痛苦的过程。

    【讨论】:

    • 是的,这是最简单的方法,但我仍在寻找不需要外部解析器的解决方案。正如我所见,InDesign 解析器忽略了“禁用输出转义”功能。要完成这两个步骤,我必须使用外部解析器或 xml 编辑器。
    • @Robert "寻找不需要外部解析器的解决方案。" 另一种方法是尝试自己解析 XML,使用一个完全不合适的工具。我强烈建议不要这样做。你在哪个操作系统平台上?
    • 我在 Mac OSX 上,我使用 Parallels Desktop 让 Windows Excel 与 XML。我在 InDesign 工作,这是我的职业。但现在我必须准备 Indesign 模板并将 Excel 源转换为有效且格式正确的 XML 到这些模板。我开始在 Sublime 中使用 grep 来准备正确的 xml,但现在我看到唯一的方法是编写 XLST 样式表并在导入到 InDesign 时做所有事情。这就是我努力学习的原因。
    • "我在 Mac OSX 上" 我建议你使用 AppleScript 来自动化预处理。
    • MAC OSX 中内置了一个 XSLT 处理器 - xsltproc。我认为,预处理是一种前进的方式,但它不一定是 XSLT,您可能可以使用带有 sed 甚至 tr 和字符串替换的简单 shell 脚本摆脱困境。您可以使用 AppleScript 甚至 ExtendScript 并从 InDesign 内部触发
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 2018-02-17
    • 2020-11-13
    • 2011-01-19
    • 2010-12-16
    • 1970-01-01
    相关资源
    最近更新 更多