我有一些 XML 描述书籍如下:
<root>
<chapter>
<chapter_number>some chapter</chapter_number>
<chapter_title>some title</chapter_title>
<heading_1>some heading</heading_1>
<para>some plain text <italic>some italic text</italic> some more text</para>
<list_1_letter>item 1</list_1_letter>
<list_1_letter>item 2</list_1_letter>
<list_2_bullet>sub-item 1</list_2_bullet>
<list_2_bullet>sub-item 2</list_2_bullet>
<list_1_letter>item 3</list_1_letter>
<para>some other text</para>
<list_1_number>item 1</list_1_number>
<list_2_roman>sub-item 1</list_2_roman>
<list_2_roman>sub-item 2</list_2_roman>
<list_1_number>item 2</list_1_number>
<list_2_roman>sub-item 3</list_2_roman>
<list_2_roman>sub-item 4</list_2_roman>
</chapter>
</root>
列表需要包装在描述它们的元素中,但子列表应该是它们上方列表的子列表。在这种情况下,所需的输出将是:
<root>
<chapter>
<chapter_number>some chapter</chapter_number>
<chapter_title>some title</chapter_title>
<heading_1>some heading</heading_1>
<para>some plain text <italic>some italic text</italic> some more text</para>
<letter_list>
<list_1_letter>item 1</list_1_letter>
<list_1_letter>item 2</list_1_letter>
<bullet_list>
<list_2_bullet>sub-item 1</list_2_bullet>
<list_2_bullet>sub-item 2</list_2_bullet>
</bullet_list>
<list_1_letter>item 3</list_1_letter>
</letter_list>
<para>some other text</para>
<number_list>
<list_1_number>item 1</list_1_number>
<roman_list>
<list_2_roman>sub-item 1</list_2_roman>
<list_2_roman>sub-item 2</list_2_roman>
</roman_list>
<list_1_number>item 2</list_1_number>
<roman_list>
<list_2_roman>sub-item 3</list_2_roman>
<list_2_roman>sub-item 4</list_2_roman>
</roman_list>
</number_list>
</chapter>
</root>
从这里调整一些其他答案我可以包装列表,但我只能包装相同列表的序列,它将产生:
<number_list>
<list_1_number>item 1</list_1_number>
</number_list>
<roman_list>
<list_2_roman>sub-item 1</list_2_roman>
<list_2_roman>sub-item 2</list_2_roman>
</roman_list>
<number_list>
<list_1_number>item 2</list_1_number>
</number_list>
<roman_list>
<list_2_roman>sub-item 3</list_2_roman>
<list_2_roman>sub-item 4</list_2_roman>
</roman_list>
不完全是我所追求的!事实证明,这比我在接受这个时所预期的要复杂得多。