gpt4 book ai didi

xslt - XSL 查找节点之间的所有节点

转载 作者:行者123 更新时间:2023-12-01 13:04:16 24 4
gpt4 key购买 nike

我有一个格式不正确的大型 XML 文件,其中与单个行项目相关的信息被分成多行信息,我试图将这些信息与父行项目 (ITEM_ID) 组合在一起。信息是顺序的,所以关键是 ITEM_ID 节点,但我似乎无法创建正确的 XSL 来对与项目 (ITEM_ID) 相关的信息进行分组,给定以下 XML 源(更新以包括新发现的孙元素XML 来源):

<LINE_INFO>
<ITEM_ID>some_part_num</ITEM_ID>
<DESC>some_part_num_description</DESC>
<QTY>nn</QTY>
<UNIT>uom</UNIT>
</LINE_INFO>
<LINE_INFO>
<EXT_DESC>more_description_for_some_part_num</EXT_DESC>
</LINE_INFO>
<LINE_INFO>
<ITEM_ID>some_other_part_num</ITEM_ID>
<DESC>some_other_part_num_description</DESC>
<QTY>nn</QTY>
<UNIT>uom</UNIT>
</LINE_INFO>
<LINE_INFO>
<EXT_DESC>more_description_for_some_other_part_num</EXT_DESC>
</LINE_INFO>
<LINE_INFO>
<LINE_NOTE>This is a note related to some_other_part_num</LINE_NOTE>
</LINE_INFO>
<LINE_INFO>
<ADDTL_NOTE_DETAIL>
<NOTE>This is the grandchild note that sometimes appears in my data</NOTE>
</ADDTL_NOTE_DETAIL>
</LINE_INFO>
<LINE_INFO>
<ITEM_ID>yet_another_part_num</ITEM_ID>
<DESC>yet_another_part_num_description</DESC>
<QTY>nn</QTY>
<UNIT>uom</UNIT>
</LINE_INFO>
...

期望的输出:

<LINE_INFO>
<ITEM_ID>some_part_num</ITEM_ID>
<DESC>some_part_num_description</DESC>
<QTY>nn</QTY>
<UNIT>uom</UNIT>
<EXT_DESC>more_description_for_some_part_num</EXT_DESC>
</LINE_INFO>
<LINE_INFO>
<ITEM_ID>some_other_part_num</ITEM_ID>
<DESC>some_other_part_num_description</DESC>
<QTY>nn</QTY>
<UNIT>uom</UNIT>
<EXT_DESC>more_description_for_some_other_part_num</EXT_DESC>
<LINE_NOTE>This is a note related to some_other_part_num</LINE_NOTE>
<NOTE>This is the grandchild note that sometimes appears in my data</NOTE>
</LINE_INFO>
<LINE_INFO>
<ITEM_ID>yet_another_part_num</ITEM_ID>
<DESC>yet_another_part_num_description</DESC>
<QTY>nn</QTY>
<UNIT>uom</UNIT>
</LINE_INFO>

最佳答案

这个转换:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:key name="kFollowing" match="LINE_INFO[not(ITEM_ID)]"
use="generate-id(preceding-sibling::LINE_INFO[ITEM_ID][1])"/>

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

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

<xsl:apply-templates select="key('kFollowing', generate-id())/node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="LINE_INFO[not(ITEM_ID)]"/>
</xsl:stylesheet>

应用于提供的 XML 文档时(包装在单个顶部元素中以使其格式正确):

<t>
<LINE_INFO>
<ITEM_ID>some_part_num</ITEM_ID>
<DESC>some_part_num_description</DESC>
<QTY>nn</QTY>
<UNIT>uom</UNIT>
</LINE_INFO>
<LINE_INFO>
<EXT_DESC>more_description_for_some_part_num</EXT_DESC>
</LINE_INFO>
<LINE_INFO>
<ITEM_ID>some_other_part_num</ITEM_ID>
<DESC>some_other_part_num_description</DESC>
<QTY>nn</QTY>
<UNIT>uom</UNIT>
</LINE_INFO>
<LINE_INFO>
<EXT_DESC>more_description_for_some_other_part_num</EXT_DESC>
</LINE_INFO>
<LINE_INFO>
<LINE_NOTE>This is a note related to some_other_part_num</LINE_NOTE>
</LINE_INFO>
<LINE_INFO>
<ITEM_ID>yet_another_part_num</ITEM_ID>
<DESC>yet_another_part_num_description</DESC>
<QTY>nn</QTY>
<UNIT>uom</UNIT>
</LINE_INFO>
</t>

产生想要的、正确的结果:

<t>
<LINE_INFO>
<ITEM_ID>some_part_num</ITEM_ID>
<DESC>some_part_num_description</DESC>
<QTY>nn</QTY>
<UNIT>uom</UNIT>
<EXT_DESC>more_description_for_some_part_num</EXT_DESC>
</LINE_INFO>
<LINE_INFO>
<ITEM_ID>some_other_part_num</ITEM_ID>
<DESC>some_other_part_num_description</DESC>
<QTY>nn</QTY>
<UNIT>uom</UNIT>
<EXT_DESC>more_description_for_some_other_part_num</EXT_DESC>
<LINE_NOTE>This is a note related to some_other_part_num</LINE_NOTE>
</LINE_INFO>
<LINE_INFO>
<ITEM_ID>yet_another_part_num</ITEM_ID>
<DESC>yet_another_part_num_description</DESC>
<QTY>nn</QTY>
<UNIT>uom</UNIT>
</LINE_INFO>
</t>

请注意:使用 key 轻松高效地识别所有LINE_INFO没有 ITEM_ID 的节点 child 并立即关注LINE_INFO带有 ITEM_ID 的节点 child 。

关于xslt - XSL 查找节点之间的所有节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4036056/

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