XSLT-从具有匹配名称ID的前一个第一项中查找元素值-使用条件? [英] XSLT - Find element values from first preceding item with matching name id - Using conditionals?

查看:81
本文介绍了XSLT-从具有匹配名称ID的前一个第一项中查找元素值-使用条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来如下的XML.

I have an XML that looks the Below.

我试图用每个Image节点属性中的所有值填充文件制作者记录.

I am trying to fill a filemaker record with all the values from each of the Image node attributes.

但是请注意,在显示的XML中,如果Image节点不止一次使用(由其文件ID定义),则其后续子元素将不存在.

However notice that within the XML presented, if a Image node is used more than once, defined by its file id , its subsequent children's elements are not present.

我有下面的当前示例xsl基本上可以运行,但是我想在样式表中添加一个条件评估,内容为: 用英语讲: 如果'key'元素不存在,则找到第一个在前的匹配项并使用这些元素中的值. "string"和"reel"元素也是如此.

I have the current sample xsl below that is basically working but I would like to add a conditional evaluation to the stylesheet that says: In English: if the 'key' element does not exist the find the first preceding matching and use the values from those elements. The same would apply for the 'string and 'reel' elements.

结果将用所有相关数据填充每个记录列,而不是现在有时仅是此类重复文件的部分数据.

The result will fill each records columns with all the relevant data instead of what is now sometimes only partial data for such duplicated files.

很抱歉我的描述不好.

任何帮助将不胜感激.

XML:

<?xml version="1.0"?>
<xmeml>
<boxset>
<stream>
    <track>
        <image>
            <start>0</start>
            <end>90</end>
            <file id="abcde">
                <key>95</key>
                <string>1023</string>
                <time>
                    <reel>142</reel>
                </time>
            </file>
        </image>
        <image>
            <start>90</start>
            <end>120</end>
            <file id="bcdef">
                <key>55</key>
                <string>1023</string>
                <time>
                    <reel>64</reel>
                </time>
            </file>
        </image>
    </track>
    <track>
        <image>
            <start>120</start>
            <end>130</end>
            <file id="abcde"/>
        </image>
        <image>
            <start>130</start>
            <end>180</end>
            <file id="cdefg">
                <key>92</key>
                <string>1023</string>
                <time>
                    <reel>194</reel>
                </time>
            </file>
        </image>    
    </track>
</stream>
</boxset>
</xmeml>

XSLT:

<?xml version='1.0' encoding='UTF-8' ?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/'>
<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
<ERRORCODE>0</ERRORCODE>
<PRODUCT BUILD="11/13/2002" NAME="Filemaker Pro" VERSION="6.0V4"/>
<DATABASE DATEFORMAT="d/M/yyyy" LAYOUT="" NAME="combotest.fp7" RECORDS="" TIMEFORMAT="h:mm:ss a"/>
<METADATA>

<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="start" TYPE="NUMBER"/>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="end" TYPE="NUMBER"/>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="fileID" TYPE="TEXT"/>

<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="key" TYPE="NUMBER"/>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="string" TYPE="NUMBER"/>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="reel" TYPE="NUMBER"/>
</METADATA>
<xsl:for-each select=" //image ">


<RESULTSET FOUND="">

<ROW MODID="" RECORDID="">

<COL><DATA><xsl:value-of select="./start" /></DATA></COL>
<COL><DATA><xsl:value-of select="./end" /></DATA></COL>
<COL><DATA><xsl:value-of select="./file/@id" /></DATA></COL>

<COL><DATA><xsl:value-of select="./file/key" /></DATA></COL>
<COL><DATA><xsl:value-of select="./file/string" /></DATA></COL>
<COL><DATA><xsl:value-of select="./file/time/reel" /></DATA></COL>

</ROW>

</xsl:for-each>


</RESULTSET></FMPXMLRESULT>
</xsl:template>
</xsl:stylesheet>

如果要填充XML,他们希望看到它的方式看起来像这样.

If the XML were to be filled they way Id like to see it it would look like this.

理想的XML:

<?xml version="1.0"?>
<xmeml>
<boxset>
<stream>
    <track>
        <image>
            <start>0</start>
            <end>90</end>
            <file id="abcde">
                <key>95</key>
                <string>1023</string>
                <time>
                    <reel>142</reel>
                </time>
            </file>
        </image>
        <image>
            <start>90</start>
            <end>120</end>
            <file id="bcdef">
                <key>55</key>
                <string>1023</string>
                <time>
                    <reel>64</reel>
                </time>
            </file>
        </image>
    </track>
    <track>
        <image>
            <start>120</start>
            <end>130</end>
<!-- ideal data for repeated file "abcde" -->
            <file id="abcde">
                <key>95</key>
                <string>1023</string>
                <time>
                    <reel>142</reel>
                </time>
            </file>
<!-- end ideal repeated data -->
        </image>
        <image>
            <start>130</start>
            <end>180</end>
            <file id="cdefg">
                <key>92</key>
                <string>1023</string>
                <time>
                    <reel>194</reel>
                </time>
            </file>
        </image>    
    </track>
</stream>
</boxset>
</xmeml>

推荐答案

您需要定义一个XSLT密钥以通过其ID查找文件定义:

You need to define a XSLT key to find file definition by its ID:

<xsl:key name="file" match="file[*]" use="@id" />

此处,[*]谓词可确保只有具有至少一个子元素的file会被其ID索引.由于每个文件只有一个这样的定义,因此这正是我们所需要的.然后使用key()函数找到定义:

Here the [*] predicate ensures that only files with at least one child element will be indexed by their id. Since each file has only one such a definition, this is exactly what we need. Then you find the definition using the key() function:

...
<COL><DATA><xsl:value-of select="./end" /></DATA></COL>
<xsl:variable name="file" select="key('file', file/@id)" />
<COL><DATA><xsl:value-of select="$file/@id" /></DATA></COL>
<COL><DATA><xsl:value-of select="$file/key" /></DATA></COL>
<COL><DATA><xsl:value-of select="$file/string" /></DATA></COL>
<COL><DATA><xsl:value-of select="$file/time/reel" /></DATA></COL>
...

XSLT key添加在样式表的顶层(即xsl:templatexsl:output等).

XSLT keys are added at the top level of the stylesheet (i.e. among xsl:template, xsl:output, etc.).

这篇关于XSLT-从具有匹配名称ID的前一个第一项中查找元素值-使用条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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