Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: xsl copy

From: "Dimitre Novatchev" <dnovatchev@-----.--->
To: NULL
Date: 11/12/2008 7:27:00 PM
"Gan Kol" <gankol@g...> wrote in message 
news:uYgPV9PRJHA.5080@T......
> Hi,
>
> I have an xml something like this.
> <Test>
>    <Type>New</Type>
>    <Lang>
>       <English>
>            <Screenplay>test11</Screenplay>
>            <Script>script11</Script>
>         </English>
>       <ExpiryDate>2009-10-10</ExpiryDate>
>    </Lang>
>    <StudentNumber>1234333</StudentNumber>
>    <RequestDate>2008-10-10</RequestDate>
>    <FirstName>David</FirstName>
>    <MiddleName>-</MiddleName>
>    <LastName>Burges</LastName>
>    <Qualifier>-</Qualifier>
>    <Status></Status>
>  </Test>
>
> I would like to have an output xml as the same structure as above, but
> Omit the elements if a "-" is the only data in the element (for eg. should 
> keep the date element expiry date, but omit MiddleName, Qualifier)
> Since i dont know about the xml data and structure inside the xml when 
> doing the xsl transformation, i tried using xsl copy instead of select,
> but didnt know how to check for the conditions.


The following override of the identity transformation is a natural solution:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match=
    "*[text() and not(node()[not(self::text())])]
                       [normalize-space() = '-']"/>
</xsl:stylesheet>

When applied on the originally specified xml document:

<Test>
 <Type>New</Type>
 <Lang>
  <English>
   <Screenplay>test11</Screenplay>
   <Script>script11</Script>
  </English>
  <ExpiryDate>2009-10-10</ExpiryDate>
 </Lang>
 <StudentNumber>1234333</StudentNumber>
 <RequestDate>2008-10-10</RequestDate>
 <FirstName>David</FirstName>
 <MiddleName>-</MiddleName>
 <LastName>Burges</LastName>
 <Qualifier>-</Qualifier>
 <Status></Status>
</Test>

the wanted result is produced:

<Test>
   <Type>New</Type>
   <Lang>
      <English>
         <Screenplay>test11</Screenplay>
         <Script>script11</Script>
      </English>
      <ExpiryDate>2009-10-10</ExpiryDate>
   </Lang>
   <StudentNumber>1234333</StudentNumber>
   <RequestDate>2008-10-10</RequestDate>
   <FirstName>David</FirstName>
   <LastName>Burges</LastName>
   <Status/>
</Test>

Hope this helped.

Cheers,
Dimitre Novatchev 




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent