Skip to Main Content

DevOps, CI/CD and Automation

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

XML to HTML using XSLT

VP2Aug 12 2016 — edited Aug 16 2016

Hello, we receive an XML file from Oracle. In this file, unfortunately one of the hierarchies is created in a not exactly standard format, although it is still valid. Here is an example:

<topics>

  <topic>Topic 1

    <sub_topics>

      <sub_topic>Sub topic 1_1</sub_topic>

      <sub_topic>Sub topic 1_2 </sub_topic>

   </sub_topics>

  </topic>

  <topic>Topic 2

    <sub_topics>

      <sub_topic>Sub topic 2_1</sub_topic>

      <sub_topic>Sub topic 2_2 </sub_topic>

   </sub_topics>

  </topic>

<topics>

As you can see "Topic 1" and "Topic 2" are not rapped into their own tags, for instance <hdr>Topic 1</hdr>. I would consider this way more standard

Currently, if I apply the following XSLT snippet to this section:

p><i>Topics: </i></p>

  <xsl:for-each select="topics">

     <ul>

        <xsl:for-each select="topic">

           <li><xsl:value-of select="." /></li>

        </xsl:for-each>

     </ul>

    </xsl:for-each>

The HTML result is:

Topics:

  • Topic 1 Topic 1_1 Topic 1_2 
  • Topic 2 Topic 2_1 Topic 2_2 

If I wrap the topics into a set of additional tags, like that

<topics>

  <topic><hdr>Topic 1</hdr>

    <sub_topics>

      <sub_topic>Sub topic 1_1</sub_topic>

      <sub_topic>Sub topic 1_2 </sub_topic>

   </sub_topics>

  </topic>

  <topic><hdr>Topic 2</hdr>

    <sub_topics>

      <sub_topic>Sub topic 2_1</sub_topic>

      <sub_topic>Sub topic 2_2 </sub_topic>

   </sub_topics>

  </topic>

<topics>

And modify my XSLT like that:

<p><i>Topics: </i></p>

<xsl:for-each select="topics/topic">

<xsl:for-each select="hdr">

<p><xsl:value-of select="." /></p>

</xsl:for-each>

<ul>

<xsl:for-each select="sub_topics/sub_topic">

<li><xsl:value-of select="." /></li>

</xsl:for-each>

</ul>

</xsl:for-each>

The result is fine:

Topics:

Topic 1

  • Topic 1_1
  • Topic 1_2 

Topic 2

  • Topic 2_1
  • Topic 2_2 

Question: I would prefer to change the XSL to handle this exception rather than writing a parser in PLSQL and adding the new tags, which I can do for sure. I would appreciate if one of XSLT aces show me the way how to modify my XSL file.

Thanks in advance

<topics>

  <topic>Topic 1

    <sub_topics>

      <sub_topic>Sub topic 1_1</sub_topic>

      <sub_topic>Sub topic 1_2 </sub_topic>

   </sub_topics>

  </topic>

  <topic>Topic 2

    <sub_topics>

      <sub_topic>Sub topic 2_1</sub_topic>

      <sub_topic>Sub topic 2_2 </sub_topic>

   </sub_topics>

  </topic>

<topics>

This post has been answered by odie_63 on Aug 13 2016
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 13 2016
Added on Aug 12 2016
2 comments
403 views