Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Copy certain nodes

From: Peter Flynn <peter.nosp@-.--------.-->
To: NULL
Date: 10/2/2006 12:25:00 AM

mark wrote:
> Peter Flynn napisal(a):
>> You're asking two different things here. Do you want:
>>
>> a) all <file> elements which contain <size> and <mode> elements,
>>     reproducing only those elements;
>> or
>> b) all <file> elements regardless, but reproducing only <size>
>>     and <mode> if they have them.
>>
>> Your data and query is ambiguous in this regard.
>>
>> ///Peter
> 
> Thanks for your replay.
> One time it would be <size> and <modified>, the oher time just <owner>.
> But always I want to preserve the source xml structure and contain in
> <file> all elements given as a parameter. I hope I explained thing out.

Close enough :-) This XSLT requires the parameter "include" to contain a
string value of the required element type names, separates by spaces.
There is probably a more elegant way but it's late.

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                 version="1.0">

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

   <xsl:param name="include"/>

   <xsl:template match="root">
     <root>
       <xsl:apply-templates/>
     </root>
   </xsl:template>

   <xsl:template match="file">
     <xsl:variable name="elements">
       <xsl:for-each select="*">
         <xsl:if test="contains($include,name())">
           <xsl:text>y</xsl:text>
         </xsl:if>
       </xsl:for-each>
     </xsl:variable>
     <xsl:if test="contains($elements,'y')">
       <file name="{@name}">
         <xsl:apply-templates select="*[contains($include,name())]"
           mode="included"/>
       </file>
     </xsl:if>
   </xsl:template>

   <xsl:template match="*" mode="included">
     <xsl:copy-of select="."/>
   </xsl:template>

</xsl:stylesheet>

How you pass the parameter depends on your XSLT processor and your 
operating environment. Using Saxon, I typed

java -jar /saxon/saxon8.jar -o test.new test.xml test.xsl 
"include=\"modified owner\""

which gave me the result in test.new:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <file name="AAA">
       <modified>2</modified>
       <owner>Tom</owner>
    </file>
    <file name="BBB">
       <modified>6</modified>
       <owner>Al</owner>
    </file>
    <file name="CCC">
       <modified>6</modified>
       <owner>Jack</owner>
    </file>
    <file name="DDD">
       <modified>4</modified>
       <owner>Jane</owner>
    </file>
</root>

///Peter
-- 
XML FAQ: http://xml.silmaril.ie/


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