将节点分为三个级别并求和一个节点

编程入门 行业动态 更新时间:2024-10-08 22:58:39
本文介绍了将节点分为三个级别并求和一个节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请遵循示例xml中的注释,它解释了我需要的一切,它是由三个scenerio来检查输出xml中的每个scenerio的顺序相同.

Please follow the comments in the sample xml it explains everything I need, it is having three scenerio's check each scenerio in the output xml in the same sequence.

输入XML

<!-- In the below xml there will be one ASNInDesc with multiple ASNInPO's and each ASNInPO contains one ASNInCtn and each ASNInCtn contains one ASNInItem --> <ASNInDesc> <asn_nbr>ASN-1</asn_nbr> <!-- In the below two ASNInPO's po_nbr is same container_id under ASNInCtn is same and item_id under ASNInItem is same. In this case two ASNInPO's has to be merged and two ASNInCtn's has to be merged into one tag(since the container_id is same) and two ASNInItem's has to be merged into one tag and unit_qty is to be added --> <ASNInPO> <po_nbr>PO-1</po_nbr> <ASNInCtn> <container_id>CONTAINER-1</container_id> <ASNInItem> <item_id>ITEM-1</item_id> <unit_qty>2</unit_qty> </ASNInItem> </ASNInCtn> </ASNInPO> <ASNInPO> <po_nbr>PO-1</po_nbr> <ASNInCtn> <container_id>CONTAINER-1</container_id> <ASNInItem> <item_id>ITEM-1</item_id> <unit_qty>2</unit_qty> </ASNInItem> </ASNInCtn> </ASNInPO> <!-- In the below two ASNInPO's po_nbr is same container_id under ASNInCtn is same and item_id under ASNInItem is different. In this case two ASNInPO's has to be merged and two ASNInCtn's has to be merged into one tag and two different ASNInItem's for the two different items --> <ASNInPO> <po_nbr>PO-2</po_nbr> <ASNInCtn> <container_id>CONTAINER-2</container_id> <ASNInItem> <item_id>ITEM-2</item_id> <unit_qty>3</unit_qty> </ASNInItem> </ASNInCtn> </ASNInPO> <ASNInPO> <po_nbr>PO-2</po_nbr> <ASNInCtn> <container_id>CONTAINER-2</container_id> <ASNInItem> <item_id>ITEM-3</item_id> <unit_qty>3</unit_qty> </ASNInItem> </ASNInCtn> </ASNInPO> <!-- In the below two ASNInPO's po_nbr is same container_id under ASNInCtn is different and item_id under ASNInItem is different. In this case two ASNInPO's has to be merged into one tag with two different ASNInCtn's each having their own ASNInItem --> <ASNInPO> <po_nbr>PO-3</po_nbr> <ASNInCtn> <container_id>CONTAINER-3</container_id> <ASNInItem> <item_id>ITEM-3</item_id> <unit_qty>2</unit_qty> </ASNInItem> </ASNInCtn> </ASNInPO> <ASNInPO> <po_nbr>PO-3</po_nbr> <ASNInCtn> <container_id>CONTAINER-4</container_id> <ASNInItem> <item_id>ITEM-3</item_id> <unit_qty>2</unit_qty> </ASNInItem> </ASNInCtn> </ASNInPO> </ASNInDesc>

输出XML

<?xml version = '1.0' encoding = 'UTF-8'?> <ASNInDesc> <asn_nbr>ASN-1</asn_nbr> <!-- Scenerio-1 --> <ASNInPO> <po_nbr>PO-1</po_nbr> <ASNInCtn> <container_id>CONTAINER-1</container_id> <ASNInItem> <item_id>ITEM-1</item_id> <unit_qty>4</unit_qty> </ASNInItem> </ASNInCtn> </ASNInPO> <!-- Scenerio-2 --> <ASNInPO> <po_nbr>PO-2</po_nbr> <ASNInCtn> <container_id>CONTAINER-2</container_id> <ASNInItem> <item_id>ITEM-2</item_id> <unit_qty>3</unit_qty> </ASNInItem> <ASNInItem> <item_id>ITEM-3</item_id> <unit_qty>3</unit_qty> </ASNInItem> </ASNInCtn> </ASNInPO> <!-- Scenerio-3 --> <ASNInPO> <po_nbr>PO-3</po_nbr> <ASNInCtn> <container_id>CONTAINER-3</container_id> <ASNInItem> <item_id>ITEM-3</item_id> <unit_qty>2</unit_qty> </ASNInItem> </ASNInCtn> <ASNInCtn> <container_id>CONTAINER-4</container_id> <ASNInItem> <item_id>ITEM-3</item_id> <unit_qty>2</unit_qty> </ASNInItem> </ASNInCtn> </ASNInPO>

我尝试使用自己的xsl,但无法处理所有场景.请帮我解决这个问题. 预先感谢.

I have tried with my own xsl, but couldn't handle all the scenerios. Please help me in solving this. Thanks in Advance.

推荐答案

以下是使用 Muenchian分组.这是基于我对其他问题的回答.

Here's a XSLT 1.0 stylesheet using Muenchian grouping. This is based on my answer to your other question.

这是相同的方法,但是使用已调整键,其中我们使用元素自己的ID和父元素的ID的串联来查找要分组的元素.例如,使用以下级联键查找ASNInItem元素:concat(../../po_nbr, '|', ../container_id, '|', item_id).分隔符的选择并不重要,但是您需要选择一个不出现在ID中的字符.

This is the same approach, but with adjusted keys where we use a concatenation of the element's own id and the parent elements' id to look up elements for grouping. For example, ASNInItem elements are looked up using the following concatenated key: concat(../../po_nbr, '|', ../container_id, '|', item_id). The choice of separator is not important, but you need to choose a character which does not appear in the ids.

还增加了单位数量的总和.

Also the summing of unit quantities has been added.

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="www.w3/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" encoding="UTF-8"/> <xsl:key name="ASNInPO" match="ASNInPO" use="po_nbr"/> <xsl:key name="ASNInCtn" match="ASNInCtn" use="concat(../po_nbr, '|', container_id)"/> <xsl:key name="ASNInItem" match="ASNInItem" use="concat(../../po_nbr, '|', ../container_id, '|', item_id)"/> <xsl:template match="ASNInDesc"> <xsl:copy> <xsl:copy-of select="asn_nbr"/> <xsl:apply-templates select="ASNInPO[generate-id() = generate-id(key('ASNInPO', po_nbr)[1])]"/> </xsl:copy> </xsl:template> <xsl:template match="ASNInPO"> <xsl:copy> <xsl:copy-of select="po_nbr"/> <xsl:apply-templates select="key('ASNInPO', po_nbr)/ ASNInCtn[generate-id() = generate-id(key('ASNInCtn', concat(../po_nbr, '|', container_id))[1])]"/> </xsl:copy> </xsl:template> <xsl:template match="ASNInCtn"> <xsl:copy> <xsl:copy-of select="container_id"/> <xsl:apply-templates select="key('ASNInCtn', concat(../po_nbr, '|', container_id))/ ASNInItem[generate-id() = generate-id(key('ASNInItem', concat(../../po_nbr, '|', ../container_id, '|', item_id))[1])]"/> </xsl:copy> </xsl:template> <xsl:template match="ASNInItem"> <xsl:copy> <xsl:copy-of select="item_id"/> <unit_qty> <xsl:value-of select="sum(key('ASNInItem', concat(../../po_nbr, '|', ../container_id, '|', item_id))/unit_qty)"/> </unit_qty> </xsl:copy> </xsl:template> </xsl:stylesheet>

更多推荐

将节点分为三个级别并求和一个节点

本文发布于:2023-08-03 14:33:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287697.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   级别

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!