Hey guys,

I'm tying to merge two XML file for days but it doesn't work. Here is the problem:

I want to merge these XML files:
graphs.xml

<?xml version='1.0' encoding='utf-8'?>
<ph:Graphs xmlns:ph="http://www.merge.something.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <ph:Graph name="mass_spring_mo">
        <ph:Element id="0" type="Mass">
            <ph:Attribute>
                <ph:AttributeField name="type" value="float" />
            </ph:Attribute>
            <ph:Port id="1" type="port">
                <ph:Attribute>
                    <ph:AttributeField name="type" value="int" />
                </ph:Attribute>
            </ph:Port>
        </ph:Element>
        <ph:Edge id="3" sourceid="4" targetid="5" />
    </ph:Graph>
</ph:Graphs>

and metamodel.xml

<?xml version='1.0' encoding='utf-8'?>
<ph:Metamodel xmlns:ph="http://www.merge.something.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ph:ElementType name="ElementType">
        <ph:ElementType name="Mass">
            <ph:Attribute>
                <ph:AttributeField name="type" value="int" />
                <ph:AttributeField name="name" value="m" />
                <ph:AttributeField name="value" value="5" />
                <ph:AttributeField name="send_to_graph_rewrite_engine" value="True" />
            </ph:Attribute>
        </ph:ElementType>
    </ph:ElementType>
</ph:Metamodel>

Therefore I created an index.xml file:

<?xml version="1.0"?>
<ph:index xmlns:ph="http://www.merge.something.com">
   <ph:entry>graphs</ph:entry>
   <ph:entry>metamodel</ph:entry>
</ph:index>

An I started with this XSLT file:

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

<xsl:output method="xml"/>

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


<xsl:template match="ph:index/ph:entry">
   <xsl:apply-templates select="document(concat(.,'.xml'))"/>
</xsl:template>

<xsl:template match="ph:Graphs">
   <xsl:value-of select="ph:Graph"/>
</xsl:template>


</xsl:stylesheet>

My disered output should be looking like that:

<?xml version='1.0' encoding='utf-8'?>
<ph:Graphs xmlns:ph="http://www.merge.something.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <ph:Graph name="mass_spring_mo">
        <ph:Element id="0" type="Mass">
            <ph:Attribute>
                <ph:AttributeField name="type" value="float" />
            </ph:Attribute>
            <ph:Attribute>
                <ph:AttributeField name="type" value="int" />
                <ph:AttributeField name="name" value="m" />
                <ph:AttributeField name="value" value="5" />
                <ph:AttributeField name="send_to_graph_rewrite_engine" value="True" />
            </ph:Attribute>
            <ph:Port id="1" type="port">
                <ph:Attribute>
                    <ph:AttributeField name="type" value="int" />
                </ph:Attribute>
            </ph:Port>
        </ph:Element>
        <ph:Edge id="3" sourceid="4" targetid="5" />
    </ph:Graph>
</ph:Graphs>

But there is no output!? Where is error!? Can anyone help me please.
Thanks!

Bye Michael

Recommended Answers

All 2 Replies

this not working with browser

with xslt2 it is possible to solv this problem

this will work in browser only opera 11
when you use xslt2 parser this will work too
xml graphs.xml

<?xml-stylesheet href="graphs.xsl" type="text/xsl" ?>
<ph:Graphs xmlns:ph="http://www.merge.something.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<ph:Graph name="mass_spring_mo">
		<ph:Element id="0" type="Mass">
			<ph:Attribute>
				<ph:AttributeField name="type" value="float"/>
			</ph:Attribute>
			<ph:Port id="1" type="port">
				<ph:Attribute>
					<ph:AttributeField name="type" value="int"/>
				</ph:Attribute>
			</ph:Port>
		</ph:Element>
		<ph:Edge id="3" sourceid="4" targetid="5"/>
	</ph:Graph>
</ph:Graphs>

xml metamodel.xml

<ph:Metamodel xmlns:ph="http://www.merge.something.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<ph:ElementType name="ElementType">
		<ph:ElementType name="Mass">
			<ph:Attribute>
				<ph:AttributeField name="type" value="int"/>
				<ph:AttributeField name="name" value="m"/>
				<ph:AttributeField name="value" value="5"/>
				<ph:AttributeField name="send_to_graph_rewrite_engine" value="True"/>
			</ph:Attribute>
		</ph:ElementType>
	</ph:ElementType>
</ph:Metamodel>

xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ph="http://www.merge.something.com">
	<xsl:output indent="yes" method="xml"/>
	<xsl:template match="/">
		<ph:Graphs>
			<xsl:apply-templates select="ph:Graphs"/>
		</ph:Graphs>
	</xsl:template>
	<xsl:template match="ph:Graphs">
		<xsl:apply-templates select="ph:Graph"/>
	</xsl:template>
	<xsl:template match="ph:Graph">
		<ph:Graph name="{@name}">
			<xsl:apply-templates select="ph:Element"/>
		</ph:Graph>
	</xsl:template>
	<xsl:template match="ph:Element">
		<xsl:variable name="type" select="@type"/>
		<ph:Element id="{@id}" type="{@type}">
			<xsl:apply-templates select="ph:Attribute"/>
			<xsl:variable name="metamodel">
				<xsl:copy-of select="document('metamodel.xml')//ph:ElementType[@name=$type]/ph:Attribute"/>
			</xsl:variable>
			<xsl:copy-of select="$metamodel"/>
			<xsl:apply-templates select="ph:Port"/>
		</ph:Element>
	</xsl:template>
	<xsl:template match="ph:Attribute">
		<xsl:copy-of select="."/>
	</xsl:template>
	<xsl:template match="ph:Port">
		<xsl:copy-of select="."/>
	</xsl:template>
</xsl:stylesheet>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.