Solved

reformat xml using XSLT

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Result Set to XML 4 50
tutorial for ebay api 3 79
Grunt script for Build Process 1 64
Xslt: how to use use for each to loop through four iterations only 4 37
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 was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

749 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