Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. XML / XSL
  4. XSL to get only elements which have no childs

XSL to get only elements which have no childs

Scheduled Pinned Locked Moved XML / XSL
xmlhelpquestion
4 Posts 2 Posters 13 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrKBA
    wrote on last edited by
    #1

    Hi, I make my question after some research in web without any clear response to my problem. I have the following XML and I need to apply an XSL transformation to get only nodes which haven't child. Input :

    Expected output :

    Richard DeemingR 1 Reply Last reply
    0
    • M MrKBA

      Hi, I make my question after some research in web without any clear response to my problem. I have the following XML and I need to apply an XSL transformation to get only nodes which haven't child. Input :

      Expected output :

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Something like this should work:

      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="Root_Element">
      <xsl:element name="{name()}">
      <xsl:apply-templates select="*|@*"/>
      </xsl:element>
      </xsl:template>

      <xsl:template match="\*">
          <xsl:choose>
              <xsl:when test="count(\*) = 0">
                  <xsl:element name="{name()}">
                      <xsl:apply-templates select="@\*"/>
                  </xsl:element>
              </xsl:when>
              <xsl:otherwise>
                  <xsl:apply-templates select="\*" />
              </xsl:otherwise>
          </xsl:choose>
      </xsl:template>
      
      <xsl:template match="@\*">
          <xsl:attribute name="{name()}">
              <xsl:value-of select="."/>
          </xsl:attribute>
      </xsl:template>
      

      </xsl:stylesheet>


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 2 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        Something like this should work:

        <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="Root_Element">
        <xsl:element name="{name()}">
        <xsl:apply-templates select="*|@*"/>
        </xsl:element>
        </xsl:template>

        <xsl:template match="\*">
            <xsl:choose>
                <xsl:when test="count(\*) = 0">
                    <xsl:element name="{name()}">
                        <xsl:apply-templates select="@\*"/>
                    </xsl:element>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="\*" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
        
        <xsl:template match="@\*">
            <xsl:attribute name="{name()}">
                <xsl:value-of select="."/>
            </xsl:attribute>
        </xsl:template>
        

        </xsl:stylesheet>


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        M Offline
        M Offline
        MrKBA
        wrote on last edited by
        #3

        Yes thank you it works but how can I keep the root element please ? I means the (

        )

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Something like this should work:

          <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:template match="Root_Element">
          <xsl:element name="{name()}">
          <xsl:apply-templates select="*|@*"/>
          </xsl:element>
          </xsl:template>

          <xsl:template match="\*">
              <xsl:choose>
                  <xsl:when test="count(\*) = 0">
                      <xsl:element name="{name()}">
                          <xsl:apply-templates select="@\*"/>
                      </xsl:element>
                  </xsl:when>
                  <xsl:otherwise>
                      <xsl:apply-templates select="\*" />
                  </xsl:otherwise>
              </xsl:choose>
          </xsl:template>
          
          <xsl:template match="@\*">
              <xsl:attribute name="{name()}">
                  <xsl:value-of select="."/>
              </xsl:attribute>
          </xsl:template>
          

          </xsl:stylesheet>


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          M Offline
          M Offline
          MrKBA
          wrote on last edited by
          #4

          Yes great, thank you.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups