xsl-list
[Top] [All Lists]

RE: RE: tansforming an XML into another XML help needed !

2005-04-21 00:22:04
----------------------
Here is the stylesheet
----------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:myLangs="my:Langs" exclude-result-prefixes="myLangs">
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"
doctype-system="dcr4.5.dtd"/>
        <xsl:strip-space elements="*"/>
        <myLangs:myLangs>
                <lang>
                        <abbr/>
                        <fullName>common</fullName>
                </lang>
                <lang>
                        <abbr>EN</abbr>
                        <fullName>english</fullName>
                </lang>
                <lang>
                        <abbr>FR</abbr>
                        <fullName>french</fullName>
                </lang>
                <lang>
                        <abbr>NL</abbr>
                        <fullName>dutch</fullName>
                </lang>
        </myLangs:myLangs>
        <!-- item name -->
        <xsl:template match="item">
                <item name="{substring-before(concat(@name, '_'), '_')}">
                        <value>
                                <xsl:if test="contains(@name,'_')">
                                        <xsl:value-of select="value"/>
                                </xsl:if>
                        </value>
                </item>
        </xsl:template>
        <xsl:template match="record">
                <xsl:element name="record">
                        <xsl:attribute name="name"><xsl:value-of
select="@name"/></xsl:attribute>
                        <xsl:attribute name="type">content</xsl:attribute>
                        <!-- manage common -->
                        <item name="common">
                                <value>
                                        <xsl:for-each 
select="/record/item[not(contains(@name,'_'))]">
                                                <xsl:if 
test="count(current()//item[contains(@name,'_')]) = 0">
                                                        <xsl:copy-of 
select="current()"/>
                                                </xsl:if>
                                        </xsl:for-each>
                                </value>
                        </item>
                        <!-- manage languages -->
                        <xsl:for-each 
select="document('')/*/myLangs:*/lang[abbr != '']">
                                <item name="{fullName}">
                                        <value>
                                                <xsl:variable 
name="currentAbbr" select="current()/abbr"/>
                                                <!-- first display 1st level 
items with a language,  those are
leaves -->
                                                <xsl:variable name="vRec"
select="/record/item[substring-after(@name, '_') = $currentAbbr]"/>
                                                <xsl:for-each select="$vRec">
                                                        <!--display itself-->
                                                        <xsl:apply-templates 
select="current()"/>
                                                </xsl:for-each>
                                                <!-- display first level items 
that are not leaves (no language)
and have children <item>-->
                                                <xsl:variable name="vRec2"
select="/record/item[not(contains(@name, '_')) and count(value/item) >
0]"/>
                                                <xsl:for-each select="$vRec2">
                                                        <!--xsl:value-of 
select="concat(' ****name is
',current()/@name)"/-->
                                                        <item 
name="{current()/@name}">
                                                                <value>
                                                                        <xsl:if 
test="count(current()//item[substring-after(@name,
'_') = $currentAbbr]) > 0">
                                                                                
<!-- display childs-->
                                                                                
<xsl:variable name="sons"
select="$vRec2/value/item[substring-after(@name, '_') = $currentAbbr] |
$vRec2/value/item[substring-after(@name, '_') ='']"/>
                                                                                
<xsl:for-each select="$sons">
                                                                                
        <!--xsl:value-of select="concat('     son name is
',current()/@name)"/-->
                                                                                
        <xsl:if test="contains(current()/@name,'_')">
                                                                                
                <xsl:apply-templates select="current()"/>
                                                                                
        </xsl:if>
                                                                                
        <xsl:if test="not(contains(current()/@name,'_'))">
                                                                                
                <item name="{current()/@name}">
                                                                                
                        <value>
                                                                                
                                <!-- display grand-childs-->
                                                                                
                                <xsl:variable name="grandSons"
select="current()/value/item[substring-after(@name, '_') =
$currentAbbr]"/>
                                                                                
                                <xsl:for-each select="$grandSons">
                                                                                
                                        <!--xsl:value-of select="concat('     
grand name is
',current()/@name)"/-->
                                                                                
                                        <xsl:apply-templates 
select="current()"/>
                                                                                
                                </xsl:for-each>
                                                                                
                        </value>
                                                                                
                </item>
                                                                                
        </xsl:if>
                                                                                
</xsl:for-each>
                                                                        
</xsl:if>
                                                                </value>
                                                        </item>
                                                </xsl:for-each>
                                        </value>
                                </item>
                        </xsl:for-each>
                </xsl:element>
        </xsl:template>
</xsl:stylesheet>

-----------------------
Here is a full xml file
-----------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE record SYSTEM "dcr4.5.dtd">
<record name="disclaimer" type="content">
        <item name="DateOn">
                <value>07-03-2005</value>
        </item>
        <item name="DateOff">
                <value/>
        </item>
        <item name="PageName">
                <value>disclaimer</value>
        </item>
        <item name="ContactName">
                <value>michel c. gillet</value>
        </item>
        <item name="ContactEmail">
                <value>luc(_dot_)rooms(_at_)belgacom(_dot_)be</value>
        </item>
        <item name="ContentOwnerName">
                <value>luc rooms</value>
        </item>
        <item name="NavigationCaption_EN">
                <value>legal disclaimer</value>
        </item>
        <item name="NavigationCaption_FR">
                <value>legal disclaimer</value>
        </item>
        <item name="NavigationCaption_NL">
                <value>legal disclaimer</value>
        </item>
        <item name="Title_EN">
                <value>read carefully</value>
        </item>
        <item name="Title_FR">
                <value>read carefully</value>
        </item>
        <item name="Title_NL">
                <value>read carefully</value>
        </item>
        <item name="Abstract_EN">
                <value>The following information is strictly confidential. 
Bringing
this information to the attention of non-authorized persons could
seriously harm Belgacom. 
It is necessary, while handling this knowledge, to adhere strictly to
the Group Policy guidelines. 
Any violation of these regulations can be penalized according to the
proce-dures applicable in the company.</value>
        </item>
        <item name="Abstract_FR">
                <value>The following information is strictly confidential. 
Bringing
this information to the attention of non-authorized persons could
seriously harm Belgacom. 
It is necessary, while handling this knowledge, to adhere strictly to
the Group Policy guidelines. 
Any violation of these regulations can be penalized according to the
proce-dures applicable in the company.</value>
        </item>
        <item name="Abstract_NL">
                <value>The following information is strictly confidential. 
Bringing
this information to the attention of non-authorized persons could
seriously harm Belgacom. 
It is necessary, while handling this knowledge, to adhere strictly to
the Group Policy guidelines. 
Any violation of these regulations can be penalized according to the
proce-dures applicable in the company.</value>
        </item>
        <item name="DisplayAnchorList"/>
        <item name="Content">
                <value>
                        <item name="Title_EN">
                                <value/>
                        </item>
                        <item name="Title_FR">
                                <value/>
                        </item>
                        <item name="Title_NL">
                                <value/>
                        </item>
                        <item name="Paragraph">
                                <value>
                                        <item name="SubParagraph_EN">
                                                <value>Sub1 en</value>
                                        </item>
                                        <item name="SubParagraph_FR">
                                                <value>Sub1 fr</value>
                                        </item>
                                        <item name="SubParagraph_NL">
                                                <value>Sub1 nl</value>
                                        </item>
                                </value>
                                <value>
                                        <item name="SubParagraph_EN">
                                                <value>Sub2 en</value>
                                        </item>
                                        <item name="SubParagraph_FR">
                                                <value>Sub2 fr</value>
                                        </item>
                                        <item name="SubParagraph_NL">
                                                <value>Sub2 nl</value>
                                        </item>
                                </value>
                                <value>
                                        <item name="SubParagraph_EN">
                                                <value>Sub3 en</value>
                                        </item>
                                        <item name="SubParagraph_FR">
                                                <value>Sub3 fr</value>
                                        </item>
                                        <item name="SubParagraph_NL">
                                                <value>Sub3 nl</value>
                                        </item>
                                </value>
                        </item>
                        <item name="TableLinkURL_EN">
                                <value/>
                        </item>
                        <item name="TableLinkURL_FR">
                                <value/>
                        </item>
                        <item name="TableLinkURL_NL">
                                <value/>
                        </item>
                        <item name="TableLinkLabel_EN">
                                <value/>
                        </item>
                        <item name="TableLinkLabel_FR">
                                <value/>
                        </item>
                        <item name="TableLinkLabel_NL">
                                <value/>
                        </item>
                        <item name="TableLinkDescription_EN">
                                <value/>
                        </item>
                        <item name="TableLinkDescription_FR">
                                <value/>
                        </item>
                        <item name="TableLinkDescription_NL">
                                <value/>
                        </item>
                        <item name="ImageURL_EN">
                                <value/>
                        </item>
                        <item name="ImageURL_FR">
                                <value/>
                        </item>
                        <item name="ImageURL_NL">
                                <value/>
                        </item>
                        <item name="ImageAlt_EN">
                                <value/>
                        </item>
                        <item name="ImageAlt_FR">
                                <value/>
                        </item>
                        <item name="ImageAlt_NL">
                                <value/>
                        </item>
                        <item name="ImageLink_EN">
                                <value/>
                        </item>
                        <item name="ImageLink_FR">
                                <value/>
                        </item>
                        <item name="ImageLink_NL">
                                <value/>
                        </item>
                        <item name="ImageOpenInNewWindow"/>
                        <item name="Links">
                                <value>
                                        <item name="LinkURL_EN">
                                                <value/>
                                        </item>
                                        <item name="LinkURL_FR">
                                                <value/>
                                        </item>
                                        <item name="LinkURL_NL">
                                                <value/>
                                        </item>
                                        <item name="LinkLabel_EN">
                                                <value/>
                                        </item>
                                        <item name="LinkLabel_FR">
                                                <value/>
                                        </item>
                                        <item name="LinkLabel_NL">
                                                <value/>
                                        </item>
                                        <item name="LinkDescription_EN">
                                                <value/>
                                        </item>
                                        <item name="LinkDescription_FR">
                                                <value/>
                                        </item>
                                        <item name="LinkDescription_NL">
                                                <value/>
                                        </item>
                                        <item name="LinkOpenInNewWindow"/>
                                </value>
                        </item>
                </value>
        </item>
        <item name="CntType">
                <value>budget, revenues &amp; expenses</value>
        </item>
        <item name="ExtraKeyWords_EN">
                <value/>
        </item>
        <item name="ExtraKeyWords_FR">
                <value/>
        </item>
        <item name="ExtraKeyWords_NL">
                <value/>
        </item>
        <item name="SPKeywords">
                <value/>
        </item>
        <item name="LastUpdate">
                <value>19/04/2005</value>
        </item>
</record>


-----------------------

--- Pierre-Yves <kyrios(_at_)rocketmail(_dot_)com> wrote:
Hello,


The example I provided is a simplified version of the xml that focus
on
the problem.  I have hundreds of real sources, their structure might
be
slightly different but the principle remains the same.

In attach you'll find 2 "sample" xml and the stylesheet.
But the stylesheet is really dirty and it's not working at 100% yet
The main problem is that it's not recursive, I only parse 3 levels
(meaning if there are more embedded elements it will not work).

If you consider the XML as a tree, all the items that have a name
attribute that ends with _XX (_EN, _NL, _FR) are always leaves
(cannot
contain a "sub-item").  

The ones that don't end with _XX (PageName, DateOn, Paragraph,
Content...) might contain other items or might be leaves.

If they are leaves they should go in "common" if not, it's part of
the
structure that should be reproduiced in the output (see my first
mail).

Sorry for the "dirty" example but I started learning XSL only 2 days
ago.


Regards,
Pierre.


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--