Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11751

XSLT 2 or 3 - Add structure to a flat XML file using for-each-group : group-starting-with

$
0
0

I've been given an Verity .otl file and have been asked to "decode" so all the rules can be remade.

I naturally thought take the text file and convert it to XML and process it. My first pass creates XML and adds useful attributes. Here is an example of the output of my 1st pass.

<?xml version="1.0" encoding="UTF-8"?><root>  <line pos="1" Lv="1" val="# topic_OST_Agri_export |Or|"/><line pos="2" Lv="2" val="## topic_OST_Export_contracts |Or|"/><line pos="3" Lv="3" val="### |Phrase|"/><line pos="4" Lv="4" val="#### ~export~"/><line pos="5" Lv="4" val="#### |Wildcard| /wordtext = ~contract#~"/><line pos="6" Lv="3" val="### ~contract for the export~"/><line pos="7" Lv="3" val="### |Phrase|"/><line pos="8" Lv="4" val="#### ~contract~"/><line pos="9" Lv="4" val="#### ~for~"/><line pos="10" Lv="4" val="#### |Wildcard| /wordtext = ~export#~"/><line pos="11" Lv="2" val="## topic_OST_Export_figures |Or|"/><line pos="12" Lv="3" val="### |Phrase|"/><line pos="13" Lv="4" val="#### ~export~"/><line pos="14" Lv="4" val="#### |Wildcard| /wordtext = ~figure#~"/><line pos="15" Lv="3" val="### ~export statistics~"/><line pos="16" Lv="3" val="### |Phrase|"/><line pos="17" Lv="4" val="#### ~Agriculturle~"/><line pos="18" Lv="4" val="#### |Wildcard| /wordtext = ~export#~"/><line pos="19" Lv="4" val="#### |Wildcard| /wordtext = ~increas#~"/><line pos="20" Lv="3" val="### |Phrase|"/><line pos="21" Lv="4" val="#### ~defence~"/><line pos="22" Lv="4" val="#### |Wildcard| /wordtext = ~export#~"/><line pos="23" Lv="4" val="#### |Wildcard| /wordtext = ~reach#~"/><line pos="24" Lv="3" val="### |Near/6|"/><line pos="25" Lv="4" val="#### |Phrase|"/><line pos="26" Lv="5" val="##### ~Agri~"/><line pos="27" Lv="5" val="##### |Wildcard| /wordtext = ~export#~"/><line pos="28" Lv="4" val="#### |Any|"/><line pos="29" Lv="5" val="##### ~statistics~"/><line pos="30" Lv="5" val="##### ~trends~"/><line pos="31" Lv="5" val="##### ~figures~"/><line pos="32" Lv="5" val="##### |Wildcard| /wordtext = ~annual#~"/><line pos="33" Lv="5" val="##### ~per cent~"/><line pos="34" Lv="5" val="##### ~million~"/><line pos="35" Lv="5" val="##### ~billion~"/><line pos="36" Lv="2" val="## ~Agri export~"/><line pos="37" Lv="2" val="## ~Agri exports~"/><line pos="38" Lv="2" val="## ~exporting Agri~"/></root>

This is just one snippet of a file with 70k lines.

I'd like to convert this into an xml structure that groups the lines according to their @Lv

An example from above would be ( @Type and @calledFrom are added by me in my unsuccessful attempts to understand the data and achieve the goal)

<line pos="1" Lv="1" val="# topic_OST_Agri_export |Or|"><line pos="2" Lv="2" val="## topic_OST_Export_contracts |Or|" Type="Topic"           calledFrom="1"><line pos="3" Lv="3" val="### |Phrase|" Type="phrase"><line pos="4" Lv="4" val="#### ~export~" Type="Word"/><line pos="5" Lv="4" val="#### |Wildcard| /wordtext = ~contract#~" Type="Wildcard"/><line pos="6" Lv="3" val="### ~contract for the export~" Type="Word"/><line pos="7" Lv="3" val="### |Phrase|" Type="phrase"><line pos="8" Lv="4" val="#### ~contract~" Type="Word"/><line pos="9" Lv="4" val="#### ~for~" Type="Word"/><line pos="10" Lv="4" val="#### |Wildcard| /wordtext = ~export#~" Type="Wildcard"/></line></line></line>

What have I already failed at?

Recursion: xsl:Chosing on different @Types. This looked promising but failed due to gobbling up subsequent lines when they shouldn't have.

I feel group-starting-with is the answer to structure the data for further processing and understanding but after a day I just don't "get" group-starting-with.

The last post I read on Stack Overflow on a similar question ended

"The trick is really understanding that group-starting-with shall be apattern not a condition."BlockquoteBlockquote

Source: grouping-xml-elements-using-xslts-for-each-group-and-group-starting-with-attrib

Any guidance on how to test following siblings Attribute and test if they are required in the group would be, of course most welcome.


Viewing all articles
Browse latest Browse all 11751

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>