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
Solved

reformat xml using XSLT

Posted on 2004-10-16
3
683 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
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Import data into excel from web page 10 101
XML XSL Message 3 27
XML Removal- Powershell 4 28
Powershell script to zip up xml files in a directory 13 26
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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.
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 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…

792 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