1

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 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.

1 Answer 1

2

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>.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Jelovirt, just came up with one more senerio where i have OL instead of UL in the embedad element.? How to catch this and process?
How to handle OL and ol or combination. my markup is generated using freetextbox html editor so in some browsers the mark is generate as OL,UL,LI where as some of them ol, ul, li? is there a way we can get rid this case sensivity?
@atif: the easiest way is to change li to li | LI, etc., and change following-sibling::*[1]/self::ul to following-sibling::*[1][self::ul or self::UL].
>Thanks Jelovirt, just came up with one more senerio where i have OL instead of ... . Sadly, I fear you will come up with many more scenarios before you have a complete statement of your requirements. I suggest you spend some effort defining the requirements more precisely before attempting to write code (or asking others to write it for you).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.