You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Wei Cheng <We...@baylor.edu> on 2001/02/06 01:09:45 UTC

Re[2]: how to correct this xsl file

Hi Frank,

Monday, February 05, 2001, 7:29:04 AM, you wrote:

FG> Hi Wei,

FG> I'm new at this myself, but I think the problem is that you're trying to use XPath to access the document as a database, and that doesn't work.

FG> What your expressions are saying is (I think) something like "collect all of the <name> nodes of my parent, and test if that nodeset is equal to the person/name I am at".  This is either always
FG> false (if the comparison compares a single node to a set) or else is only true if the person/name happens to match the first /doc/name (if the comparison compares the single node to the first
FG> node of the set).  In any case, it's not going to be what you want, which is to test whether the person/name data matches the data for any /doc/name.

FG> I think you can do what you want by using the key() function.  Try:

FG> <xsl:key name="firstname" match="/doc/name" use="firstname"/>
FG> <xsl:key name="lastname" match="/doc/name" use="lastname"/>
FG> ...
FG> <xsl:template match="person"  >
FG>   <xsl:if test="not(name/firstname=key('firstname',name/firstname) and name/lastname=key('lastname',name/lastname))">
FG>            <ERROR>it's not a foreign key</ERROR>
FG>        </xsl:if>
FG>  </xsl:template>

FG> Also, if you have control of the document format, you have an awful lot of repeated data.  You could do something of the form:

FG> <doc>
FG>  <person name="n1">
FG>   <age>...</age>
FG>  </person>
FG>  <name id="n1">
FG>   <firstname>...</firstname>
FG>   <lastname>...</lastname>
FG>  </name>

FG> Then, in the XSL:

FG> <xsl:key name="namekey" match="/doc/name" use="@id"/>
FG> ...
FG> <xsl:template match="person">
FG>   <xsl:if test="not(key('namekey',@name))">
FG>     <ERROR>...</ERROR>
FG>   </xsl:if>
FG> </xsl:template>

FG> This keeps all your names in one place.

FG> Regards,
FG> Frank

  Hi, thanks first!
    but it seems this doesn't work!
    <xsl:if test="not(name/firstname=key('firstname',name/firstname) and name/lastname=key('lastname',name/lastname))">
    I think that key('firstname',name/firstname) returns a nodeset
    and compare the current node name/firstname with this nodeset
    can't get the correct result.
    i try to change not(key('firstname',name/firstname))
    it seems can give me the correct answer for only comparing the
    firstname, because if the selected nodeset is null , then output
    the error. but I can't figure out how to check the firstname and
    lastname together ?
    I attach my file  foo2.xml, fk.xsl , and use the
    SimpleTransform.java in the example.
    Thanks all!
Best regards,
 Wei                            mailto:Wei_Cheng@baylor.edu