Solved

reformat xml using XSLT

Posted on 2004-10-16
3
677 Views
Last Modified: 2013-11-19
I have an xml which is showing like this ( the inner tags also has a line feed ) :

for example :
<EMPLID>
      9999
 </EMPLID>
should be like :
 <EMPLID>9999</EMPLID>

======================================
<PDTOOL_DATA>
  <EVALUATIONS>
    <EMPLID>
      9999
    </EMPLID>
    <REVIEW_DT>
      2004/09/01
    </REVIEW_DT>
  </EVALUATIONS>
   <CM_EVAL_JR>
    <EMPLID>
      9999
    </EMPLID>      
    <JOBCODE>
      054321
    </JOBCODE>      
   </CM_EVAL_JR>
   <COMPETENCIES>
    <EMPLID>
      9999
    </EMPLID>
    <COMPETENCY>
      014129
    </COMPETENCY>
    <EFFDT>
      2004/09/01
    </EFFDT>
    <PROFICIENCY>
      2
     </PROFICIENCY>
   </COMPETENCIES>
</PDTOOL_DATA>


I want to reformat it like this ( Can any one  help me with the XSLT for it )


<PDTOOL_DATA>
  <EVALUATIONS>
    <EMPLID>9999</EMPLID>
    <REVIEW_DT>2004/09/01</REVIEW_DT>
  </EVALUATIONS>
   <IBM_CM_EVAL_JR>
    <EMPLID>9999</EMPLID>      
    <JOBCODE>054321</JOBCODE>      
  </IBM_CM_EVAL_JR>
   <COMPETENCIES>
    <EMPLID>9999</EMPLID>
    <COMPETENCY>014129</COMPETENCY>
    <EFFDT>2004/09/01</EFFDT>
    <PROFICIENCY>2</PROFICIENCY>
  </COMPETENCIES>
</PDTOOL_DATA>


0
Comment
Question by:royalcyber
3 Comments
 
LVL 15

Accepted Solution

by:
dualsoul earned 500 total points
Comment Utility
hi, royalcyber

did we finish with this one:  http://www.experts-exchange.com/Web/Web_Languages/XML/Q_21169765.html

?

here is the XSLT i'm using for pretty printing, it hanles empty tags, tags with simle text content, and tags with element content:
.....................................................................
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
      <xsl:template match="/">
            <xsl:value-of select="'&#13;&#10;'"/>
            <xsl:apply-templates />
      </xsl:template>
   
   <!-- empty tags -->      
      <xsl:template match="*[not(child::*)][not(text())]">
            <xsl:param name="tab"/>
            <xsl:value-of select="$tab"/>
            <xsl:call-template name="openingTag" />
            <!-- close tag immediately  -->
            <xsl:text disable-output-escaping="yes">/&gt;</xsl:text>
            <xsl:value-of select="'&#13;&#10;'"/>
      </xsl:template>
      
      <!-- tags with text content -->
      <xsl:template match="*[not(child::*)][child::text()]">
            <xsl:param name="tab"/>
            <xsl:value-of select="$tab"/>
            <xsl:call-template name="openingTag" />
            <xsl:value-of select="normalize-space(text())"/>
            <xsl:call-template name="closingTag" />
      </xsl:template>
      
   <!-- all other cases -->
      <xsl:template match="*">
            <xsl:param name="tab"/>
            <xsl:value-of select="$tab"/>
            <xsl:call-template name="openingTag" />
            <xsl:value-of select="'&#13;&#10;'"/>
            
            <!-- process childs -->
            <xsl:apply-templates select="text() | node()">
                  <xsl:with-param name="tab" select="concat($tab,'    ')"/>
            </xsl:apply-templates>
            <xsl:value-of select="$tab"/>
            
            <xsl:call-template name="closingTag" />
      </xsl:template>
      
      <!-- outputs content of text nodes, according to pretty-print format -->
      <xsl:template match="text()">
            <xsl:param name="tab"/>
            <xsl:value-of select="$tab"/>
            <xsl:value-of select="normalize-space(.)"/>
      </xsl:template>
      
      <!-- renders opening tag -->
      <xsl:template name="openingTag">
            <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
            <xsl:value-of select="name()"/>
            <xsl:for-each select="attribute::*">
                  <xsl:text> </xsl:text>
                  <xsl:value-of select="name()"/>="<xsl:value-of select="."/>"
        </xsl:for-each>
            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>                  
      </xsl:template>
      <!-- renders closing tag -->
      <xsl:template name="closingTag">
            <xsl:text disable-output-escaping="yes">&lt;/</xsl:text>
            <xsl:value-of select="name()"/>
            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
            <xsl:value-of select="'&#13;&#10;'"/>
      </xsl:template>
</xsl:stylesheet>
.....................................................................

, for this input.xml:
<?xml version="1.0" encoding="UTF-8"?>
<PDTOOL_DATA>
      <a />
      <b></b>
  <EVALUATIONS>
    <EMPLID>
     9999
    </EMPLID>
    <REVIEW_DT>
     2004/09/01
    </REVIEW_DT>
  </EVALUATIONS>
   <CM_EVAL_JR>
    <EMPLID>
     9999
    </EMPLID>    
    <JOBCODE>
     054321
    </JOBCODE>    
   </CM_EVAL_JR>
   <COMPETENCIES>
    <EMPLID>
     9999
    </EMPLID>
    <COMPETENCY>
     014129
    </COMPETENCY>
    <EFFDT>
     2004/09/01
    </EFFDT>
    <PROFICIENCY>
     2
     </PROFICIENCY>
   </COMPETENCIES>
</PDTOOL_DATA>

, it will reformat it this way:

<?xml version="1.0" encoding="UTF-8"?>
<PDTOOL_DATA>
    <a>/>
    <b>/>
    <EVALUATIONS>
        <EMPLID>9999</EMPLID>
        <REVIEW_DT>2004/09/01</REVIEW_DT>
    </EVALUATIONS>
    <CM_EVAL_JR>
        <EMPLID>9999</EMPLID>
        <JOBCODE>054321</JOBCODE>
    </CM_EVAL_JR>
    <COMPETENCIES>
        <EMPLID>9999</EMPLID>
        <COMPETENCY>014129</COMPETENCY>
        <EFFDT>2004/09/01</EFFDT>
        <PROFICIENCY>2</PROFICIENCY>
    </COMPETENCIES>
</PDTOOL_DATA>


, does it suites you?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now