Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Merge Two XML docments with XSLT

From: "Daniel" <softwareengineer98037@-----.--->
To: NULL
Date: 9/8/2004 2:21:00 PM
Is there any way to make it so that the attributes are not persisted if they
have no entry in the maps.xml?

That worked accept the result it returned was:

<?xml version="1.0" encoding="utf-8"?>
<top>
   <foo x="1" y="2"/>
   <bar x="1" y="2"/>
   <cars>
      <automobile red="1"/>
   </cars>
   <houses>
      <close deck="true" frontdoor="open"/>
   </houses>
</top>

but the expected result was:

<top>
 <foo x="1" y="2"/>
 <bar x="1" y="2"/>
 <cars>
  <automobile red="1"/>
 </cars>
 <houses>
  <close deck="true"/>
 </houses>
</top>

For my case I do not want  frontdoor="open" to be persisted because it has
no entry in the maps document:

<maps>
 <element source="car" target="automobile">
  <attribute source="blue" target="red"/>
  <attribute source="right" target="left"/>
 </element>
 <element source="house" target="close">
  <attribute source="balcony" target="deck"/>
 </element>
</maps>

Everything else looks perfect.

"Marrow" <m--a-r-r-o-w@m-a-r-r-o-w--s-o-f-t.com> wrote in message
news:eMAl%23VakEHA.3912@T......
> Hi Daniel,
>
> Try something like...
>
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:output method="xml" indent="yes"/>
> <xsl:variable name="maps" select="document('maps.xml',/)/maps/element"/>
>
> <xsl:template match="/">
>   <xsl:apply-templates/>
> </xsl:template>
>
> <xsl:template match="*">
>   <xsl:variable name="this-element-map" select="$maps[@source =
> local-name(current())]"/>
>   <xsl:choose>
>     <xsl:when test="$this-element-map">
>       <xsl:element name="{$this-element-map/@target}">
>         <xsl:apply-templates select="@*" mode="mapped">
>           <xsl:with-param name="attributes-map"
> select="$this-element-map/attribute"/>
>         </xsl:apply-templates>
>         <xsl:apply-templates/>
>       </xsl:element>
>     </xsl:when>
>     <xsl:otherwise>
>       <xsl:copy>
>         <xsl:apply-templates select="@* | node()"/>
>       </xsl:copy>
>     </xsl:otherwise>
>   </xsl:choose>
> </xsl:template>
>
> <xsl:template match="@*" mode="mapped">
>   <xsl:param name="attributes-map"/>
>   <xsl:variable name="this-attribute-map" select="$attributes-map[@source
=
> local-name(current())]"/>
>   <xsl:choose>
>     <xsl:when test="$this-attribute-map">
>       <xsl:attribute name="{$this-attribute-map/@target}">
>         <xsl:value-of select="."/>
>       </xsl:attribute>
>     </xsl:when>
>     <xsl:otherwise>
>       <xsl:copy/>
>     </xsl:otherwise>
>   </xsl:choose>
> </xsl:template>
>
> <xsl:template match="@* | text() | comment() | processing-instruction()">
>   <xsl:copy/>
> </xsl:template>
> </xsl:stylesheet>
>
>
> HTH
> Marrow
> http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
> http://www.topxml.com/Xselerator
>
> "Daniel" <softwareengineer98037@y...> wrote in message
> news:%23EzPpOWkEHA.524@T......
> > Is this possible with an XSLT to merge these two XML documents and get
the
> > third document? If so any help, guidence, XSLT would be much
appreciated!
> >
> > Maps Document:
> >
> > <maps>
> >  <element source="car" target="automobile">
> >   <attribute source="blue" target="red"/>
> >   <attribute source="right" target="left"/>
> >  </element>
> >  <element source="house" target="close">
> >   <attribute source="balcony" target="deck"/>
> >  </element>
> > </maps>
> >
> >
> > Data Document:
> >
> > <top>
> >  <foo x="1" y="2"/>
> >  <bar x="1" y="2"/>
> >  <cars>
> >   <car blue="1"/>
> >  </cars>
> >  <houses>
> >   <house balcony="true" frontdoor="open"/>
> >  </houses>
> > </top>
> >
> >
> > Desired Target Document After XSLT Transformation
> >
> > <top>
> >  <foo x="1" y="2"/>
> >  <bar x="1" y="2"/>
> >  <cars>
> >   <automobile red="1"/>
> >  </cars>
> >  <houses>
> >   <house deck="true"/>
> >  </houses>
> > </top>
> >
> >
>
>




transparent
Print
Mail
Digg
delicious
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