2

我使用 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>

任何建议将不胜感激。

4

1 回答 1

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>
于 2013-01-23T02:58:06.693 回答