Make chat sheet from one element to another elemen

2019-09-23 15:40发布

Input File:

    <?xml version="1.0" encoding="UTF-8"?>
<root>
    <a>
        <notice>100</notice>
        <chat>10, 20, 30, 40</chat>
    </a>
    <a>
        <notice>101</notice>
        <chat>40, 50, 60</chat>
    </a>
    <a>
        <notice>102</notice>
        <chat>10, 30, 60</chat>
    </a>
    <a>
        <notice>103</notice>
        <chat>70, 10, 20</chat>
    </a>
</root>

My requirement is make a chat entry and find the effected notice like below example:

    <a>
    <chat>10</chat>
    <notice>100, 102, 103</notice>    
</a>

<a>
    <chat>20</chat>
    <notice>100, 103</notice>    
</a>

<a>
    <chat>30</chat>
    <notice>100, 102</notice>    
</a>

Note: I have to use 1.0 version of XSLT.

标签: xml xslt xpath
1条回答
爷的心禁止访问
2楼-- · 2019-09-23 16:29

If you are truly limited to XSLT 1.0, you will have to do this in three steps:

  1. Tokenize the chat values. Ideally, at the end of this step you would have a variable containing:

    <item notice="100" chat="10"/>
    <item notice="100" chat="20"/>
    <item notice="100" chat="30"/>
    <item notice="100" chat="40"/>
    <item notice="101" chat="40"/>
    <item notice="101" chat="50"/>
    <item notice="101" chat="60"/>
    <item notice="102" chat="10"/>
    <item notice="102" chat="30"/>
    <item notice="102" chat="60"/>
    <item notice="103" chat="70"/>
    <item notice="103" chat="10"/>
    <item notice="103" chat="20"/>
    
  2. Convert the variable into a node-set, using the EXSLT exsl:node-set() function (or another similar function supported by your processor).

  3. Group the items by the chat attribute, using Muenchian grouping.

There are plenty of examples here showing how to do each step. If you run into problems, post a specific question.

Note that some XSLT processors support some extension functions that could be helpful here - notably: str:tokenize() and set:distinct().

查看更多
登录 后发表回答