2015-10-19 37 views
0

我無法將XML項目列表轉換爲獨立表格。一個問題是計算行數,因爲這是必需的,但不能作爲簡單的count()處理。使用XSLT將動態XML轉換爲Adobe InDesign表

InDesign的表應該看起來像:

 
+------------+-------------+------------+ 
|ARTIKELNR |BEZEICHNUNG |VK1BRUTTO | 
+------------+-------------+------------+ 
|   |TEXT1      | 
+------------+--------------------------+ 
... 

的XML看起來像:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?> 
<kategorie> 
    <artikel> 
<ARTIKELNR>    200151</ARTIKELNR> 
<BEZEICHNUNG>ARTIST ONE - the first release</BEZEICHNUNG> 
<TEXT1></TEXT1> 
<VK1BRUTTO>13,9900</VK1BRUTTO> 
    </artikel> 
    <artikel> 
<ARTIKELNR>    200153</ARTIKELNR> 
<BEZEICHNUNG>ARTIST TWO - the second release</BEZEICHNUNG> 
<TEXT1>Lorem ipsum dolor whatever...</TEXT1> 
<VK1BRUTTO>13,9900</VK1BRUTTO> 
    </artikel> 
.... 

正如你可以看到有時TEXT1是空的。在這種情況下:這個項目沒有第二行。

Therfore我寫下這個(InDesign中)XSLT:

<?xml version="1.0" encoding="iso-8859-1"?> 
<xsl:stylesheet xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/" exclude-result-prefixes="xs"> 
<xsl:output method="xml" encoding="utf-8" indent="no"/> 
<xsl:strip-space elements="ARTIKELNR"/> 
<xsl:template name="rowcount"> 
    <xsl:param name="rows"/> 
    <xsl:for-each select="article"> 
     <xsl:if test="TEXT1 != ''"> 
      <xsl:call-template name="rowcount"> 
      <xsl:with-param name="rows" select="$rows + 1" /> 
      </xsl:call-template> 
     </xsl:if> 
    </xsl:for-each> 
</xsl:template> 
<xsl:template match="/kategorie"> 
<xsl:variable name="rows"><xsl:call-template name="rowcount"/> 
/xsl:variable> 
<Table aid:tcols="3" aid:trows="$rows" aid:table="table" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/"> 

    <xsl:for-each select="artikel"> 
     <cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="25.30pt" aid:pstyle="abs_Produkt_Artikelnummer" aid:cstyle="zei_Produkt_Artikelnummer" aid5:cellstyle="zel_Produkt_Artikelnummer" aid:theader=""> 
      <xsl:value-of select="substring(ARTIKELNR, string-length(ARTIKELNR) - 6)"/> 
     </cell> 
     <cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="136.76pt" aid:pstyle="abs_Produkt_Bezeichnung" aid:cstyle="zei_Produkt_Bezeichnung" aid5:cellstyle="zel_Produkt_Bezeichnung" aid:theader=""> 
      <xsl:value-of select="BEZEICHNUNG"/> 
     </cell> 
     <cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:ccolwidth="21.55pt" aid:pstyle="abs_Produkt_VKBrutto" aid:cstyle="zei_Produkt_VKBrutto" aid5:cellstyle="zel_Produkt_VKBrutto" aid:theader=""> 
      <xsl:value-of select="substring(VK1BRUTTO, string-length(VK1BRUTTO), string-length(VK1BRUTTO)-2)"/> 
     </cell> 
     <xsl:variable name="freitext" select="TEXT1"/> 
     <xsl:if test="$freitext != ''"> 
      <cell aid:table="cell" aid:crows="1" aid:ccols="1" aid:pstyle="abs__empty" aid:cstyle="zei__empty" aid5:cellstyle="zel__empty" aid:theader=""> 
      </cell> 
      <cell aid:table="cell" aid:crows="1" aid:ccols="2" aid:pstyle="abs_Produkt_Beschreibung" aid:cstyle="zei_Produkt_Beschreibung" aid5:cellstyle="zel_Produkt_Beschreibung" aid:theader=""> 
       <xsl:value-of select="TEXT1"/> 
      </cell> 
     </xsl:if> 
    </xsl:for-each> 

</Table> 
</xsl:template> 
</xsl:stylesheet> 

失敗: 1.計算不正確的行數。 2. InDesign不會根據結果生成表格。

有人可以解釋有什麼問題,也許給我一個正確的代碼?

+0

您可以發佈轉換的預期XML結果嗎? –

回答

0

如果我猜測正確,你想要做這樣的事情(省略非相關屬性):

XSLT 1.0

<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" version="1.0" encoding="UTF-8" indent="yes"/> 

<xsl:template match="/kategorie"> 
    <Table aid:tcols="3" aid:trows="{count(artikel) + count(artikel/TEXT1[text()])}"> 
     <xsl:for-each select="artikel"> 
      <cell> 
       <xsl:value-of select="normalize-space(ARTIKELNR)"/> 
      </cell> 
      <cell> 
       <xsl:value-of select="BEZEICHNUNG"/> 
      </cell> 
      <cell> 
       <xsl:value-of select="VK1BRUTTO"/> 
      </cell> 
      <xsl:if test="TEXT1/text()"> 
       <cell/> 
       <cell> 
        <xsl:value-of select="TEXT1"/> 
       </cell> 
       <cell/> 
      </xsl:if> 
     </xsl:for-each> 
    </Table> 
</xsl:template> 

</xsl:stylesheet> 

應用到你的輸入例子,其結果將是:

<?xml version="1.0" encoding="UTF-8"?> 
<Table xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/" aid:tcols="3" aid:trows="3"> 
    <cell>200151</cell> 
    <cell>ARTIST ONE - the first release</cell> 
    <cell>13,9900</cell> 
    <cell>200153</cell> 
    <cell>ARTIST TWO - the second release</cell> 
    <cell>13,9900</cell> 
    <cell/> 
    <cell>Lorem ipsum dolor whatever...</cell> 
    <cell/> 
</Table> 
0

謝謝。隨着一些「神奇」,我做到了:

<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" version="1.0" encoding="ISO-8859-1" indent="yes"/> 
<xsl:template match="/kategorie"> 
<Produkte> 
<Tabelle aid:table="table" aid:tcols="3" aid:trows="{count(artikel) + count(artikel/TEXT1[text()])}" xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" xmlns:aid5="http://ns.adobe.com/AdobeInDesign/5.0/"> 

     <xsl:for-each select="artikel"> 

      <cell aid:table="cell" aid:ccolwidth="25.30" aid:pstyle="abs_Produkt_Artikelnummer" aid:cstyle="zei_Produkt_Artikelnummer" aid5:cellstyle="zel_Produkt_Artikelnummer" > 
       <xsl:value-of select="normalize-space(ARTIKELNR)"/> 
      </cell> 
      <cell aid:table="cell" aid:ccolwidth="136.76" aid:pstyle="abs_Produkt_Bezeichnung" aid:cstyle="zei_Produkt_Bezeichnung" aid5:cellstyle="zel_Produkt_Bezeichnung"> 
       <xsl:value-of select="BEZEICHNUNG"/> 
      </cell> 
      <cell aid:table="cell" aid:ccolwidth="21.55" aid:pstyle="abs_Produkt_VKBrutto" aid:cstyle="zei_Produkt_VKBrutto" aid5:cellstyle="zel_Produkt_VKBrutto"> 
       <xsl:value-of select="substring(VK1BRUTTO, 0, string-length(VK1BRUTTO)-1)"/> 
      </cell> 
      <xsl:if test="TEXT1/text()"> 
       <cell aid:table="cell" aid:ccolwidth="25.30" aid:cstyle="zei_Produkt" aid5:cellstyle="zel_Produkt"></cell> 
       <cell aid:table="cell" aid:ccolwidth="158.31" aid:ccols="2" aid:pstyle="abs_Produkt_Beschreibung" aid:cstyle="zei_Produkt_Beschreibung" aid5:cellstyle="zel_Produkt_Beschreibung"> 
        <xsl:value-of select="TEXT1"/> 
       </cell> 
      </xsl:if> 

     </xsl:for-each> 

    </Tabelle> 
</Produkte> 
</xsl:template> 
</xsl:stylesheet> 

提示:要能夠把在InDesign表,必須有一個僞對象(即「PRODUKTE」)圍着桌子。

Johannes