2014-02-19 31 views
1

我需要从XML转换为Indesign xml代码。请建议任何人知道答案。我需要从XML List-Item转换为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> 

感谢 西瓦库玛M.

+0

我的答案是XSLT。或XQuery。或者使用标准API来手动编写解码/操纵/序列化XML的代码。但是:您的建议输出已损坏;如果您希望文档被现代XML工具接受,您必须为'aid:'前缀提供一个名称空间绑定。 – keshlam

回答

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

嗨,你的答案工作正常,非常感谢你! –

+0

@SivakumarM请接受此答案(在左侧标记勾号,使其显示为绿色)。这是在Stackoverflow上说“谢谢”的礼貌方式。 –