动态增加节点值
我有一个输入 xml,其结构如下:
<VEN>
<CUS>
<MEM>
<MEM>
<MEM>
</CUS>
<CUS>
<MEM>
<MEM>
<MEM>
</CUS>
每个节点在输入 xml 中出现多次。每个MEM中都有一个属性@CLM01 必须根据 MEM 节点每 20 次出现的情况为其分配一个值。从第一个 VEN/CUS 开始,如果 MEM 有 55 个节点,则前 20 个节点的 @CLM01 值为“1”,对于相同的 VEN/CUS 组合,接下来的 20 个节点的值为“2”,其余 15 个节点的值为“2”。 @CLM01 值为“3”。
对于同一 VEN 下的下一个 CUS(假设该 CUS 有 30 个 MEM 节点),前 20 个 MEM 节点的 @CLM01 值为“4”,接下来的 10 个 MEM 的 @CLM01 值为“5”。这样继续下去......我希望我清楚我想要实现的目标。
谢谢。是否可以?使用 xslt 1.0
这是示例输入
<Data>
<VEN vendorId= "v1">
<CUS custId = "c1">
<MEM memId="m1" CLM01=""/>
<MEM memId="m2" CLM01=""/>
<MEM memId="m3" CLM01=""/>
<MEM memId="m4" CLM01=""/>
<MEM memId="m5" CLM01=""/>
</CUS>
<CUS custId = "c2">
<MEM memId="m11" CLM01=""/>
<MEM memId="m12" CLM01=""/>
<MEM memId="m13" CLM01=""/>
<MEM memId="m14" CLM01=""/>
<MEM memId="m15" CLM01=""/>
<MEM memId="m16" CLM01=""/>
<MEM memId="m17" CLM01=""/>
<MEM memId="m18" CLM01=""/>
</CUS>
</VEN>
<VEN vendorId= "v2">
<CUS custId = "c1">
<MEM memId="m4" CLM01=""/>
<MEM memId="m5" CLM01=""/>
<MEM memId="m6" CLM01=""/>
<MEM memId="m7" CLM01=""/>
<MEM memId="m8" CLM01=""/>
</CUS>
<CUS custId = "c1">
<MEM memId="m33" CLM01=""/>
<MEM memId="m44" CLM01=""/>
<MEM memId="m55" CLM01=""/>
<MEM memId="m66" CLM01=""/>
<MEM memId="m77" CLM01=""/>
<MEM memId="m88" CLM01=""/>
<MEM memId="m99" CLM01=""/>
<MEM memId="m11" CLM01=""/>
</CUS>
</VEN>
期望的输出(而不是 MEM 节点出现 20 次,我们每出现 3 次就出现一次)
<Data>
<VEN vendorId= "v1">
<CUS custId = "c1">
<MEM memId="m1" CLM01="1"/>
<MEM memId="m2" CLM01="1"/>
<MEM memId="m3" CLM01="1"/>
<MEM memId="m4" CLM01="2"/>
<MEM memId="m5" CLM01="2"/>
</CUS>
<CUS custId = "c2">
<MEM memId="m11" CLM01="3"/>
<MEM memId="m12" CLM01="3"/>
<MEM memId="m13" CLM01="3"/>
<MEM memId="m14" CLM01="4"/>
<MEM memId="m15" CLM01="4"/>
<MEM memId="m16" CLM01="4"/>
<MEM memId="m17" CLM01="5"/>
<MEM memId="m18" CLM01="5"/>
</CUS>
</VEN>
<VEN vendorId= "v2">
<CUS custId = "c1">
<MEM memId="m4" CLM01="6"/>
<MEM memId="m5" CLM01="6"/>
<MEM memId="m6" CLM01="6"/>
<MEM memId="m7" CLM01="7"/>
<MEM memId="m8" CLM01="7"/>
</CUS>
<CUS custId = "c1">
<MEM memId="m33" CLM01="8"/>
<MEM memId="m44" CLM01="8"/>
<MEM memId="m55" CLM01="8"/>
<MEM memId="m66" CLM01="9"/>
<MEM memId="m77" CLM01="9"/>
<MEM memId="m88" CLM01="9"/>
<MEM memId="m99" CLM01="10"/>
<MEM memId="m11" CLM01="10"/>
</CUS>
</VEN>
I have an input xml which has the following structure,
<VEN>
<CUS>
<MEM>
<MEM>
<MEM>
</CUS>
<CUS>
<MEM>
<MEM>
<MEM>
</CUS>
each node occurs multiple times in input xml. There is an attribute @CLM01 in every MEM
node which has to be assigned a value depending upon every 20 occurences of MEM nodes. Starting from the first VEN/CUS, if there are 55 nodes of MEM then first 20 nodes will have @CLM01 value of '1' and for same VEN/CUS combination the next 20 nodes will have value '2' and remaining 15 nodes will have @CLM01 value '3'.
And for the next CUS(assuming this CUS has 30 MEM nodes) under the Same VEN the first twenty MEM nodes will have @CLM01 value of '4' and next ten MEM will have @CLM01 with value '5'. This goes on..... I hope i am clear what i am trying to achieve.
Thanks. is it possible? using xslt 1.0
Here is the sample input
<Data>
<VEN vendorId= "v1">
<CUS custId = "c1">
<MEM memId="m1" CLM01=""/>
<MEM memId="m2" CLM01=""/>
<MEM memId="m3" CLM01=""/>
<MEM memId="m4" CLM01=""/>
<MEM memId="m5" CLM01=""/>
</CUS>
<CUS custId = "c2">
<MEM memId="m11" CLM01=""/>
<MEM memId="m12" CLM01=""/>
<MEM memId="m13" CLM01=""/>
<MEM memId="m14" CLM01=""/>
<MEM memId="m15" CLM01=""/>
<MEM memId="m16" CLM01=""/>
<MEM memId="m17" CLM01=""/>
<MEM memId="m18" CLM01=""/>
</CUS>
</VEN>
<VEN vendorId= "v2">
<CUS custId = "c1">
<MEM memId="m4" CLM01=""/>
<MEM memId="m5" CLM01=""/>
<MEM memId="m6" CLM01=""/>
<MEM memId="m7" CLM01=""/>
<MEM memId="m8" CLM01=""/>
</CUS>
<CUS custId = "c1">
<MEM memId="m33" CLM01=""/>
<MEM memId="m44" CLM01=""/>
<MEM memId="m55" CLM01=""/>
<MEM memId="m66" CLM01=""/>
<MEM memId="m77" CLM01=""/>
<MEM memId="m88" CLM01=""/>
<MEM memId="m99" CLM01=""/>
<MEM memId="m11" CLM01=""/>
</CUS>
</VEN>
The Desired output (instead of 20 occurences of MEM nodes we go for every 3 occurences)
<Data>
<VEN vendorId= "v1">
<CUS custId = "c1">
<MEM memId="m1" CLM01="1"/>
<MEM memId="m2" CLM01="1"/>
<MEM memId="m3" CLM01="1"/>
<MEM memId="m4" CLM01="2"/>
<MEM memId="m5" CLM01="2"/>
</CUS>
<CUS custId = "c2">
<MEM memId="m11" CLM01="3"/>
<MEM memId="m12" CLM01="3"/>
<MEM memId="m13" CLM01="3"/>
<MEM memId="m14" CLM01="4"/>
<MEM memId="m15" CLM01="4"/>
<MEM memId="m16" CLM01="4"/>
<MEM memId="m17" CLM01="5"/>
<MEM memId="m18" CLM01="5"/>
</CUS>
</VEN>
<VEN vendorId= "v2">
<CUS custId = "c1">
<MEM memId="m4" CLM01="6"/>
<MEM memId="m5" CLM01="6"/>
<MEM memId="m6" CLM01="6"/>
<MEM memId="m7" CLM01="7"/>
<MEM memId="m8" CLM01="7"/>
</CUS>
<CUS custId = "c1">
<MEM memId="m33" CLM01="8"/>
<MEM memId="m44" CLM01="8"/>
<MEM memId="m55" CLM01="8"/>
<MEM memId="m66" CLM01="9"/>
<MEM memId="m77" CLM01="9"/>
<MEM memId="m88" CLM01="9"/>
<MEM memId="m99" CLM01="10"/>
<MEM memId="m11" CLM01="10"/>
</CUS>
</VEN>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此转换:
应用于提供的 XML 文档时:
产生所需的正确结果:
说明:
两遍变换。
第一遍添加假
MEM
元素,以便结果中的每个CUS
元素都有MEM
子元素,其数量是的倍数>$pSize
。第二遍:
使用和覆盖身份规则。
使用< /code>
使用
level="any"
获取MEM
元素的正确编号。This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
Explanation:
Two pass transformation.
The first pass adds fake
MEM
elements so that eachCUS
element in the result hasMEM
children whose number is a multiple of$pSize
.The second pass:
Using and overriding the identity rule.
Using
<xsl:number>
to get the correct numbering ofMEM
elements withlevel="any"
.只是为了好玩,一次转换:
输出:
注意:混合子节点和兄弟节点遍历。隧道参数。
Just for fun, one pass transformation:
Output:
Note: Mixing children and siblings traversal. Tunneling parameters.