Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:17:00+00:00 2026-06-14T15:17:00+00:00

I have two XML documents with different structures (examples below) that I want to

  • 0

I have two XML documents with different structures (examples below) that I want to merge in the XSLT document I use for creating HTML output.

What I want to do is to create a table like this (example with cars):

Serialnr    Brand    Color    Year    Price    Condition
 123456     'Opel'   'Blue'   '1978'  '10000$'   'Used'
  ...        ...      ...      ...     ...      ...

The last two of these (Price and Condition) are to be found in the second document (foo2.xml), and the matching between these documents is the serialnr. The cars isn’t ordered in the same order in the documents, and it exist more cars in foo2.xml (that won’t be used) than in foo1.xml.

foo1.xml:

<cars>
    <car>
        <serialnr>123456</serialnr>
        <brand>Opel</brand>
        <color>Blue</color>
        <year>1978</year>
    </car>
    ...and so on...

foo2.xml:

<vehicles>
    <vehicle>
        <cell nr="2"><data nr="3">123456</data></vehicle> // <--- match on serialnr (123456)
        <cell><data nr="3">10000$</data></cell>
        <cell><data nr="3">Used</data></cell>
    ...and so on...

Foo1.xml is connected to the XSLT document with PHP on the server side, and foo2.xml is merged in the XSLT document (see below).

cars.xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">

    <xsl:param name="foo2" select="'foo2.xml'"/>

    <xsl:template match="/">

        <xsl:text disable-output-escaping='yes'>&lt;!DOCTYPE html></xsl:text>

        <html>
            <head>
                <title>Producers</title>
            </head>
            <body>
                       ...//table, thead, tr, th...

                    <tbody>
                        <xsl:for-each select="//car">
                        <tr>
                            <td>
                                <xsl:value-of select="serialnr" />
                            </td>
                            <td>
                                <xsl:value-of select="brand" />
                            </td>
                            <td>
                                <xsl:value-of select="color" />
                            </td>
                            <td>
                                <xsl:value-of select="year" />
                            </td>
                          </xsl:for-each>
                          <xsl:for-each select="document($foo2)//Vehicle">
                               // I need to match the serialnr in some way to get the correct car's data here, but how?
                               <td>
                                    // Selecting Cell node 2 (price)
                                    //<xsl:value-of select="/? "/>
                               </td>
                               <td>
                                    // Selecting Cell node 3 (condition)
                                    //<xsl:value-of select="/? "/>
                               </td>

So, how can I do this? I really appreciate help here. Thanks a lot in advance!

Notes:

  1. The structure of foo2.xml isn’t consistent. Suddenly the child node changes from being “vehicle” to “part” (i.e. “vehicles/vehicle” to “vehicles/part”).
  2. In foo2.xml there are a lot of “vehicle” nodes that contains of data I’m not interested in, so I suppost I must use some sort of if statement to test whether the desired data exists or not when looping through them?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:17:00+00:00Added an answer on June 14, 2026 at 3:17 pm

    Taking a guess at the answers to the questions that I posted in the main comment feed, please find this XSLT 1.0 solution.

    With this document as the main input…

    <cars>
       <car>
            <serialnr>123456</serialnr>
            <brand>Opel</brand>
            <color>Blue</color>
            <year>1978</year>
      </car>
       <car>
            <serialnr>789</serialnr>
            <brand>Toyota</brand>
            <color>Beige</color>
            <year>2005</year>
      </car>
    </cars>  
    

    … and this document as a readable document resource passed in to the style-sheet with parameter name foo2…

    <vehicles>
      <vehicle>
        <cell><data>123456</data></cell>
        <cell><data>10000$</data></cell>
        <cell><data>Used</data></cell>
      </vehicle>
      <vehicle>
        <cell><data>789</data></cell>
        <cell><data>20000$</data></cell>
        <cell><data>Pre-loved</data></cell>
      </vehicle>
    

    …this XSLT 1.0 style-sheet…*

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" doctype-system="about:legacy-compat" encoding="UTF-8" indent="yes" />
    <xsl:strip-space elements="*" />  
    
    <xsl:param name="foo2" select="'foo2.xml'" />
    <xsl:variable name="vehicles" select="document($foo2)/vehicles/vehicle" />
    
    <xsl:template match="/">
     <html lang="en">
       <head><title>Cars</title></head>
       <body>  
         <xsl:apply-templates select="cars" />
       </body>  
     </html>
    </xsl:template>
    
    <xsl:template match="cars">
      <table>
        <th>
          <td>Serialnr</td> <td>Brand</td> <td>Color</td> <td>Year</td> <td>Price</td> <td>Condition</td>
        </th>
        <xsl:apply-templates select="car" />
      </table>
    </xsl:template>
    
    <xsl:template match="car">
      <xsl:variable name="srl" select="serialnr/text()" />
        <tr>
          <td><xsl:value-of select="$srl" /></td>
          <td><xsl:value-of select="brand" /></td>
          <td><xsl:value-of select="color" /></td>
          <td><xsl:value-of select="year" /></td>
          <td><xsl:value-of select="$vehicles/self::*[cell/data=$srl]/cell[2]" /></td>
          <td><xsl:value-of select="$vehicles/self::*[cell/data=$srl]/cell[3]" /></td>
        </tr>
    </xsl:template>
    
    </xsl:stylesheet>
    

    …will yield this output (a HTML5 document) …

    <!DOCTYPE html SYSTEM "about:legacy-compat">
    <html lang="en">
      <head>
        <META http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Cars</title>
      </head>
      <body>
        <table>
          <th>
            <td>Serialnr</td>
            <td>Brand</td>
            <td>Color</td>
            <td>Year</td>
            <td>Price</td>
            <td>Condition</td>
          </th>
          <tr>
            <td>123456</td>
            <td>Opel</td>
            <td>Blue</td>
            <td>1978</td>
            <td>10000$</td>
            <td>Used</td>
          </tr>
          <tr>
            <td>789</td>
            <td>Toyota</td>
            <td>Beige</td>
            <td>2005</td>
            <td>20000$</td>
            <td>Pre-loved</td>
          </tr>
        </table>
      </body>
    </html>
    

    Note

    A solution using keys would also be possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two XML sources to retrieve data from. I want to use them
What is the best/fastest way to merge two xml documents with ruby? I have
I have an XML file and I would like to merge two different CONTACT
I have two XML docs that I've created and I want to combine these
I want to determine whether two different child nodes within an XML document are
I have some XML that looks something like this: <Root> <Documents> <Document id=1/> </Documents>
i have two opposing requirements related to an xml document and its related xsd.
this is my situation: I have two org.w3c.dom.Document created from two xml files. What
I have two XmlDocuments that both have a namespace attribute specified. Both documents have
I have two XML files that are generated by another application I have no

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.