开发者

Conversion of XML data using XSLT

开发者 https://www.devze.com 2023-03-20 05:06 出处:网络
I have the following mark up <ul> <li>this is bullet list</li> <li>this is another bullet</li>

I have the following mark up

       <ul>
              <li>this is bullet list</li>
              <li>this is another bullet</li>
              <ul>
                  <li>this is embaded</li>
                  <li>this is embaded</li>
                     <ul>
                        <li>this开发者_如何学JAVA is furthur embaded</li>
                     </ul>
               </ul>
        </ul>

and i need a xslt script to transform into

<xml>
   <unorderlist>
   <list><text>this is bullet list</text></list>
   <list><para><text>this is another bullet</text>
     <unorderlist>
       <list><text>this is embaded</text></list>
       <list><para><text>this is embaded</text>
           <unorderlist>
              <list><text>this is furthur embabed</text></list>
            </unorderlist>
           </para></list>
        </unorderlist>
     </para>
   </list>
</unorderlist>
</xml>

Basically all the nested items should appear within the last node tag. Any hint will be really appreciated.


For example

<xsl:template match="/">
  <xml>
    <xsl:apply-templates/>
  </xml>
</xsl:template>

<xsl:template match="ul">
  <unorderlist>
    <xsl:apply-templates select="li"/>
  </unorderlist>
</xsl:template>

<xsl:template match="li">
  <list>
    <xsl:choose>
      <xsl:when test="following-sibling::*[1]/self::ul">
        <para>
          <text>
            <xsl:apply-templates/>
          </text>
          <xsl:apply-templates select="following-sibling::*[1]/self::ul"/>
        </para>
      </xsl:when>
      <xsl:otherwise>
        <text>
          <xsl:apply-templates/>
        </text>
      </xsl:otherwise>
    </xsl:choose>
  </list>
</xsl:template>

If you want to support more complex input, use a separate mode to pull the <ul> to preceding <li>.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号