创建XSL样式表以管理FileMaker Pro XML输出

时间:2013-01-22 21:29:46

标签: xml xslt filemaker adobe-indesign

我使用XML将数据提供给InDesign模板,并且正在从较旧的,更简单的设置(Excel电子表格中的映射单元格)切换到FileMaker Pro。 FileMaker Pro以与InDesign文档中已建立的XML结构略有不同的格式导出XML。

从FileMaker Pro导出XML时,我可以选择使用XSL样式表,但我不知道如何创建一个(并且我发现的教程看起来很模糊,或者假设我已经知道了我不知道。

FileMaker Pro导出如下所示:

<?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>Projects.fmp12</DATABASE>
<LAYOUT></LAYOUT>
<ROW MODID="1" RECORDID="2">
<Project_name></Project_name>
<City_1></City_1>
<Size></Size>
<Building></Building>
<City_2></City_2>
<Completion></Completion>
<Scope_of_work></Scope_of_work>
<Description></Description>
</ROW>

我需要编写样式表,以便导出的XML与每个记录的结构匹配:

<project>
    <project_title>
        <name></name>
        <city_1></city_1>
    </project_title>
    <project_information>
        <size></size>
        <building></building>
        <city_2></city_2>
        <completion></completion>
        <scope_of_work></scope_of_work>
    </project_information>
    <description></description>
</project>

非常感谢任何建议。

1 个答案:

答案 0 :(得分:5)

您没有指定结果XML的外部节点是什么样的,或者它是否使用命名空间,但是如何:

<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="Project_name" to="name" />
    <item from="City_1" to="city_1" />
    <item from="Size" to="size" />
    <item from="Building" to="building" />
    <item from="City_2" to="city_2" />
    <item from="Completion" to="completion" />
    <item from="Scope_of_work" to="scope_of_work" />
    <item from="Description" to="description" />
  </xsl:variable>

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

  <xsl:template match="fm:ROW">
    <project>
      <project_title>
        <xsl:apply-templates select="fm:Project_name | fm:City_1" mode="rename" />
      </project_title>
      <project_information>
        <xsl:apply-templates 
         select="fm:size | fm:Building | fm:City_2 | fm:Completion | fm:Scope_of_work"
         mode="rename" />
      </project_information>
      <xsl:apply-templates select="fm:Description" mode="rename" />
    </project>
  </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>

在此输入上运行时:

<FMPDSORESULT xmlns="http://www.filemaker.com/fmpdsoresult">
  <ERRORCODE>0</ERRORCODE>
  <DATABASE>Projects.fmp12</DATABASE>
  <LAYOUT></LAYOUT>
  <ROW MODID="1" RECORDID="2">
    <Project_name>The best project</Project_name>
    <City_1>New York</City_1>
    <Size>3</Size>
    <Building>Chrysler</Building>
    <City_2>Los Angeles</City_2>
    <Completion>2012-10-12</Completion>
    <Scope_of_work>Big</Scope_of_work>
    <Description>A fun project</Description>
  </ROW>
  <ROW MODID="1" RECORDID="2">
    <Project_name>A pretty good project</Project_name>
    <City_1>Chicago</City_1>
    <Size>4</Size>
    <Building>30 Fake St.</Building>
    <City_2>Charlotte</City_2>
    <Completion>2013-02-03</Completion>
    <Scope_of_work>Medium</Scope_of_work>
    <Description>A serious project</Description>
  </ROW>
</FMPDSORESULT>

产生这个:

<root>
  <project>
    <project_title>
      <name>The best project</name>
      <city_1>New York</city_1>
    </project_title>
    <project_information>
      <building>Chrysler</building>
      <city_2>Los Angeles</city_2>
      <completion>2012-10-12</completion>
      <scope_of_work>Big</scope_of_work>
    </project_information>
    <description>A fun project</description>
  </project>
  <project>
    <project_title>
      <name>A pretty good project</name>
      <city_1>Chicago</city_1>
    </project_title>
    <project_information>
      <building>30 Fake St.</building>
      <city_2>Charlotte</city_2>
      <completion>2013-02-03</completion>
      <scope_of_work>Medium</scope_of_work>
    </project_information>
    <description>A serious project</description>
  </project>
</root>