XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
dorian dinhSubject: Generating unique ID in whole tree
Author: dorian dinh
Date: 28 Oct 2006 12:46 PM
Hello there,
i need to input a xmi:id attribute in every element of a xml document. If the attribute already exists, i don't generate it. In the other hand, when i generate one, its value must be different from those existing.

For the first part its done. For the second part i tried to help myself with a old post of micheal kay. But it doesn't work.
He doesn't seem to understand :
<xsl:key name="k" match="child::node()[@xmi:id]" use="@xmi:id"/>
(no error at compiling but my test fails)

XSL :
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xmi="http://www.omg.org/XMI">

<xsl:output method="xml" encoding="ISO-8859-1" indent="yes"/>
<xsl:key name="k" match="child::node()[@xmi:id]" use="@xmi:id"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="child::node()[not(@xmi:id)]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="xmi:id">
<xsl:call-template name="make-id">
<xsl:with-param name="try" select="generate-id()"/>
</xsl:call-template>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="child::node()[@xmi:id]">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template name="make-id">
<xsl:param name="try"/>
<xsl:choose>
<xsl:when test="not(key('k', $try))">
<xsl:value-of select="$try"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="make-id">
<xsl:with-param name="try" select="concat($try, 'ï')"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>


INPUT :
<?xml version="1.0" encoding="ASCII"?>
<xmi:XMI xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI">
<main>
<bookstore>
<book category="WEB">
<title lang="en" id="blabla">Learning XML</title>
<author>
<blabla lang="fr">
<toto xmi:id="N65556">
blabla
</toto>
</blabla>
</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
</main>
</xmi:XMI>


OUTPUT SHOULD BE:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xmi:XMI xmlns:xmi="http://www.omg.org/XMI" xmi:version="2.0" xmi:id="N65537">
<main xmi:id="N65542">
<bookstore xmi:id="N65544">
...
<blabla lang="fr" xmi:id="N65556ï">
<toto xmi:id="N65556">
blabla
</toto>
</blabla>
...
</bookstore>
</main>
</xmi:XMI>


BUT OUTPUT IS :

<?xml version="1.0" encoding="ISO-8859-1"?>
<xmi:XMI xmlns:xmi="http://www.omg.org/XMI" xmi:version="2.0" xmi:id="N65537">
<main xmi:id="N65542">
<bookstore xmi:id="N65544">
...
<blabla lang="fr" xmi:id="N65556"> !!!!!!!!!!!!!!
<toto xmi:id="N65556">
blabla
</toto>
</blabla>
...
</bookstore>
</main>
</xmi:XMI>

Postnext
Ivan PedruzziSubject: Generating unique ID in whole tree
Author: Ivan Pedruzzi
Date: 28 Oct 2006 09:58 PM

Try the following, it should work fine

Hope this helps
Ivan Pedruzzi
Stylus Studio Team



<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xmi="http://www.omg.org/XMI">
<xsl:output method="xml" encoding="ISO-8859-1"/>
<xsl:key name="k" match="@xmi:id" use="."/>
<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:if test="not(@xmi:id)">
<xsl:attribute name="xmi:id">
<xsl:value-of select="generate-id()"/>
<xsl:if test="key('k', generate-id())">ï</xsl:if>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Postnext
dorian dinhSubject: Generating unique ID in whole tree
Author: dorian dinh
Date: 28 Oct 2006 10:09 PM
hmm... found the error :
<xsl:key name="id-table" match="*[@xmi:id]" use="@xmi:id"/>

... still don't understand difference between those expressions :

child::node()[@xmi:id]
child::*[@xmi:id]

otherwise i don't understand why this expression :
descendant::*[@xmi:id] can't be built. i tried to use this because "child" should only take direct children of my context not all descendant children... and my id-table should consider whole element in my tree.

Its not clear in my head... Any help ?

thx forward

Postnext
Ivan PedruzziSubject: Generating unique ID in whole tree
Author: Ivan Pedruzzi
Date: 29 Oct 2006 01:30 PM
Hi Dorian,

The pattern in the match attribute of xsl:key element can be a relative expression because the context of a key is established where they key is invoked so @xmi:id is a perfectly acceptable pattern.

child::* matches only elements while child:nodes() matches any type of node therefore if you are lookig an attribute in the predicate to want to use *.

Only 'child' and 'attribute' axes are allowed in a match pattern outside predicates.

* is shorthand for child::*

Which XSLT processor are you running?

Does the solution I wrote works for you?

Ivan Pedruzzi
Stylus Studio Team

Postnext
dorian dinhSubject: Generating unique ID in whole tree
Author: dorian dinh
Date: 29 Oct 2006 02:00 PM
your solution doesnt work.
1) need recursive calling template make-id in case "<kind of id>ï" already exists so as to make "<kind of id>ïï...ï"
2) need to make an access to precedant child.

this line make my algorithm working :

<xsl:key name="id-table" match="*[@xmi:id]" use="@xmi:id"/>

thx anyway for taking a look to my pb.

Dorian.

Posttop
Ivan PedruzziSubject: Generating unique ID in whole tree
Author: Ivan Pedruzzi
Date: 29 Oct 2006 02:35 PM

You didn't state the requirement 1 in your message.

I am not sure what you mean with number 2; in any case the important thing is that you have it working now.

Ivan

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.