Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 9th, 2006, 01:25 PM
carrilho.paulo@gmail.com
Guest
 
Posts: n/a
Default XSLT Help

I've a project where the XML should be transformed.

If a tag exists in Output and Parameters, the Parameters one should be
replaced by the Output corresponding and its childs.

Example: (XML In):

<RESULT>
<PARAMETERS>
<CYCLE>
<ENDDATE></ENDDATE>
<NUMBER>50</NUMBER>
</CYCLE>
<FILE>
<CHECK>Yes</CHECK>
</FILE>
<DIR>
<NAME>ONE</NAME>
</DIR>
<RUNTYPE>SAMPLE</RUNTYPE>
</PARAMETERS>
<OUTPUT jobfactor="71">
<CYCLE>
<NAME>MARY<\NAME>
<SURNAME>ROSE<\SURNAME>
</CYCLE>
<FILE>
<FILENAME>E:\ftphome\vodafone\xml\CC20060215S50.XM L</FILENAME>
</FILE>
</OUTPUT>
</RESULT>

(XML Out):

<PARAMETERS>
<CYCLE>
<NAME>MARY<\NAME>
<SURNAME>ROSE<\SURNAME>
</CYCLE>
<FILE>
<FILENAME>E:\ftphome\vodafone\xml\CC20060215S50.XM L</FILENAME>
</FILE>
<DIR>
<NAME>ONE</NAME>
</DIR>
<RUNTYPE>SAMPLE</RUNTYPE>
</PARAMETERS>

I've done somtehing like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0"
encoding="iso-8859-1"></xsl:output>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="OUTPUT"></xsl:template>
<xsl:template match="/RESULT/PARAMETERS/FILE">
<xsl:copy-of select="/RESULT/OUTPUT/FILE" />
</xsl:template>
</xsl:stylesheet>

but this only works if FILE exists, it is not generic. Might you help
me, please?

Its urgent.

  #2  
Old March 9th, 2006, 04:45 PM
Andy Dingley
Guest
 
Posts: n/a
Default Re: XSLT Help


carrilho.paulo@gmail.com wrote:
[color=blue]
> If a tag exists in Output and Parameters, the Parameters one should be
> replaced by the Output corresponding and its childs.[/color]

It's impossible to answer this because you haven't defined what
"corresponds" means, Usually a problem like this has an id attribute
somewhere and it's a question of writing predicates that either match,
or exclude elements with matching ids.

In your example, is "correspondence" just based on position? Can there
be more than one "Output" or "Parameter" in the document ?

Tell us what "corresponds" means, and we can probably help you.

  #3  
Old March 9th, 2006, 06:55 PM
carrilho.paulo@gmail.com
Guest
 
Posts: n/a
Default Re: XSLT Help

Thank you in advance.

If and only if a tag from Parameters, exists in Output (with the same
name). This tag in Output should overwrite the tag with the same name
in Parameters.

<Parameters>
<A>
<B>One<B/>
<A/>
<D>
<E>Three<E/>
<D/>
<Parameters/>
<Output>
<A>
<C>Two<C/>
<A>
<Output/>

the XSLT should transforms it into:

<Parameters>
<A>
<C>Two<C/>
<A/>
<D>
<E>Three<E/>
<D/>
<Parameters/>

The A tag (and its childs) were copied from Output into Parameters.

  #4  
Old March 9th, 2006, 09:45 PM
Peter Flynn
Guest
 
Posts: n/a
Default Re: XSLT Help

carrilho.paulo@gmail.com wrote:[color=blue]
> Thank you in advance.
>
> If and only if a tag from Parameters, exists in Output (with the same
> name). This tag in Output should overwrite the tag with the same name
> in Parameters.
>
> <Parameters>
> <A>
> <B>One<B/>
> <A/>
> <D>
> <E>Three<E/>
> <D/>
> <Parameters/>
> <Output>
> <A>
> <C>Two<C/>
> <A>
> <Output/>[/color]

That isn't XML. Your end-tags are all defective.
[color=blue]
> the XSLT should transforms it into:
>
> <Parameters>
> <A>
> <C>Two<C/>
> <A/>
> <D>
> <E>Three<E/>
> <D/>
> <Parameters/>
>
> The A tag (and its childs) were copied from Output into Parameters.[/color]

Assuming you fix up your file, try this.

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

<xsl:output method="xml"/>

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

<xsl:template match="Parameters/*">
<xsl:choose>
<xsl:when test="../../Output/*[name()=current()/name()]">
<xsl:copy-of select="../../Output/*[name()=current()/name()]"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

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

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles