我需要从XML List-Item转换为Indesign xml代码

时间:2014-02-19 12:27:45

标签: xml xslt adobe-indesign

我需要从XML转换为Indesign xml代码。请建议任何人都知道答案。

输入XML

<root>
<p><list list-type="order">
<list-item><p>This is quick.</p></list-item>
<list-item><p>Sivakumar Given and the fact that
   <list list-type="bullet">
   <list-item><p>This is sublist</p></list-item>
   <list-item><p>This is sub middle list</p></list-item>
   <list-item><p>This is sub last list</p></list-item>
   </list>
</p></list-item>
<list-item><p>We are now left with four remaining parameters:</p></list-item>
<list-item><p>Given and the fact that</p></list-item>
<list-item><p>In Section we obtained an estimate for the mean of.</p></list-item>
<list-item><p>From steps 3, 4, and 5 above, we have generated (a table of) values for.</p></list-item>
</list></p>
</root>

必需的输出

<root>
<p><list list-type="order">
<list-item aid:pstyle="Order-First"><p>This is quick.</p></list-item>
<list-item aid:pstyle="Order-Middle"><p>Sivakumar Given and the fact that
   <list list-type="bullet">
   <list-item aid:pstyle="SubBullet-First"><p>This is sublist</p></list-item>
   <list-item aid:pstyle="SubBullet-Middle"><p>This is sub middle list</p></list-item>
   <list-item aid:pstyle="SubBullet-Last"><p>This is sub last list</p></list-item>
   </list>
</p></list-item>
<list-item aid:pstyle="Order-Middle"><p>We are now left with four remaining parameters:</p></list-item>
<list-item aid:pstyle="Order-Middle"><p>Given and the fact that</p></list-item>
<list-item aid:pstyle="Order-Middle"><p>In Section we obtained an estimate for the mean of.</p></list-item>
<list-item aid:pstyle="Order-Last"><p>From steps 3, 4, and 5 above, we have generated (a table of) values for.</p></list-item>
</list></p>

由于 Sivakumar M。

1 个答案:

答案 0 :(得分:1)

您可以使用此样式表。检查命名空间xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0"是否适合您的版本。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0"
    version="1.0">

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

    <xsl:template match="list-item[../@list-type='order']">
        <xsl:copy>
            <xsl:call-template name="add-attribute-to-list-item">
                <xsl:with-param name="type" select="'Order'"/>
            </xsl:call-template>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="list-item[../@list-type='bullet']">
        <xsl:copy>
            <xsl:call-template name="add-attribute-to-list-item">
                <xsl:with-param name="type" select="'SubBullet'"/>
            </xsl:call-template>
            <xsl:apply-templates />
        </xsl:copy>
    </xsl:template>

    <xsl:template name="add-attribute-to-list-item">
        <xsl:param name="type"/>
            <xsl:attribute name="aid:pstyle">
                <xsl:choose>
                    <xsl:when test="position() = 1">
                        <xsl:value-of select="concat($type,'-First')"/>
                    </xsl:when>
                    <xsl:when test="position() = last()">
                        <xsl:value-of select="concat($type,'-Last')"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="concat($type,'-Middle')"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:attribute>
    </xsl:template>

    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/">
        <root><xsl:apply-templates select="root/p"/></root>
    </xsl:template>

</xsl:stylesheet>

结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0">
   <p>
      <list list-type="order">
         <list-item aid:pstyle="Order-First">
            <p>This is quick.</p>
         </list-item>
         <list-item aid:pstyle="Order-Middle">
            <p>Sivakumar Given and the fact that
            <list list-type="bullet">
                  <list-item aid:pstyle="SubBullet-First">
                     <p>This is sublist</p>
                  </list-item>
                  <list-item aid:pstyle="SubBullet-Middle">
                     <p>This is sub middle list</p>
                  </list-item>
                  <list-item aid:pstyle="SubBullet-Last">
                     <p>This is sub last list</p>
                  </list-item>
               </list>
            </p>
         </list-item>
         <list-item aid:pstyle="Order-Middle">
            <p>We are now left with four remaining parameters:</p>
         </list-item>
         <list-item aid:pstyle="Order-Middle">
            <p>Given and the fact that</p>
         </list-item>
         <list-item aid:pstyle="Order-Middle">
            <p>In Section we obtained an estimate for the mean of.</p>
         </list-item>
         <list-item aid:pstyle="Order-Last">
            <p>From steps 3, 4, and 5 above, we have generated (a table of) values for.</p>
         </list-item>
      </list>
   </p>
</root>