Creating parent-child elements from semantic hiearchy in element values in XSLT 2

試著忘記壹切 提交于 2019-12-01 10:31:33

First phase: transform

<p>(2)(a) blah</p>
<p>(b) blah</p>

into

<p>(2)</p>
<p>(a) blah</p>
<p>(b) blah</p>

using something like

<xsl:template match="p">
  <xsl:for-each select="tokenize(., '\(')">
     <xsl:if test="normalize-space(.)">
       <p>(<xsl:value-of select="."/></p>
     </xsl:if>
  </xsl:for-each>
</xsl:template>

Second phase:

First write a function

<xsl:function name="f:level" as="xs:integer">
  <xsl:param name="p" as="element(p)"/>
  ....
</xsl:function>

which computes the "semantic level" based on matching your regular expressions. You seem to know how to do this part.

Then write a recursive grouping function:

<xsl:function name="f:group" as="element(p )*">
  <xsl:param name="in" as="element(p )*"/>
  <xsl:param name="level" as="xs:integer"/>
  <xsl:for-each-group select="$in" group-starting-with="p[f:level(.)=$level]">
    <p><xsl:value-of select="current-group()[1]"/>
      <xsl:sequence select="f:group(current-group()[position() gt 1], $level+1)"/>
    </p>
  </xsl:for-each-group>
</xsl:function>

and call this function like this:

<xsl:template match="content">
  <xsl:sequence select="f:group(p, 1)"/>
</xsl:template>

Not tested.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!