为从 filemaker 导出数据创建正确的 XSL 文件

create a correct XSL file for export data from filemaker

我在 Filemaker 中有一个数据库,我想使用此布局以 xml 格式导出 table:

<?xml version="1.0" encoding="UTF-8"?>
<ricette>
    <tipologia>Gelato al Latte</tipologia>
        <nome>Fior di Panna</nome>
            <ingrediente>BaseBiancaScirocco_2014 no condensato</ingrediente>
                <qta>1234</qta>
            <ingrediente>Panna Fresca 35%mg</ingrediente>
                <qta>1234</qta> 
            <ingrediente>Latte Intero Fresco AQ</ingrediente>
                <qta>1234</qta> 
    <tipologia>BaseGiallaScirocco</tipologia>
    ....
    ....
</ricette>

这是 filemaker 中的数据table:

http://postimg.org/image/hjoparu8b/

我创建了这个 XSL 导出文件:

<xsl:stylesheet 
    version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fm="http://www.filemaker.com/fmpxmlresult" 
    exclude-result-prefixes="fm">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

  <!-- expected columns -->
    <xsl:variable name="TYPO" select="1" />
    <xsl:variable name="NAME" select="2" />
    <xsl:variable name="INGREDIENT" select="3" />
    <xsl:variable name="QTY" select="4" />

    <xsl:template match="/">
    <ricette>
      <xsl:for-each select="fm:FMPXMLRESULT/fm:RESULTSET/fm:ROW">
          <tipologia><xsl:value-of select="fm:COL[$TYPO]/fm:DATA"/></tipologia>
          <nome><xsl:value-of select="fm:COL[$NAME]/fm:DATA"/></nome>
          <ingrediente><xsl:value-of select="fm:COL[$INGREDIENT]/fm:DATA"/></ingrediente>
          <qta><xsl:value-of select="fm:COL[$QTY]/fm:DATA"/></qta>
      </xsl:for-each> 
    </ricette>
    </xsl:template>
</xsl:stylesheet>

但结果是:

<?xml version="1.0" encoding="UTF-8"?>

<ricette>

<tipologia>Gelato al Latte</tipologia>

<nome>Fior di Panna</nome>

<ingrediente>BaseBiancaScirocco_2014 no condensato</ingrediente>

<qta>1234</qta>

<tipologia>Gelato al Latte</tipologia>

<nome>Fior di Panna</nome>

<ingrediente>Panna Fresca 35%mg</ingrediente>

<qta>1234</qta>

<tipologia>Gelato al Latte</tipologia>

<nome>Fior di Panna</nome>

<ingrediente>Latte Intero Fresco AQ</ingrediente>

<qta>1234</qta>

...
...
<ricette>

在结果文件中,行与行之间有很多 space,但没有制表符。

  1. What difference does it make?
  2. In order to have the result indented automatically, start your stylesheet with:

    <xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:fm="http://www.filemaker.com/fmpxmlresult" 
        xmlns:xalan="http://xml.apache.org/xalan"
        exclude-result-prefixes="fm xalan">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" xalan:indent-amount="4"/>
    

    However, this will not produce the result you show, because your indenting is not based on the actual hierarchy of the output, where tipologia, nome, ingrediente and qta are all siblings of each other.

    In order to get fake indents as shown in your expected output, you would have to disable automatic indenting and insert the required white space as literal text, for example:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fm="http://www.filemaker.com/fmpxmlresult" 
    exclude-result-prefixes="fm">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
    
    <!-- expected columns -->
    <xsl:variable name="TYPO" select="1" />
    <xsl:variable name="NAME" select="2" />
    <xsl:variable name="INGREDIENT" select="3" />
    <xsl:variable name="QTY" select="4" />
    
     <xsl:template match="/">
        <ricette>
            <xsl:text>&#10;</xsl:text>
            <xsl:for-each select="fm:FMPXMLRESULT/fm:RESULTSET/fm:ROW">
                <xsl:text>&#9;</xsl:text>
                <tipologia><xsl:value-of select="fm:COL[$TYPO]/fm:DATA"/></tipologia>
                <xsl:text>&#10;&#9;&#9;</xsl:text>
                <nome><xsl:value-of select="fm:COL[$NAME]/fm:DATA"/></nome>
                <xsl:text>&#10;&#9;&#9;&#9;</xsl:text>
                <ingrediente><xsl:value-of select="fm:COL[$INGREDIENT]/fm:DATA"/></ingrediente>
                <xsl:text>&#10;&#9;&#9;&#9;&#9;</xsl:text>
                <qta><xsl:value-of select="fm:COL[$QTY]/fm:DATA"/></qta>
                <xsl:text>&#10;</xsl:text>
            </xsl:for-each> 
        </ricette>
    </xsl:template>
    

  3. Note also that in your expected output, there seems to be grouping of ingredients by name - but your stylesheet does not do anything to achieve such grouping.

  4. I am not sure what cause the double returns; I'd suggest you make sure your stylesheet uses only LF or CR as line delimiters, not 两个都。 It could also be an artifact created by the application you are using to view the result (esp. if you're on Windows).