Filemaker 13使用XSLT导出格式

时间:2014-09-08 16:07:23

标签: xml xslt filemaker

我将我的数据库从Filemaker Pro 13导出到FMPDSORESULT语法中的XML,并尝试创建一个XSLT文件,以将XML导出格式化为我需要的特定结构。我已经为记录的简单初始单字符串参数做了这个。但现在我的数据略有不同。

以下是FMPDSORESULT Grammar中的完整一个记录样本,其中包含参数:

<?xml version="1.0" encoding="UTF-8" ?><!-- This grammar has been deprecated - use FMPXMLRESULT instead -->
<FMPDSORESULT xmlns="http://www.filemaker.com/fmpdsoresult">
    <ERRORCODE>0</ERRORCODE>
    <DATABASE></DATABASE>
    <LAYOUT></LAYOUT>
    <ROW MODID="178" RECORDID="6">
        <ID>535</ID>
        <Design_Name_Original>G001_Folder</Design_Name_Original>
        <Material_Type>Folding Carton</Material_Type>
        <Usage>Packaging</Usage>
        <Design_Group_Original>Folders (Group G)</Design_Group_Original>
        <Display_Order>
            <DATA>2</DATA>
            <DATA>3</DATA>
            <DATA>4</DATA>
            <DATA>5</DATA>
        </Display_Order>
        <Parameter_Name>
            <DATA>Allowance</DATA>
            <DATA>Length</DATA>
            <DATA>Width</DATA>
            <DATA>Depth</DATA>
        </Parameter_Name>
        <Parameter_Datatype>
            <DATA>Numeric Material Thickness</DATA>
            <DATA>Numeric Distance - LWD</DATA>
            <DATA>Numeric Distance - LWD</DATA>
            <DATA>Numeric Distance - LWD</DATA>
        </Parameter_Datatype>
        <Default_Inches>
            <DATA>1/8</DATA>
            <DATA>7</DATA><DATA>7</DATA>
            <DATA>2</DATA>
        </Default_Inches>
        <Default_Metric>
            <DATA>3</DATA>
            <DATA>175</DATA>
            <DATA>175</DATA>
            <DATA>50</DATA>
        </Default_Metric>
        <Min_for_Inch>
            <DATA>1/64</DATA>
            <DATA>1/32</DATA>
            <DATA>1/32</DATA>
            <DATA>1/32</DATA>
        </Min_for_Inch>
        <Min_for_mm>
            <DATA>,5</DATA>
            <DATA>1</DATA>
            <DATA>1</DATA>
            <DATA>1</DATA>
        </Min_for_mm>
        <Max_for_Inch>
            <DATA>3/4</DATA>
            <DATA>200</DATA>
            <DATA>200</DATA>
            <DATA>200</DATA>
        </Max_for_Inch>
        <Max_for_mm>
            <DATA>15</DATA>
            <DATA>5000</DATA>
            <DATA>5000</DATA>
            <DATA>5000</DATA>
        </Max_for_mm>
    </ROW>
</FMPDSORESULT>

我需要这个结构:

<Designs>
    <Design>
    <ID>535</ID>
    <Name>G001_Folder</Name>
    <Category>Folders (Group G)</Category>
    <Parameters>
        <Parameter>
            <ParamID>2</ParamID>
            <ParamName>Allowance</ParamName>
            <ParamDatatype>Numeric Material Thickness</ParamDatatype>
            <DefaultInches>1/8</DefaultInches>
            <DefaultMM>3</DefaultMM>
            <MinInches>1/64</MinInches>
            <MinMM>,5</MinMM>
            <MaxInches>3/4</MaxInches>
            <MaxMM>15</MaxMM>
        </Parameter>
        <Parameter>
            <ParamID>3</ParamID>
            <ParamName>Length</ParamName>
            <ParamDatatype>Numeric Distance - LWD</ParamDatatype>
            <DefaultInches>7</DefaultInches>
            <DefaultMM>175</DefaultMM>
            <MinInches>1/32</MinInches>
            <MinMM>1</MinMM>
            <MaxInches>200</MaxInches>
            <MaxMM>5000</MaxMM>
        </Parameter>
        <Parameter>
            <ParamID>4</ParamID>
            <ParamName>Width</ParamName>
            <ParamDatatype>Numeric Distance - LWD</ParamDatatype>
            <DefaultInches>7</DefaultInches>
            <DefaultMM>175</DefaultMM>
            <MinInches>1/32</MinInches>
            <MinMM>1</MinMM>
            <MaxInches>200</MaxInches>
            <MaxMM>5000</MaxMM>
        </Parameter>
        <Parameter>
            <ParamID>5</ParamID>
            <ParamName>Depth</ParamName>
            <ParamDatatype>Numeric Distance - LWD</ParamDatatype>
            <DefaultInches>2</DefaultInches>
            <DefaultMM>50</DefaultMM>
            <MinInches>1/32</MinInches>
            <MinMM>1</MinMM>
            <MaxInches>200</MaxInches>
            <MaxMM>5000</MaxMM>
        </Parameter>
    </Parameters>
</Design>
</Designs>

我已经编写了这段代码:

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

  <xsl:variable name="rename">
    <item from="ID" to="ID" />
    <item from="Design_Name_Original" to="Name" />
    <item from="Material_Type" to="Material" />
    <item from="Usage" to="Usage" />
    <item from="Design_Group_Original" to="Category" />
    <item from="Created" to="Created" />
    <item from="Modified" to="Modified" />
    <item from="Display_Order" to="ParamID" />
    <item from="Parameter_Name" to="ParamName" />
    <item from="Parameter_Datatype" to="ParamDatatype" />
    <item from="Default_Inches" to="DefaultInches" />
    <item from="Default_Metric" to="DefaultMM" />
    <item from="Min_for_Inch" to="MinInches" />
    <item from="Min_for_mm" to="MinMM" />
    <item from="Max_for_Inch" to="MaxInches" />
    <item from="Max_for_mm" to="MaxMM" />
  </xsl:variable>

  <xsl:template match="/*">
    <Designs>
      <xsl:apply-templates select="fm:ROW"/>
    </Designs>
  </xsl:template>

  <xsl:template match="fm:ROW">
    <Design>
        <xsl:apply-templates 
         select="fm:ID | fm:Design_Name_Original | fm:Material_Type | fm:Usage | fm:Design_Group_Original | fm:Created | fm:Modified" mode="rename" />
        <Parameters>
          <Parameter>
            <xsl:apply-templates 
            select="fm:Display_Order" mode="rename" />
          </Parameter>   
        </Parameters>
    </Design>
  </xsl:template>

  <xsl:template match="*" mode="rename">
    <xsl:element name="{document('')//xsl:variable[@name = 'rename']/item[@from = local-name(current())]/@to}">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

得到了这个结果:

<?xml version="1.0" encoding="UTF-8"?>
<Designs>
<Design>
<ID>535</ID>
<Name>G001_Folder</Name>
<Material>Folding Carton</Material>
<Usage>Packaging</Usage>
<Category>Folders (Group G)</Category>
<Created>04.09.2014 10:20:30</Created>
<Modified>04.09.2014 16:58:16</Modified>
<Parameters>
<Parameter>
<ParamID>2345</ParamID>
<ParamName>AllowanceLengthWidthDepth</ParamName>
<ParamDatatype>Numeric Material ThicknessNumeric Distance - LWDNumeric Distance - LWDNumeric Distance - LWD</ParamDatatype>
<DefaultInches>1/8772</DefaultInches>
<DefaultMM>317517550</DefaultMM>
<MinInches>1/641/321/321/32</MinInches>
<MinMM>,5111</MinMM>
<MaxInches>3/4200200200</MaxInches>
<MaxMM>15500050005000</MaxMM>
</Parameter>
</Parameters>
<Prices>
<Currency/>
<Type/>
<Price/>
</Prices>
</Design>
</Designs>

如何传播这些参数以获得我的结构?感谢。

UPD。我添加了完整版的代码。

1 个答案:

答案 0 :(得分:0)

我相信你通过使用重复字段(如果这是导致双重DATA元素的原因)使这个(以及许多其他事情)变得更加困难。另请注意,如果您决定重命名字段,则使用DSO语法会咬你。

尽管如此,请尝试以下样式表:

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

<xsl:template match="/">
    <Designs>
        <xsl:for-each select="dso:FMPDSORESULT/dso:ROW">
            <Design>
                <ID><xsl:value-of select="dso:ID" /></ID>
                <Name><xsl:value-of select="dso:Design_Name_Original" /></Name>
                <Category><xsl:value-of select="dso:Design_Group_Original" /></Category>
                <Parameters>
                    <xsl:for-each select="dso:Display_Order/dso:DATA">
                        <xsl:variable name="i" select="position()" />
                        <Parameter>
                            <ParamID><xsl:value-of select="." /></ParamID>

                            <ParamName><xsl:value-of select="../../dso:Parameter_Name/dso:DATA[$i]" /></ParamName>
                            <ParamDatatype><xsl:value-of select="../../dso:Parameter_Datatype/dso:DATA[$i]" /></ParamDatatype>
                            <!-- more of the same here -->

                        </Parameter>
                    </xsl:for-each>
                </Parameters>
            </Design>
        </xsl:for-each>
    </Designs>
</xsl:template>

</xsl:stylesheet>

请注意,您的源只有一条记录,因此这里包含了一些关于正确分解的猜测。