Re: Extend XML file attributes using XSLT

"Christian Rühl" <[email protected]> Fri, 23 Nov 2007 02:44:13 -0800 (PST)
Newsgroups gmane.comp.mozilla.devel.layout.xslt
Organization http://groups.google.com
Message-ID <[email protected]>
On 23 Nov., 09:15, "Christian Rühl" <[email protected]> wrote:
> new day, new problem here... :(
>
> i have two xml files with some similar attributes. i want to copy
> specific content (namely attributes) from one xml file to the other.
>
> to show you of what i'm talking, heres how my archive and my product
> tree xml files look like (very abstract):
>
> --- archive.xml ---
>
> <Common name="..." path="..." />
> <Common ... />
> <Glue name="..." path="..." />
> <Glue ... />
> <Mtds name="..." path="..." />
> <Mtds ... />
> <Mtds ... />
> <ARCHIVE>
> <Module moduleName="moduleA" variant="2" subsystem="subsystemA"
> path="..." textConvention="..." />
> <Module moduleName="moduleX" variant="17" subsystem="subsystemB"
> path="..." textConvention="..." />
> <Product cid="9000" subsystem="subsystemD" path="..."
> textConvention="..." />
> </ARCHIVE>
>
> --- prodtree.xml ---
>
> <PRODUCT_TREE productName="TestProduct" softwareVersion="1.0"
> description="...">
> <TopLevelComponent topLevelModule="Mab" cid="1200" description="...">
> <Module selectedModule="moduleA" instance="12"/>
> <Component topLevelModule="Mxy" cid="9000" description="...">
> <SubComponent topLevelModule="Mxy" cid="5500" description="...">
> <Module selectedModule="moduleX" instance="1"/>
> </SubComponent>
> </Component>
> </TopLevelComponent>
> </PRODUCT_TREE>
>
> ------------------------------------------
> hint:
> PRODUCT_TREE/selectedModule == ARCHIVE/moduleName
> ------------------------------------------
>
> what i want to do now is to extend the prodtree.xml as said below:
>
> - for all <*Component> nodes: check if there is a <Product> node in
> archive.xml with the same cid? if so: copy all other attributes from
> that node.
> - for all <Module> nodes: check if there is a <Module> node in
> archive.xml with the selectedModule == moduleName? if so: copy all
> other attributes from that node.
>
> so that the outcome looks like this:
>
> --- result.xml ---
>
> <PRODUCT_TREE productName="TestProduct" softwareVersion="1.0"
> description="...">
> <TopLevelComponent topLevelModule="Mab" cid="1200" description="...">
> <Module selectedModule="moduleA" instance="12" variant="2"
> subsystem="subsystemA" path="..." textConvention="..."/>
> <Component topLevelModule="Mxy" cid="9000" subsystem="subsystemD"
> path="..." description="...">
> ...
> </PRODUCT_TREE>
>
> ------------------------------------------
>
> i really hope you can help me again! thats how far i came by now:
>
> <?xml version="1.0"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
>         <xsl:output method="xml" indent="yes"/>
>         <xsl:param name="archive"/>
>         <xsl:template match="/">
>                 <TOP>
>                         <xsl:for-each select="TopLevelComponent">
>                                 <TopLevelComponent cid="{@cid}" topLevelModule="{@topLevelModule}"
> softwareVersion="{@softwareVersion}">
>                                         <xsl:variable name="cid" select="string(@cid)"/>
>                                         <xsl:for-each select="document($archive)">
>                                                 <xsl:copy-of select="cid($cid)/*"/>
>                                         </xsl:for-each>
>                                 </TopLevelComponent>
>                         </xsl:for-each>
>                 </TOP>
>         </xsl:template>
> </xsl:stylesheet>
>
> doesn't work of course... :(

Ooops, I just got the advice that my posting style is a little
annoying...
I beg your pardon for that and I will try to make a better
questioning. And don't get me wrong - I don't want you to do my job!
But of course I need your help!

So here are my files (as you might call it SSCCE):

----- archive.xml -----
<?xml version="1.0" encoding="UTF-8"?>
<PMBv2>
        <ARCHIVE>
                <Common path="D:/common/common_1.tmp" name="common_1"/
>
                <Common path="D:/common/common_2.tmp" name="common_2"/
>
                <Common path="D:/common/common_3.tmp" name="common_3"/
>
                <Glue path="D:/glue/glue_1.tmp" name="glue_1"/>
                <Glue path="D:/glue/glue_2.tmp" name="glue_2"/>
                <Mtds path="D:/mtds/mtds_1.tmp" name="mtds_1"/>
                <Mtds path="D:/mtds/mtds_2.tmp" name="mtds_2"/>
                <Mtds path="D:/mtds/mtds_3.tmp" name="mtds_3"/>
                <Mtds path="D:/mtds/mtds_4.tmp" name="mtds_4"/>
                <Mtds path="D:/mtds/mtds_5.tmp" name="mtds_5"/>
                <Mtds path="D:/mtds/mtds_6.tmp" name="mtds_6"/>
                <Mtds path="D:/mtds/mtds_7.tmp" name="mtds_7"/>
                <Mtds path="D:/mtds/mtds_8.tmp" name="mtds_8"/>
                <Mtds path="D:/mtds/mtds_9.tmp" name="mtds_9"/>
                <Module path="D:/module-specific/
module_mt_m1235_3_{MNCL}{MNCN}
{MNMI}.tmp" mid="m1235" moduleName="MODULE-MT-M1235-3-TMP" variant="3"
tokens="{MNCL}{MNCN}{MNMI}" tc="D:/module_tc/module_mt_m1235_tc.tmp"/>
                <Module path="D:/module-specific/
module_mt_m1237_2_subsystemA_{MNCL}
{MNCN}{MNMI}.tmp" mid="m1237" moduleName="MODULE-MT-M1237-2-
SUBSYSTEMA-
TMP" variant="2" subsystem="subsystemA" tokens="{MNCL}{MNCN}{MNMI}"
tc="D:/module_tc/module_mt_m1237_tc.tmp"/>
                <Product path="D:/product-specifc/
product_mt_product_9000_{MNCL}
{MNCN}{MNMI}.tmp" cid="5500" moduleName="PRODUCT-MT-PRODUCT-9000-MIB"
tokens="{MNCL}{MNCN}{MNMI}"/>
                <Product path="D:/product-specifc/
product_mt_product_5500_subsystemX_{MNCL}{MNCN}{MNMI}.tmp" cid="5500"
moduleName="PRODUCT-MT-PRODUCT-5500-SUBSYSTEMX-MIB"
subsystem="subsystemX" tokens="{MNCL}{MNCN}{MNMI}" tc="D:/module_tc/
product_mt_product_5500_tc.tmp"/>
        </ARCHIVE>
</PMBv2>

----- prodtree.xml -----
<?xml version="1.0" encoding="UTF-8"?>
<PMBv2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="D:\PMBv2\install
\pmbv2_prodtree_schema.xsd">
        <PRODUCT_TREE productName="MyProduct" description="Text"
softwareVersion="2.0">
                <TopLevelComponent topLevelModule="M14" cid="1700"
description="Text">
                        <Module selectedModule="MODULE-MT-M1235-3-MIB"
instance="23"
description="Text"/>
                        <Component topLevelModule="M16" cid="1300"
description="Component
description.">
                                <Module instance="12"
description="Text" selectedModule="MODULE-MT-
M1236-2-TMP"/>
                                <SubComponent topLevelModule="M16"
cid="5500" description="Text">
                                        <Module instance="10"
description="Text" selectedModule="MODULE-
MT-M1235-3-TMP"/>
                                </SubComponent>
                                <SubComponent topLevelModule="M14"
cid="1300" description="Text">
                                        <Module instance="17"
description="Text" selectedModule="MODULE-
MT-M1235-4-TMP"/>
                                        <SubComponent
topLevelModule="M9" cid="9000" description="Text">
                                                <Module instance="12"
description="Text" selectedModule="MODULE-
MT-M1235-3-TMP"/>
                                        </SubComponent>
                                </SubComponent>
                        </Component>
                </TopLevelComponent>
        </PRODUCT_TREE>
</PMBv2>

So, what I want to do now is the following:
- copy all <Common>, <Glue> and <Mtds> nodes to prodtree.xml

For each node in prodtree.xml check if
- there is a <Product> in archive.xml with the same cid attribute.
-->if so: append all attributes (that would then be: path, subsystem,
moduleName and tokens)  to my <*Component> node in prodtree.xml
- there is a <Module> in archive.xml where the moduleName attribute
has the same value as my current selectedModule attribute
--> if so: append all attributes (that would then be: path, mid,
variant, tokens, tc and subsystem) to the my <Module> node in
prodtree.xml

BTW: I already signed up for a XSLT workshop in January. But until
then it looks like I have to figure things out myself... And I can't
just make things like that out of thin air. :(

BTW2: Forget about that code snippet in my earlier post. That doesn't
make sense at all... Just a little trial and error gambling. :-D

Greets,
Chris