Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

reformat xml using XSLT

Posted on 2004-10-16
3
Medium Priority
?
693 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

610 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