Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Folder Full of XML Files using same schema 2

Status
Not open for further replies.

Miros

Programmer
Jan 27, 2001
506
US
Each file looks like this:
Code:
<MaxisPackages xmlns="./MaxisPackages.xsd">
  <fileInfo>
    <fileLocation>C:\Program Files\EA GAMES\The Sims 2 Family Fun Stuff\TSData\Res\Objects\objects.package</fileLocation>
  </fileInfo>
  <fileInfo>
    <fileLocation>C:\Program Files\EA GAMES\The Sims 2 Family Fun Stuff\TSData\Res\3D\CarryForward.sgfiles.package</fileLocation>
  </fileInfo> 
[red]...[/red]
  <guidInfo objectGUID="D07B9CE9" />
  <guidInfo objectGUID="10A49FB6" />
  <guidInfo objectGUID="508CB1F6" />
  <guidInfo objectGUID="50596292" />
  <guidInfo objectGUID="CF7A514E" />
  <guidInfo objectGUID="EF826029" />
[red]...[/red]
  <tgiInfo>
    <tgi_type>FC6EB1F7</tgi_type>
    <tgi_group>1C0532FA</tgi_group>
    <tgi_instance>FFD93AC1</tgi_instance>
    <tgi_instance2>00000000</tgi_instance2>
  </tgiInfo>
  <tgiInfo>
    <tgi_type>FC6EB1F7</tgi_type>
    <tgi_group>7F45284D</tgi_group>
    <tgi_instance>FFF14C45</tgi_instance>
    <tgi_instance2>D6E9E5B3</tgi_instance2>
  </tgiInfo>
  <tgiInfo>
    <tgi_type>FC6EB1F7</tgi_type>
    <tgi_group>1C0532FA</tgi_group>
    <tgi_instance>FF644CA9</tgi_instance>
    <tgi_instance2>00000000</tgi_instance2>
  </tgiInfo>
[red]...[/red]
</MaxisPackages>

I can easily write an XSL file to convert the <tgiInfo> nodes to <tgis> nodes:
Code:
<tgis tgi_type="FC6EB1F7" tgi_group="1C050000" tgi_instance2="B1BF1329" tgi_instance="FF121E17" />

Is there an XSL way to harvest all the <tgiInfo> nodes from approximately a dozen XML files in my directory and store them in one XML file? I'm willing and able to create a "master" xml file with all the file names in it (as <fileLocation> nodes) if it will help.
 
Thanks! I thought that might do it, but never get a chance to work on things in an organized manner...
 
I'm almost there...

Code:
    <xsl:template match="fileInfo">
    	<xsl:variable name="xmlfilename" >
    		<xsl:value-of select="fileLocation"/>
    	</xsl:variable>
    	<filename>
    		<xsl:value-of select="fileLocation"/>
    	</filename>
		<xsl:apply-templates select="document($xmlfilename)" >    	
		</xsl:apply-templates> 
    </xsl:template>
dumps the values from the file named in the <fileInfo/fileLocation> tag into my output file as raw text.

Code:
    <xsl:template match="fileInfo">
    	<xsl:variable name="xmlfilename" >
    		<xsl:value-of select="fileLocation"/>
    	</xsl:variable>
    	<filename>
    		<xsl:value-of select="fileLocation"/>
    	</filename>
		<xsl:apply-templates select="document($xmlfilename)[red]/MaxisPackages/fileInfo[/red]" >    	
		</xsl:apply-templates> 
    </xsl:template>
outputs nothing from the file named in the <fileInfo/fileLocation> tag. How can I specify which tags I want to process from the file opened with the document() function?
 
[0]I am not sure I understand the intention of your two blocks of tempted approach. They are that much different.

[1] In the approach as reflected in your second block, I just want to draw your attention to the need of specifying the namespace where tgiInfo lives. And that your xpath should point to tgiInfo rather than fileInfo if I understand correctly that you mean to extract the tgiInfo.

As to further elaboration, I would leave k5tm to continue with his idea - which is certainly valid.
 
Yes, it should be tgiInfo, but I was going to add a "wrapper" made from the first <fileInfo> around the block of <tgis>. I get no output from the file loaded with the document call with either tgiInfo or fileInfo.

Is it legitimate to specify which tags I want to handle from the document call?
Code:
<xsl:apply-templates select="document($xmlfilename)/MaxisPackages/fileInfo" >
 
Okay, I can elaborate [1] so that you understand and until...

[2] Since MaxisPackages and its child elements live in the namespace "./MaxisPackages.xsd", this fact must be reflected in the xpath to reference them.

[2.1] In the xsl (I mean where document() etc appears), you have to declare that namespace with prefix ns herein-below. But, you don't want that ns to appear in the output xml document at the root because if the namespace appears in the output, it is due to the document() refenencing it and _not_ that the xsl forcing that namespace. Hence, you add the attribute exclude-result-prefixes to achieve that result. Hence the xsl document root should be something like this.
[tt]
<xsl:stylesheet version="1.0"
xmlns:xsl="[ignore][/ignore]"
[blue]xmlns:ns="./MaxisPackages.xsd"
exclude-result-prefixes="ns"[/blue]
>
[/tt]
[2.2] Then, the xpath referencing the external xml document would be this.
[tt]
<xsl:apply-templates select="document($xmlfilename)/[red]ns:[/red]MaxisPackages/[red]ns:[/red][blue]tgiInfo[/blue]" >
[/tt]
or ns:fileInfo as you seem to insist and sure doing the right thing - I am not sure what actually you script there.

[2.3] In the workhorse template for that purpose, it would be something like this (ns:fileInfo or ns:tgiInfo).
[tt]
<xsl:template match="[blue]ns:tgiInfo[/blue]">
<!-- etc etc -->
</xsl:template>
[/tt]
 
Thank you! I haven't had to work with any namespace but the default yet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top