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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

conditional flag in XML file?

Status
Not open for further replies.

arilus

Programmer
Feb 21, 2006
1
US
Hello,

I have an application that uses an XML file as a configuration file. For example: A department store company that uses an XML file for configuring: different sections (men's clothing, women's clothing, jewelry, household, furniture, kid's clothing and toys etc) and the merchandise they carry. Some department stores do not have certain sections. If they have the same sections, they carry similar merchandise.

There is a DTD file that specifies each section (men's clothing), its subsection (shoes) and its merchandise type (running). There is an XML file that lists all its elements and attributes, and this file gets really big. Also a lot of very similar information gets repeated over and over again. I am using Entity Reference to solve some of the data repetition. For parsing, I use the DOM object.

When some of the department stores do not have the exact same sections, I need to create a different XML file for it. For example, the department store A has Men's Clothing, Women's Clothing, Jewelry, and Kid's Clothing. Department store B has sections in addition to those A has; it has Household and Furniture. Department store C has an Optical section in addition to the ones store A and B have.

The goals are:
1. Allow one XML file and one corresponding DTD file to work with different department stores.
2. If the XML file gets too big, break it into different small XML files (men_clothing.xml, household.xml)
3. Minimize data repetitions.

I did some research in XML books and web sites, and could not really find what I need. Using an improper solution, I am able to accomplish the above 3 goals by

1. Running a C Preprocessor with the original XML file to generate a specific XML file using this command:
cpp -P -tradition -Ddepartment_C data.xml data.xml.C

I surround the Optical section in data.xml with:

#ifdef department_C
<optical>
...
...
</optical>
#endif

Then only data.xml.C contains the Optical section, and Department C uses this XML file. Any new section or merchandize only needs to be added in one place.

2. After breaking the department sections into several small XML files, I add this #include flag in the body of data.xml
#include men_clothing.xml
#include household.xml
...
By running this command:
cpp -P -tradition data.xml data.xml.complete

This combines all the small XML files into a big one.

3. Use programming C macro syntax for parameter substitution..

Question: Does XML provide similar syntax (#ifdef, #include, or macro) to accomplish these goals?


Any insights would be greatly appreaciated.

Fan
 
Why don't you create one XML file that covers everything, then create an XSL stylesheet per department store that will create the XML for that particular store.

Jon

"I don't regret this, but I both rue and lament it.
 
arilus,
This is something you have to await for more supporting tools to appear. The natural place to do this kind of task is the XInclude, presently a candidate recommendation.
A simple tutorial can be found in this article.
But one definitely need xinclude processor to make the trnasclusion job happen which one may lack of.

As the support is not yet widespread, I think, in the meantime, you can still using classic xslt1 to do the "transclusion" derived from sgml. This is done by using document() function and identity transformation. This is an illustration by DuCharm.

In the meantime, I can add a supplementary note as to the use of DuCharm's method. I would suggest one has better to be prepared for this kind of operation to avoid name collision. Hence, it should be more often than not prepared to use a specific namespace for each external dedicated xml file to avoid this problem.

Let me illustration this.
[1] A document, say doc_base.xml, to plot out what eventually we will consolidate.
[2] Documents, say only one doc_ext.xml, to be included in the final consolidated document.
[3] An xsl file, say doc_make.xsl, to do the transformation on the base document.
[4] The output, say doc_out.xml of an xml consolidating all.

This is the construction.
[tt][blue][1] doc_base.xml[/blue]
<?xml version="1.0" encoding="utf-8"?>
<root>
<tag_1>abc</tag_1>
<tag_2>
<tag_3>def</tag_3>
</tag_2>
<transclusion src="[blue]doc_ext.xml[/blue]"/>
</root>
[/tt]
[tt][blue][2] doc_ext.xml[/blue]
<?xml version="1.0"?>
<root xmlns="urn-tsuji-dummy">
<a>1</a>
<b>
<c>2</c>
</b>
</root>
[/tt]
[tt][blue][3] doc_make.xsl[/blue]
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="<xsl:eek:utput method="xml" version="1.0" indent="yes" encoding="utf-8" />
<xsl:preserve-space elements="*" />
<xsl:template match="transclusion">
<xsl:copy-of select="document(@src)"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
[/tt]
[tt][blue][4] doc_out.xml (if you use any utility to do the job, the output might be slightly different in white-space rendement. But the essential would be the same.)[/blue]
<?xml version="1.0" encoding="utf-8"?>
<!-- file: doc_base.xml -->
<root>
<tag_1>abc</tag_1>
<tag_2>
<tag_3>def</tag_3>
</tag_2>
<root xmlns="urn-tsuji-dummy">
<a>1</a>
<b>
<c>2</c>
</b>
</root>
</root>
[/tt]
(I put the root collision in a more dramatic way. But you would usually have something less dramatic.)

I hope this illustrate the essential for what can be done within xslt1.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top