I have XML structured in the following way:
<content>
<sh1>A</sh1>
<sh2>A1</sh2>
<sh2>A2</sh2>
<sh1>B</sh1>
<sh2>B1</sh2>
<sh2>B2</sh2>
<sh2>B3</sh2>
<sh1>C</sh1>
<sh2>C1</sh2>
<sh2>C2</sh2>
<sh1>D</sh1>
<sh2>D1</sh2>
<sh2>D2</sh2>
<sh2>D3</sh2>
<sh2>D4</sh2>
</content>
As you can see there are two tag names of concern here: sh1 and sh2. These represent headers and sub-headers, and I need to organize them as such in the output XML by nesting their contents (you can see example output here). My approach so far has been to match each sh1, and then try to match each sh2 between it and the next sh1.
I found this super helpful question which has gotten me (I hope) most of the way there. I've thrown together the following XSLT (1.0):
<xsl:template match="content">
<ul>
<xsl:apply-templates select="//sh1" />
</ul>
</xsl:template>
<xsl:template match="sh1">
<li>
<h1><xsl:value-of select="." /></h1>
<ul>
<xsl:apply-templates select="./following-sibling::sh1[1]/preceding-sibling::sh2[preceding-sibling::.]" />
</ul>
</li>
</xsl:template>
<xsl:template match="sh2">
<li><xsl:value-of select="." /></li>
</xsl:template>
The problem (predictably) is that I can't have the XPath ./following-sibling::sh1[1]/preceding-sibling::sh2[preceding-sibling::.] using . signify that the sh2 should have the current node as a preceding sibling. Is there some other keyword I could use? Or perhaps another approach entirely to get the nested structure I'm going for?