XML / XSLT - 无法计算唯一的子节点

时间:2017-01-17 21:23:30

标签: xml xslt

我需要使用XSL代码转换我的XML文件,以便将内容导入InDesign。不幸的是,我在计算其中一个组的子节点时遇到了问题。

以下是XML代码:

<?xml version="1.0"?>
<records>
   <list>
      <!-- SPECIAL NOTE -->
      <specialNote>
         <specialNoteTitle>Title</specialNoteTitle>
         <specialNoteContent>Special note content</specialNoteContent>
      </specialNote>
      <!-- TITLE & ADDRESS -->
      <address>
         <listTitle>December</listTitle>
         <addressLines>
            <line1>1</line1>
            <line2>2</line2>
            <line3>3</line3>
            <line4>4</line4>
         </addressLines>
      </address>
      <!-- PRODUCTS -->
      <products>
         <productNr1>
            <productCode>Jeden</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr1>
         <productNr2>
            <productCode>1</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr2>
         <productNr3>
            <productCode>1</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr3>
         <productNr4>
            <productCode>1</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr4>
         <productNr5>
            <productCode>1</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr5>
         <productNr6>
            <productCode>1</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr6>
         <productNr7>
            <productCode>1</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr7>
         <productNr8>
            <productCode>1</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr8>
      </products>
      <!-- CODES -->
      <promoCodes>
         <promoCode1>
            <promoCode>22</promoCode>
            <instruction>22</instruction>
         </promoCode1>
         <promoCode2>
            <promoCode>21</promoCode>
            <instruction>23</instruction>
         </promoCode2>
         <promoCode3>
            <promoCode>12</promoCode>
            <instruction>12</instruction>
         </promoCode3>
         <promoCode4>
            <promoCode>22</promoCode>
            <instruction>11</instruction>
         </promoCode4>
      </promoCodes>
   </list>
   <list>
      <!-- SPECIAL NOTE -->
      <specialNote>
         <specialNoteTitle>Title</specialNoteTitle>
         <specialNoteContent>Special note content</specialNoteContent>
      </specialNote>
      <!-- TITLE & ADDRESS -->
      <address>
         <listTitle>December</listTitle>
         <addressLines>
            <line1>1</line1>
            <line2>2</line2>
            <line3>3</line3>
            <line4>4</line4>
         </addressLines>
      </address>
      <!-- PRODUCTS -->
      <products>
         <productNr1>
            <productCode>Jeden</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr1>
         <productNr2>
            <productCode>1</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr2>
         <productNr3>
            <productCode>1</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr3>
         <productNr4>
            <productCode>1</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr4>
         <productNr5>
            <productCode>1</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr5>
         <productNr6>
            <productCode>1</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr6>
         <productNr7>
            <productCode>1</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr7>
         <productNr8>
            <productCode>1</productCode>
            <productName>1</productName>
            <productBrand>1</productBrand>
            <productNK>1</productNK>
         </productNr8>
      </products>
      <!-- CODES -->
      <promoCodes>
         <promoCode1>
            <promoCode>22</promoCode>
            <instruction>22</instruction>
         </promoCode1>
         <promoCode2>
            <promoCode>21</promoCode>
            <instruction>23</instruction>
         </promoCode2>
         <promoCode3>
            <promoCode>12</promoCode>
            <instruction>12</instruction>
         </promoCode3>
         <promoCode4>
            <promoCode>22</promoCode>
            <instruction>11</instruction>
         </promoCode4>
      </promoCodes>
   </list>
</records>

我的XSL代码:

<?xml version="1.0" encoding="UTF-8"?>
<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">

   <!-- MAIN TREE -->
   <xsl:template match="/">
      <records>
      <xsl:apply-templates/>
      </records>
   </xsl:template>

   <xsl:template match="list">
      <list>
      <xsl:apply-templates select="specialNote"/>
        <xsl:apply-templates select="address"/>
        <xsl:apply-templates select="products"/>
        <xsl:apply-templates select="promoCodes"/> 
      </list>
   </xsl:template>

   <!-- SPECIAL NOTE TEMPLATE -->
   <xsl:template match="specialNote">
      <break aid:pstyle="break">
         <xsl:if test="*">
            <table aid:table="table" aid:trows="2" aid:tcols="1">
               <cell aid:table="cell" aid5:cellstyle="special_note_title" aid:crows="1" aid:ccols="1" aid:ccolwidth="362"><xsl:value-of select="specialNoteTitle" /></cell>
               <cell aid:table="cell" aid5:cellstyle="special_note_content" aid:crows="1" aid:ccols="1" aid:ccolwidth="362"><xsl:value-of select="specialNoteContent" /></cell>
            </table>
         </xsl:if>
      </break>
      <xsl:text>&#xa;</xsl:text>
   </xsl:template>

   <!-- ADDRESS TEMPLATE -->
   <xsl:template match="address">
      <table aid:table="table" aid:trows="4" aid:tcols="2">
         <cell aid:table="cell" aid5:cellstyle="address_title" aid:crows="4" aid:ccols="1" aid:ccolwidth="181"><xsl:value-of select="listTitle" /></cell>
         <xsl:for-each select="addressLines/*">
            <cell aid:table="cell" aid5:cellstyle="address_line" aid:crows="1" aid:ccols="1" aid:ccolwidth="181"><xsl:value-of select="." /></cell>
         </xsl:for-each>      
      </table>
      <xsl:text>&#xa;</xsl:text>
   </xsl:template>

   <!-- PRODUCTS TEMPLATE -->
   <xsl:template match="products">
      <xsl:variable name="products_number" select="count(./*)+1" />
      <table aid:table="table" aid:trows="$products_number" aid:tcols="4">
         <cell aid:table="cell" aid5:cellstyle="table_head" aid:crows="1" aid:ccols="1" aid:ccolwidth="61">Code</cell>
         <cell aid:table="cell" aid5:cellstyle="table_head" aid:crows="1" aid:ccols="1" aid:ccolwidth="120">Product name</cell>
         <cell aid:table="cell" aid5:cellstyle="table_head" aid:crows="1" aid:ccols="1" aid:ccolwidth="120">Product brand</cell>
         <cell aid:table="cell" aid5:cellstyle="table_head" aid:crows="1" aid:ccols="1" aid:ccolwidth="61">NK</cell>
         <xsl:for-each select="*">
            <cell aid:table="cell" aid5:cellstyle="table_content" aid:crows="1" aid:ccols="1" aid:ccolwidth="61"><xsl:value-of select="productCode" /></cell>
            <cell aid:table="cell" aid5:cellstyle="table_content" aid:crows="1" aid:ccols="1" aid:ccolwidth="120"><xsl:value-of select="productName" /></cell>
            <cell aid:table="cell" aid5:cellstyle="table_content" aid:crows="1" aid:ccols="1" aid:ccolwidth="120"><xsl:value-of select="productBrand" /></cell>
            <cell aid:table="cell" aid5:cellstyle="table_content" aid:crows="1" aid:ccols="1" aid:ccolwidth="61"><xsl:value-of select="productNK" /></cell>
         </xsl:for-each>      
      </table>
      <xsl:text>&#xa;</xsl:text>
   </xsl:template>

   <!-- PROMO CODES TEMPLATE -->
   <xsl:template match="promoCodes">
      <table aid:table="table" aid:trows="5" aid:tcols="2">
         <cell aid:table="cell" aid5:cellstyle="table_head" aid:crows="1" aid:ccols="1" aid:ccolwidth="62">Codes</cell>
         <cell aid:table="cell" aid5:cellstyle="table_head" aid:crows="1" aid:ccols="1" aid:ccolwidth="300">Instruction</cell>
         <xsl:for-each select="*">
            <cell aid:table="cell" aid5:cellstyle="table_content" aid:crows="1" aid:ccols="1" aid:ccolwidth="62"><xsl:value-of select="promoCode" /></cell>
            <cell aid:table="cell" aid5:cellstyle="table_content" aid:crows="1" aid:ccols="1" aid:ccolwidth="300"><xsl:value-of select="instruction" /></cell>
         </xsl:for-each>
      </table>
      <xsl:text>&#xa;</xsl:text>
   </xsl:template>

</xsl:stylesheet>

问题可以在产品模板中找到。您可能已经看到,我的XML代码非常复杂。有许多独特的节点,如ProductNr1,ProductNr2等。不幸的是,这些节点不能被一个标签(即“产品”)替换,因为XML是从Excel导出的(在我的情况下,它每列需要唯一的标签)。为了构建InDesign的表,我需要计算它的行​​数(“products”的所有子项+ 1)。我的XPATH应该这样做,但是当我使用它时,InDesign拒绝导入XML。我假设这是因为计算的products表的行数不正确。

我做错了什么?

2 个答案:

答案 0 :(得分:0)

我可以看到一个问题,但它可能不是导致该问题的唯一问题,因为我不知道您期望的XML是什么样的。但是,问题在于如何为表

设置trows属性
aid:trows="$products_number"

这将输出你输入的内容,实际上你想要评估变量$products_number,并使用变量的结果。为此,请使用Attribute Value Templates

aid:trows="{$products_number}"

输出XML时,应将其输出为aid:trows="9",例如。

答案 1 :(得分:0)

蒂姆C比我快,但我也想说同样的话。

此外,您可以优化您的产品&#34;通过查看姓名()以&#39; productNr&#39;

开头的任何孩子进行搜索

我发布了产品的xsl模板以供解释:

&#13;
&#13;
<!-- PRODUCTS TEMPLATE -->
    <xsl:template match="products">
        <xsl:variable name="products" select="./*[starts-with(name(), 'productNr')]"/>
        <xsl:variable name="products_number" select="count($products)+1" />
        <table xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/" aid:table="table" aid:trows="{$products_number}" aid:tcols="4">
            <cell aid:table="cell" aid5:cellstyle="table_head" aid:crows="1" aid:ccols="1" aid:ccolwidth="61">Code</cell>
            <cell aid:table="cell" aid5:cellstyle="table_head" aid:crows="1" aid:ccols="1" aid:ccolwidth="120">Product name</cell>
            <cell aid:table="cell" aid5:cellstyle="table_head" aid:crows="1" aid:ccols="1" aid:ccolwidth="120">Product brand</cell>
            <cell aid:table="cell" aid5:cellstyle="table_head" aid:crows="1" aid:ccols="1" aid:ccolwidth="61">NK</cell>
            <xsl:for-each select="$products">
                <cell aid:table="cell" aid5:cellstyle="table_content" aid:crows="1" aid:ccols="1" aid:ccolwidth="61"><xsl:value-of select="productCode" /></cell>
                <cell aid:table="cell" aid5:cellstyle="table_content" aid:crows="1" aid:ccols="1" aid:ccolwidth="120"><xsl:value-of select="productName" /></cell>
                <cell aid:table="cell" aid5:cellstyle="table_content" aid:crows="1" aid:ccols="1" aid:ccolwidth="120"><xsl:value-of select="productBrand" /></cell>
                <cell aid:table="cell" aid5:cellstyle="table_content" aid:crows="1" aid:ccols="1" aid:ccolwidth="61"><xsl:value-of select="productNK" /></cell>
            </xsl:for-each>      
        </table>
        <xsl:text>&#xa;</xsl:text>
    </xsl:template>
&#13;
&#13;
&#13;