?
Solved

reformat xml using XSLT

Posted on 2004-10-16
3
Medium Priority
?
687 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 15

Accepted Solution

by:
dualsoul earned 2000 total points
ID: 12329360
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

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

771 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