Ramesh Singh
Ramesh Singh

Reputation: 41

XSLT copying AS IS

i have xml i want to copy as is like (check xmlns="" and tags. i want to create as is . the total calculation is taken care. only this issue . it is valid . still client want the expected format to be like that. any help greatly appreciated.

source.xml

       <Employees>
       <employee>
        <dept>1</dept>
        <sec></sec>
         <employee>
          <employee>
           <dept>2</dept>
               <sec></sec>
            <employee>
            </Employees>

Expectedresult.xml

                  <Employees xmnls="1.2" xmlns:xsi="3" xsi:schemalocation="4">
                    <totalemp>2</totalemp>
                   <employee>
                     <dept>1</dept>
                      <sec></sec>
                     <employee>
                      <employee>
                           <dept>2</dept>
                                  <sec></sec>
                         <employee>
                        </Employees>

Actual result

                         <Employees>
                              <totalemp>2</totalemp>
                               <employee xmlns="">
                                <dept>1</dept>
                                  <sec/>
                                </employee>
                                 <employee>
                                   <dept>2</dept>
                                      <sec/>
                                   <employee>
                                  </Employees>

Upvotes: 0

Views: 109

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52848

In order to get <sec/> to output like <sec></sec>, try adding method="html" to your xsl:output (if you have one?).

Example:

XML Input (well-formed):

<Employees>
  <employee>
    <dept>1</dept>
    <sec/>
  </employee>
  <employee>
    <dept>2</dept>
    <sec/>
  </employee>
</Employees>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes" method="html"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="Employees">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <totalemp><xsl:value-of select="count(employee)"/></totalemp>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

XML Output (tested with Xalan and Saxon 6.5.5)

<Employees>
   <totalemp>2</totalemp>
   <employee>
      <dept>1</dept>
      <sec></sec>
   </employee>
   <employee>
      <dept>2</dept>
      <sec></sec>
   </employee>
</Employees>

Upvotes: 1

Related Questions