感谢 Stackoverflow 的 Tomalak,我能够从“旧”FMPXMLRESULT xml 语法到“新”FMRESULTSET xml 语法进行简单的 XSL 转换。
这是带有 FMPXMLRESULT 语法的原始 xml 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
<ERRORCODE>0</ERRORCODE>
<PRODUCT BUILD="06-20-2006" NAME="FileMaker Pro" VERSION="8.5v1"/>
<DATABASE DATEFORMAT="M/d/yyyy" LAYOUT="" NAME="pro_page.fp7" RECORDS="2121" TIMEFORMAT="h:mm:ss a"/>
<METADATA>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Ident" TYPE="TEXT"/>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Title" TYPE="TEXT"/>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Content" TYPE="TEXT"/>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Footnotes::Num" TYPE="TEXT"/>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Footnotes::Title" TYPE="TEXT"/>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Images::Num" TYPE="TEXT"/>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Images::FileName" TYPE="TEXT"/>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="Images::Title" TYPE="TEXT"/>
</METADATA>
<RESULTSET FOUND="1">
<ROW MODID="135" RECORDID="440">
<COL><DATA>422</DATA></COL>
<COL><DATA>title text here</DATA></COL>
<COL><DATA>some more text here</DATA></COL>
<COL>
<DATA>1</DATA>
<DATA>2</DATA>
<DATA>3</DATA>
<DATA>4</DATA>
</COL>
<COL>
<DATA>footnote 1 text here</DATA>
<DATA>footnote 2 text here</DATA>
<DATA>footnote 3 text here</DATA>
<DATA>footnote 4 text here</DATA>
</COL>
<COL>
<DATA>457</DATA>
<DATA>563</DATA>
</COL>
<COL>
<DATA>image 1 url here</DATA>
<DATA>image 2 url here</DATA>
</COL>
<COL>
<DATA>title for image 1 here</DATA>
<DATA>title for image 2 here</DATA>
</COL>
</ROW>
</RESULTSET>
这是我想要获得的 FMRESULTSET 格式(注意:我不需要完整的“元数据”部分,它可以留空,并且我删除了一些我也不需要的属性):
<?xml version="1.0" encoding="UTF-8"?>
<fmresultset version="1.0" xmlns="http://www.filemaker.com/xml/fmresultset">
<error code="0"/>
<product version="8.0.4.128" name="FileMaker Web Publishing Engine" build="06/14/2006"/>
<datasource total-count="2121" timestamp-format="MM/dd/yyyy HH:mm:ss" time-format="HH:mm:ss" table="pro_page" layout="MR_Page_An" date-format="MM/dd/yyyy" database="pro_page.fp7"/>
<metadata></metadata>
<resultset fetch-size="1" count="1">
<record>
<field name="Ident"><data>422</data></field>
<field name="Title"><data>title text here</data></field>
<field name="Content"><data>some more text here</data></field>
<relatedset table="Footnotes" count="4">
<record>
<field name="Footnotes::Num"><data>1</data></field>
<field name="Footnotes::Title"><data>footnote 1 text here</data></field>
</record>
<record>
<field name="Footnotes::Num"><data>2</data></field>
<field name="Footnotes::Title"><data>footnote 2 text here</data></field>
</record>
<record>
<field name="Footnotes::Num"><data>3</data></field>
<field name="Footnotes::Title"><data>footnote 3 text here</data></field>
</record>
<record>
<field name="Footnotes::Num"><data>4</data></field>
<field name="Footnotes::Title"><data>footnote 4 text here</data></field>
</record>
</relatedset>
<relatedset table="Images" count="2">
<record>
<field name="Images::Num"><data>457</data></field>
<field name="Images::FileName"><data>image 1 url here</data></field>
<field name="Images::Title"><data>title for image 1 here</data></field>
</record>
<record>
<field name="Images::Num"><data>563</data></field>
<field name="Images::FileName"><data>image 2 url here</data></field>
<field name="Images::Title"><data>title for image 2 here</data></field>
</record>
</relatedset>
</record>
</resultset>
到目前为止,这就是我的 XSL 转换的样子。它工作正常,但不适用于“相关集”部分。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fmp="http://www.filemaker.com/fmpxmlresult" exclude-result-prefixes="fmp">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="kMetaData" match="fmp:METADATA/fmp:FIELD" use="count(preceding-sibling::fmp:FIELD) + 1" />
<xsl:template match="/fmp:FMPXMLRESULT">
<fmresultset version="1.0">
<error code="0"/>
<product build="01/01/201" name="FileMaker Web Pro" version="8.5"/>
<datasource database="pro_page.fp7" date-format="MM/dd/yyyy" layout="MR_Page_Fr" table="pro_page" time-format="HH:mm:ss" timestamp-format="MM/dd/yyyy HH:mm:ss" total-count="1"/>
<metadata></metadata>
<resultset count="1" fetch-size="1">
<xsl:apply-templates select="fmp:RESULTSET/fmp:ROW" />
</resultset>
</fmresultset>
</xsl:template>
<xsl:template match="fmp:ROW">
<record record-id="0" mod-id="0">
<xsl:apply-templates select="fmp:COL" />
</record>
</xsl:template>
<xsl:template match="fmp:COL">
<field name="{string(key('kMetaData', position())/@NAME)}">
<data><xsl:value-of select="fmp:DATA" /></data>
</field>
</xsl:template>
</xsl:stylesheet>
任何人都足够精通 XSLT 来帮助我解决这个问题吗?谢谢!