Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 9th, 2006, 09:41 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT between two XML files

Hallo together,

I have always used the XSLT to transfer an XML file into an XHTML or HTML page. My question is if there is a big difference when I try to XSLT from an XML data to another XML target (which has its Schema structure).

Is there any kind of a tutorial site which explains an XSL Transformation to an XML data?

Your attitude determines your altitude
__________________
Your attitude determines your altitude
 
Old July 9th, 2006, 01:06 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Transforming XML to XML should be pretty easy if you know how to transform XML to HTML or XHTML.

One thing to be aware of is that if the target XML uses the same vocabulary as the source, the right coding pattern is often a "modified identity transform", where you write an identity template for elements that need to be copied, and then add additional templates for elements that need to be modified. The identity template usually looks like this:

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 11th, 2006, 10:09 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael. Another question. I didn't want to open a new topic for it. Is it possible to reference 2 XML files in an XSL? I mean I was always working with one XML file and I was giving its DTD source and it was fine but is it possible to reference 2 DTD or Schema Files in one XSL? If it is possible could you please briefly give me an example?

Many thanks.

Your attitude determines your altitude
 
Old July 11th, 2006, 10:13 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Yes, just use the document() function.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 12th, 2006, 08:37 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks again Michael. Sorry that I am bothering you with my questions but I need your help again.

I just tried to build an XML file from 2 XML files with an XSLT. The XML files are


<?xml version="1.0" encoding="UTF-8"?>
<team xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="X:\Private\XML Examples\team.xsd">
<team>Galatasaray</team>
<team>Fenerbahce</team>
<team>Besiktas</team>
<team>Trabzonspor</team>
</team>


<?xml version="1.0" encoding="UTF-8"?>
<trainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="X:\Private\XML Examples\trainer.xsd">
    <trainer>Eric Gerets</trainer>
    <trainer>Zico</trainer>
    <trainer>Jean Tigana</trainer>
    <trainer>Sebastiao Lazaroni</trainer>
</trainer>



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="team">
        <team>
              <xsl:apply-templates select="document('trainer.xml')"/>

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

So I am now using XML Spy and when I try to transform it I am still seeing a not well formed HTML data (output.html) How can I generate an XML file from them which has the following structure
<team>...</team>
<trainer>...</trainer>

I don't want you to write the whole source code but a clue or hint would have been great.

Your attitude determines your altitude
 
Old July 12th, 2006, 10:22 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, I don't understand how you want to combine these files.

The simplest way to combine them is

<xsl:template match="/">
<out>
  <xsl:copy-of select="*/team"/>
  <xsl:copy-of select="document('trainers.xml')/*/trainer"/>
</out>
</xsl:template>

but I've no idea if that's what you want.

Why are you using team/trainer as both the wrapper element and the individual element within it? It makes life very confusing.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 13th, 2006, 02:25 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hallo Michael,
I just tried to make an example for me to combine two XML sources. Of course I am quite new with this concept and I saw an example which has combined 2 sources like that and that's why I have written the XSL file in that way. What my aim was to create a new XML file like

<team>Galatasaray</team>
<trainer>Eric Gerets</trainer>

Of course it is easy to write a simple XML file which has these two elements but I just wanted to combine 2 XML files to create a new XML file. That was my concern.

Another thing:

As I was running the XSLT under XML Spy I have received this error message

select="document('trainers.xml')/*/trainer"/>
this file is not valid. Although it has its schema.

Your attitude determines your altitude
 
Old July 13th, 2006, 06:38 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your input files have lots of teams and lots of trainers, how do you decide which team and which trainer to include in the output?

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 13th, 2006, 07:37 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ah I was completely blocked out today. I think I need a for-each loop to display these teams and trainers.

Your attitude determines your altitude
 
Old July 24th, 2006, 06:20 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hallo again,

I am now designing an XML which also uses another xml file named soccerleague.xml. (It is just a little example if I cope it I am gonna begin to do my project)

Anyways, these 2 xml files (soccerteam.xml, soccerleague.xml) has a same tag named teamid. What I am now trying to do is to match the ids and write teamid teamname, trainer from soccerteam.xml and league from soccerleague.xml. So I could write this

<tr>
    <td><xsl:value-of select ="soccerteam/team/teamid"/></td>
    <td><xsl:value-of select="soccerteam/team/@name"/></td>
    <td><xsl:value-of select="soccerteam/team/trainer"/></td>
    <td><xsl:value-of select="document('soccerleague.xml')//league"/></td>
</tr>

it works fine but what I couldn't have done is to write the for-each status which matches the two teamids from soccerteam.xml and soccerleague.xml so that I can call all the entries.

I have defined this table in a template match "/". I have also defined

<xsl:param name="id" select="document('soccerleague.xml')//teamid"/> before this template.

Can anyone help me?


Your attitude determines your altitude





Similar Threads
Thread Thread Starter Forum Replies Last Post
Loading XML files in XSLT newbieboobers XSLT 2 March 12th, 2008 11:42 AM
Can 1 xslt transform an xml doc into 2 text files Raju Sarode XSLT 7 November 3rd, 2006 04:10 PM
xslt differencing between two xml files dhollis XSLT 5 August 4th, 2006 08:49 AM
Compare two xml files using xslt sudha XSLT 0 March 10th, 2006 01:04 AM
Merge XML files into a xml file using xslt lxu XML 4 November 6th, 2003 06:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.