gpt4 book ai didi

xml - 如何在 XSLT 中按节点值对 XML 元素进行分组?

转载 作者:数据小太阳 更新时间:2023-10-29 02:51:28 26 4
gpt4 key购买 nike

我正在尝试对 filemaker 数据库中的记录进行分组。我正在导出为 XML,并可选择使用 XSLT 进行转换。

我一直在搜索和阅读其他帖子,但我认为它们没有完全涵盖我想做的事情。

XML 的摘录:

<?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>Artpostersnbbs.fp7</DATABASE>
<LAYOUT />
<ROW MODID="19" RECORDID="11116">
<Art_Type>Poster</Art_Type>
<Location>1</Location>
<Line1>ELEVATOR
MACHINE
ROOM
107</Line1>
</ROW>
<ROW MODID="19" RECORDID="11116">
<Art_Type>Poster</Art_Type>
<Location>2</Location>
<Line1>ELEVATOR
MACHINE
ROOM
107</Line1>
</ROW>
<ROW MODID="19" RECORDID="11116">
<Art_Type>Poster</Art_Type>
<Location>3</Location>
<Line1>ELEVATOR
MACHINE
ROOM
107</Line1>
</ROW>
</FMPDSORESULT>

我希望组中的每条记录都匹配 ART_TYPE 和 LINE1。分组后,它应该将匹配项中的位置添加到被分组到的位置,因此它应该如下所示:

<ROW MODID="19" RECORDID="11116">
<Art_Type>Poster</Art_Type>
<Location>1 2 3</Location>
<Line1>ELEVATOR
MACHINE
ROOM
107
</Line1>
</ROW>

任何有关如何开始的帮助都将不胜感激。另外有什么好的xslt 1.0测试程序吗?

提前致谢!

编辑:我被指向 muenchian 分组并找到了这个网站:http://www.jenitennison.com/xslt/grouping/muenchian.html

所以从阅读中我想出了:

<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="artTypeNames" match="ROW" use="Art_Type" />
<xsl:key name="artCopy" match="ROW" use="Line1" />
<xsl:template match="FMPDSORESULT">
<xsl:for-each select="ROW[count(. | key('artTypeNames', Art_Type)[1]) = 1]">
<xsl:sort select="Art_Type" />
<xsl:value-of select="Art_Type" />
<xsl:for-each select="key('artTypeNames', Art_Type)">
<xsl:sort select="Art_Type" />
<xsl:value-of select="Art_Type" />
</xsl:for-each>
</xsl:for-each>

<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

我将 XML 和 XSLT 输入在线 XML 转换器,但出现“XSLT 无效”错误。

这令人沮丧。

EDIT2:在 Tim 的帮助下,我能够构建适当的 XSLT 转换:

<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fm="http://www.filemaker.com/fmpdsoresult">

<xsl:template match="fm:FMPDSORESULT">
<xsl:apply-templates select="fm:ROW[count(. | key('lineData', fm:Line1)[1]) = 1]">
</xsl:apply-templates>
</xsl:template>

<xsl:template match="fm:ROW">
<xsl:copy>
<xsl:apply-templates select="fm:Art_Type" />
<fm:Location>
<xsl:apply-templates select="key('artTypeNames', fm:Art_Type)/fm:Location" />
</fm:Location>
<xsl:apply-templates select="fm:Line1" />
</xsl:copy>
</xsl:template>

<xsl:template match="fm:Location">
<xsl:if test="position() > 1">-</xsl:if>
<xsl:value-of select="." />
</xsl:template>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

它对 Art_Type 进行分组,然后按 Line1 文本进行分组,但现在将位置编号添加到所有这些中,如下所示:

<ROW xmlns="http://www.filemaker.com/fmpdsoresult">
<Art_Type>Poster</Art_Type>
<fm:Location xmlns:fm="http://www.filemaker.com/fmpdsoresult" xmlns="">1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-29-30-31-32-33-34</fm:Location>
<Line1>CUSTODIAL
LOUNGE

117A
</Line1>
</ROW>
<ROW xmlns="http://www.filemaker.com/fmpdsoresult">
<Art_Type>Poster</Art_Type>
<fm:Location xmlns:fm="http://www.filemaker.com/fmpdsoresult" xmlns="">1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-29-30-31-32-33-34</fm:Location>
<Line1>STORAGE
ROOM

117B
</Line1>
</ROW>

如果 Line1 文本不同,应该将其添加到另一个组中。

最佳答案

如果您使用的是 XSLT 2.0,请查找有关 xsl:for-each-group 的信息。如果您使用的是 1.0,请查找有关“Muenchian 分组”的信息。

关于xml - 如何在 XSLT 中按节点值对 XML 元素进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19759805/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com