我有一些从Filemaker导出的粗糙的XML。 有一篇很棒的文章几乎可以回答我的问题( XSLT问题。当原始XML位于单独的部分中时,如何将字段标记与数据配对? )。 该文章中的xslt样式表使用在<meta>字段中键入的名称优雅地重命名了丑陋的原始xml。

很好,除非您在下面看,否则您会看到每行都有多个元素,应将其命名为batchIDbatchPartnerIDbatchPartnerName 但是,只有这三个元素的第一个子元素被重命名-其余的元素被简单地删除。

我认为样式表不会遍历所有元素(最初用于xml的每个<COL>仅有一个子代。不幸的是,我对XSLT不够熟悉,无法解决此问题……无济于事吗?

原始XML

<?xml version="1.0" encoding="UTF-8"?>
<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
  <ERRORCODE>0</ERRORCODE>
  <PRODUCT BUILD="11-13-2014" NAME="FileMaker" VERSION="Pro 13.0v4"/>
  <DATABASE DATEFORMAT="M/d/yyyy" LAYOUT="" NAME="ArticleIndex.fmp12" RECORDS="3678" TIMEFORMAT="h:mm:ss a"/>
  <METADATA>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="id" TYPE="NUMBER"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="author" TYPE="TEXT"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="url" TYPE="TEXT"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="brand" TYPE="TEXT"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="publishDate" TYPE="TEXT"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="categoryAll" TYPE="TEXT"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="originalTitle" TYPE="TEXT"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="originalBody" TYPE="TEXT"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="standardTitle" TYPE="TEXT"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="standardSubtitle" TYPE="TEXT"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="standardPublishDate" TYPE="TEXT"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="standardBody" TYPE="TEXT"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="standardNotes" TYPE="TEXT"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="standardLastUpdated" TYPE="DATE"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="standardLastReviewed" TYPE="DATE"/>
    <FIELD EMPTYOK="NO" MAXREPEAT="1" NAME="batchID" TYPE="NUMBER"/>
    <FIELD EMPTYOK="NO" MAXREPEAT="1" NAME="batchPartnerID" TYPE="NUMBER"/>
    <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="batchPartnerName" TYPE="TEXT"/>
  </METADATA>
  <RESULTSET FOUND="12">
    <ROW MODID="24" RECORDID="2028">
      <COL>
        <DATA>11023</DATA>
      </COL>
      <COL>
        <DATA>Tom Myer</DATA>
      </COL>
      <COL>
        <DATA>http://www.sitepoint.com/really-good-introduction-xml/</DATA>
      </COL>
      <COL>
        <DATA>SitePoint</DATA>
      </COL>
      <COL>
        <DATA>August 2005</DATA>
      </COL>
      <COL>
        <DATA/>
      </COL>
      <COL>
        <DATA>A Really Good Introduction to XML</DATA>
      </COL>
      <COL>
        <DATA>A Really Good Introduction to XML —E. Berliet, Lyon, France

In this chapter, we’ll cover the basics of XML – essentially, most of the information you’ll need to know to get a handle on this exciting technology. After we’re done exploring some terminology and examples, we’ll jump right in and start working with XML documents. Then, we’ll spend some time starting the project we’ll develop through the course of this book: building an XML-powered content management system.

This excerpt is taken from No Nonsense XML Web Development with PHP, SitePoint’s new release, by Thomas Myer, which was designed to help you start using XML to build intelligent ‘Future-Proof’ PHP applications today.</DATA>
      </COL>
      <COL>
        <DATA>A Really, Really, Really Good Introduction to XML</DATA>
      </COL>
      <COL>
        <DATA>Starting out right</DATA>
      </COL>
      <COL>
        <DATA>8/24/2005</DATA>
      </COL>
      <COL>
        <DATA>A Really, Really, Really Good Introduction to XML —E. Berliet, Lyon, France

In this chapter, we’ll cover the basics of XML – essentially, most of the information you’ll need to know to get a handle on this exciting technology. After we’re done exploring some terminology and examples, we’ll jump right in and start working with XML documents. Then, we’ll spend some time starting the project we’ll develop through the course of this book: building an XML-powered content management system.

This excerpt is taken from No Nonsense XML Web Development with PHP, SitePoint’s new release, by Thomas Myer, which was designed to help you start using XML to build intelligent ‘Future-Proof’ PHP applications today.

The title contains over 350 pages of XML and PHP goodies. It walks you through the process of building a fully-functional XML-based content management system with PHP. And all the code used in the book is available to customers in a downloadalbe archive.

To find out more about “No Nonsense XML Web Development with PHP”, visit the book’s information page, or review the contents of the entire publication. As always, you can download this excerpt as a PDF if you prefer.
     </COL>
      <COL>
        <DATA/>
      </COL>
      <COL>
        <DATA/>
      </COL>
      <COL>
        <DATA/>
      </COL>
      <COL>
        <DATA>4127</DATA>
        <DATA>4130</DATA>
        <DATA>4136</DATA>
      </COL>
      <COL>
        <DATA>1101</DATA>
        <DATA>1107</DATA>
        <DATA>1140</DATA>
      </COL>
      <COL>
        <DATA>First Client Name</DATA>
        <DATA>Second Client Name</DATA>
        <DATA>Third Client Name</DATA>
      </COL>
    </ROW>
  </RESULTSET>
</FMPXMLRESULT>

XSLT样式表

<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" encoding="UTF-8" indent="yes"/>

  <!-- the key indexes the METADATA fields by their position -->
  <xsl:key 
    name="kMetaData" 
    match="fmp:METADATA/fmp:FIELD" 
    use="count(preceding-sibling::fmp:FIELD) + 1" 
  />

  <!-- separate templates increase readability -->
  <xsl:template match="/fmp:FMPXMLRESULT">
    <content>
      <xsl:apply-templates select="fmp:RESULTSET/fmp:ROW" />
    </content>
  </xsl:template>

  <xsl:template match="fmp:ROW">
    <contentitem>
      <xsl:apply-templates select="fmp:COL" />
    </contentitem>
  </xsl:template>

  <xsl:template match="fmp:COL">
    <!-- column name lookup is high-speed because of the key -->
    <xsl:element name="{string(key('kMetaData', position())/@NAME)}">
      <xsl:value-of select="fmp:DATA" />
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

翻译的XML

<?xml version="1.0" encoding="UTF-8"?>
<content>
  <contentitem>
    <id>11023</id>
    <author>Tom Myer</author>
    <url>http://www.sitepoint.com/really-good-introduction-xml/</url>
    <brand>SitePoint</brand>
    <publishDate>August 2005</publishDate>
    <categoryAll/>
    <originalTitle>A Really Good Introduction to XML</originalTitle>
    <originalBody>A Really Good Introduction to XML —E. Berliet, Lyon, France

In this chapter, we’ll cover the basics of XML – essentially, most of the information you’ll need to know to get a handle on this exciting technology. After we’re done exploring some terminology and examples, we’ll jump right in and start working with XML documents. Then, we’ll spend some time starting the project we’ll develop through the course of this book: building an XML-powered content management system.

This excerpt is taken from No Nonsense XML Web Development with PHP, SitePoint’s new release, by Thomas Myer, which was designed to help you start using XML to build intelligent ‘Future-Proof’ PHP applications today.</originalBody>
    <standardTitle>A Really, Really, Really Good Introduction to XML</standardTitle>
    <standardSubtitle>Starting out right</standardSubtitle>
    <standardPublishDate>8/24/2005</standardPublishDate>
    <standardBody>A Really, Really, Really Good Introduction to XML —E. Berliet, Lyon, France

In this chapter, we’ll cover the basics of XML – essentially, most of the information you’ll need to know to get a handle on this exciting technology. After we’re done exploring some terminology and examples, we’ll jump right in and start working with XML documents. Then, we’ll spend some time starting the project we’ll develop through the course of this book: building an XML-powered content management system.

This excerpt is taken from No Nonsense XML Web Development with PHP, SitePoint’s new release, by Thomas Myer, which was designed to help you start using XML to build intelligent ‘Future-Proof’ PHP applications today.

The title contains over 350 pages of XML and PHP goodies. It walks you through the process of building a fully-functional XML-based content management system with PHP. And all the code used in the book is available to customers in a downloadalbe archive.

To find out more about “No Nonsense XML Web Development with PHP”, visit the book’s information page, or review the contents of the entire publication. As always, you can download this excerpt as a PDF if you prefer.</standardBody>
    <standardNotes/>
    <standardLastUpdated/>
    <standardLastReviewed/>
    <batchID>4127</batchID>
    <batchPartnerID>1101</batchPartnerID>
    <batchPartnerName>First Client Name</batchPartnerName>
  </contentitem>
</content>

#1楼 票数:1 已采纳

或者简单地:

<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" />

<!-- separate templates are not required for something as simple as this -->
<xsl:template match="/">
    <content>
        <xsl:for-each select="fmp:FMPXMLRESULT/fmp:RESULTSET/fmp:ROW">
            <contentitem>
                <xsl:for-each select="fmp:COL">
                    <xsl:variable name="field-name" select="key('kMetaData', position())/@NAME" />
                    <xsl:for-each select="fmp:DATA">
                        <xsl:element name="{$field-name}">
                            <xsl:value-of select="." />
                        </xsl:element>
                    </xsl:for-each>
                </xsl:for-each>
            </contentitem>
        </xsl:for-each>
    </content>
</xsl:template>

</xsl:stylesheet>

请注意,这是假设您的字段名称也是(并将始终是)有效的XML元素名称。 如果不确定,请在导出时切换到FMPDSORESULT语法。 这会将您的字段名称转换为有效的XML名称(例如,用下划线替换空格)-但您还需要其他XSLT样式表。


编辑:

例如,给定以下FMPDSORESULT导出作为输入:

XML

<FMPDSORESULT xmlns="http://www.filemaker.com/fmpdsoresult">
<ERRORCODE>0</ERRORCODE>
<DATABASE>example.fmp12</DATABASE>
<LAYOUT/>
<ROW MODID="1" RECORDID="1">
<some_field>Alpha</some_field>
<another_field>
<DATA>101</DATA>
<DATA>102</DATA>
<DATA>103</DATA>
</another_field>
</ROW>
<ROW MODID="2" RECORDID="2">
<some_field>Bravo</some_field>
<another_field>
<DATA>21</DATA>
<DATA>22</DATA>
</another_field>
</ROW>
</FMPDSORESULT>

以下样式表:

XSLT 1.0

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

<xsl:template match="/">
    <root>
        <xsl:for-each select="fmp:FMPDSORESULT/fmp:ROW">
            <row> 
                <xsl:apply-templates/>
            </row>
        </xsl:for-each>
    </root>
</xsl:template>

<xsl:template match="fmp:ROW/*[not(fmp:DATA)]">
    <xsl:element name="{name(.)}">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

<xsl:template match="fmp:DATA">
    <xsl:element name="{name(..)}">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

将返回:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <row>
    <some_field>Alpha</some_field>
    <another_field>101</another_field>
    <another_field>102</another_field>
    <another_field>103</another_field>
  </row>
  <row>
    <some_field>Bravo</some_field>
    <another_field>21</another_field>
    <another_field>22</another_field>
  </row>
</root>

#2楼 票数:0

因此, 听起来您想要具有三个<DATA>值的三个batchID ,并且batchPartnerIDbatchPartnerName相同。 那正确吗?

如果是这样,可以使用for-each遍历<DATA>

<xsl:template match="fmp:COL">
  <xsl:variable name="elName"
                select="key('kMetaData', position())/@NAME" />
  <xsl:for-each select="fmp:DATA">
    <xsl:element name="{$elName}">
      <xsl:value-of select="." />
    </xsl:element>
  </xsl:for-each>
</xsl:template>

或者您可以使用参数化的模板:

<xsl:template match="fmp:COL">
  <xsl:apply-templates select="fmp:DATA">
    <xsl:with-param name="elName" select="key('kMetaData', position())/@NAME" />
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="fmp:DATA">
  <xsl:param name="elName" select="name()" />

  <xsl:element name="{$elName}">
    <xsl:value-of select="." />
  </xsl:element>
</xsl:template>

  ask by doub1ejack translate from so

未解决问题?本站智能推荐:

1回复

用于FilemakerproXML导出的XSL样式表

我正在为Filemaker Pro XML导出创建样式表,但是在浏览器中加载输出时不会呈现输出。 我已经将XSL代码与其他示例XML数据进行了测试,并且可以在浏览器中正常显示,但是不能与filemaker pro格式一起使用。 任何建议将不胜感激。 我的XML文件如下所示: 这是我的
1回复

创建XSL样式表以管理FileMakerProXML输出

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

FilemakerPro中的XSLT字段名称

我是 XSLT 的新手,我正在尝试使用 fmpxmlresult 语法转换从 Filemaker Pro 生成的 XML 文件。 Filemaker 以这种方式输出 RAW XML: 在我的 XSLT 中,我使用 它有效,但有点混乱,因为我有一个包含很多字段的 XML 文件,并且很容易按数字寻址错
2回复

XNK数据未填充在我的xslt代码中

这是我的第一个XSLT脚本,对XML或其工作原理一无所知。 将XSLT映射到Filemaker的工作原理如下 在下面的某个地方,我需要填充FM数据库。正在填充单个名称,但不填充XML数据。 下面是一个简短的XML文档,每次上载时文件名都会更改。 下面没有任何变化。
1回复

filemakerxml通过xslt导出

我正在尝试从Filemaker数据库导出一些数据并通过XSLT进行转换 我已经完成了大部分工作,但我遇到了一些嵌套数据: XML输出的当前结构是: 我的XSL在某种程度上正在工作,但我无法完全找出每个图像: 除了给出正确数量的标签但没有数据的位之外,这个工作正常 任何人都可以
2回复

FilemakerPro12XSLT

我正在filemaker中导入XML数据,我需要一些帮助来格式化数据。 我的XML文件: 我的XSLT: 导入工作正常,但我想将字段“数据”从yyyymmdd格式化为dd / mm / yyyy,在“prezzoPers”字段中使用“,”而不是“。” 我是XSLT的新手,所以如果有
2回复

使用xslt(muenchian分组)将平面xml分组,无法将其分组

我花了很多时间试图使下面的xml的muenchian分组正常工作,但是我无法使其正常工作。 我尝试了多种方法,因此发布最新尝试可能会使任何试图帮助我的人感到困惑。 :-) Xslt必须为1.0 输入文件是发票行,其中“发票抬头信息”对于属于特定发票的每一行都是重复的。 分组应在字
1回复

使用XSLT进行Filemaker13导出格式化

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