XSLT样式表删除子级而不是重命名子级 [英] XSLT stylesheet drops children instead of renaming them

查看:77
本文介绍了XSLT样式表删除子级而不是重命名子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些很漂亮的XML,它是从Filemaker导出的.有一篇很棒的帖子,几乎回答了我的问题(

I have some pretty gnarly XML that was exported from Filemaker. There is a great post that almost answers my question (XSLT question. How to pair field tags with data when original XML has them in separate sections?). The xslt stylesheet from that post elegantly renames the ugly original xml with the names keyed in the <meta> field.

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

That's great, except if you look below you will see that each row has multiple elements that should be named batchID, batchPartnerID and batchPartnerName. However, only the first child of these three elements is renamed - the rest are simply dropped.

我想象样式表不会遍历所有元素(最初处理的xml每个<COL>仅有一个孩子.不幸的是,我对XSLT不够熟悉,无法解决此问题...几乎没有帮助?谢谢.

I imagine the stylesheet just isn't looping through the elements (the xml it was originally working on only had one child per <COL>. Unfortunately, I'm not familiar enough with XSLT to fix this... Little help? Thanks.

原始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>

推荐答案

或者简单地:

<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样式表.

Note that this is assuming that your field names are (and always will be) also valid XML element names. If you cannot be sure of that, then switch to FMPDSORESULT grammar when exporting. This will convert your field names to valid XML names (e.g. replace spaces with underscores) - but you will also need a different XSLT stylesheet.

作为一个示例,给定以下FMPDSORESULT导出作为输入:

As an example, given the following FMPDSORESULT export as the input:

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>

这篇关于XSLT样式表删除子级而不是重命名子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆