Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 2nd, 2006, 11:16 PM
Authorized User
 
Join Date: Nov 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Two problems regarding selections

Hello all!

I have to problems with which I have struggled all afternoon.

Regarding the first one I have the following XML file:
   <Class1>
      <Attribute1> Atr1 </Attribute1>
      <Attribute2> Atr2 </Attribute2>
      <Attribute3> Atr3 </Attribute3>
   <Class1>

I want to be able to select Atr1 from <Attribute1> only if the tag <Attribute3> exists. How can I do that?

My second problem is that I have classes and sub-classes in a xml file . This relationship is specified as follows:
   <Class idClass="1">
      <Name>Class1</Name>
   </Class>
   <Class idClass="2"/>
      <Name>Class2</Name>
   </Class>
   <Class idClass="3"/>
      <Name>Class3</Name>
   </Class>
   <SuperClass>
      <Name>Class1</Name>
      <ClassIDSubClass idClass="2"/>
      <ClassIDSubClass idClass="3"/>
   </SuperClass>

My problem is that in my second file, I want something like this:
   <Class name="Class2">
      <SubClassOf name="Class1"/>
   </Class>
So I would have to get the idClass for each class I have, search in the ClassIDSubClass to see if it is there, and if it is, take the name of the SuperClass (the value of the <Name> tag from the <SuperClass> block). Is this possible at all?

Best regards,
Fred
 
Old November 3rd, 2006, 04:41 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Forums like this work best if you raise separate problems in separate postings.

1. select="Class1[Attribute3]/Attribute1"

2.

<xsl:key name="sc" match"=SuperClass" use="ClassIDSubClass/@idClass"/>

then

<xsl:template match="Class">
 ...
 <xsl:value-of select="key('sc', @idClass)/Name"



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 3rd, 2006, 09:04 AM
Authorized User
 
Join Date: Nov 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your help. I will post separate threads the next time.

Best regards,
Fred
 
Old November 3rd, 2006, 10:46 AM
Authorized User
 
Join Date: Nov 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again. I tried for the first problem to set the value like this:
<xsl:template match="Class1">
   <xsl:attribute name="atr" select="Class1[Attribute3]/Attribute1"/>
   <attrValue value="{@atr}"/>
</xsl:template>
but it does not work.

Please advise.

Best regards,
Fred
 
Old November 3rd, 2006, 11:07 AM
Authorized User
 
Join Date: Nov 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, so I managed to solve it, after also reading some previous posts, by using
<xsl:template match="Class1[Attribute3]/Attribute1">
   <attrValue value="{.}"/>
</xsl:template>
I need to read more about XSLT first before doing anything more!

Best regards,
Fred
 
Old November 3rd, 2006, 11:22 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

select="Class1[Attribute3]/Attribute1" is short for select="child::Class1[child::Attribute3]/child::Attribute1". But if you're already at a Class1 then you don't need to go down to a child::Class1, just use select=".[Attribute3]/Attribute1" in XSLT 2.0, or "self::*[Attribute3]/Attribute1" in 1.0

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 3rd, 2006, 02:08 PM
Authorized User
 
Join Date: Nov 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Makes perfect sense. Regarding my second problem, the way I want to construct this
<Class name="Class2">
   <SubClassOf name="Class1"/>
</Class>
is when I get to a Class element (i.e. one that has the idClass="2"), I want to look into the <SuperClass> construction to see if its idClass is present there (ClassIDSubClass with its idClass), and if it is, then add the <SubClassOf> element to the final construction having the value of "name" equal to the content of the tag "Name" in the <SuperClass> block. If it isn't (which means it is the superclass), then I just do <Class name="Class1"> for example.

I tried doing something like you mentioned in an earlier post, but I get a " ERROR: 'Syntax error in 'null'.' ". (I have: <SubClassOf name="{key('sc', @idClass)/Name>)}"/>.

Please advice if possible. Thank you!

Best regards,
Fred
 
Old November 3rd, 2006, 02:57 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your XPath expression contains an unmatched right bracket. But if that's your error then you seem to be using an XSLT processor with remarkably poor error messages. Give Saxon a try.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old November 3rd, 2006, 05:52 PM
Authorized User
 
Join Date: Nov 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am using the javax.xml.transform and javax.xml.parsers packages that apparently came with the jdk 1.5 that I have. Regarding the null error, you were right, I had a ">" which was not supposed to be there. Know I get nothing there (...=""). I will do some more digging to see what the problem might be.

Best regards,
Fred
 
Old November 3rd, 2006, 06:40 PM
Authorized User
 
Join Date: Nov 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

So I'm using JASP 1.3; I don't yet know which version of XSLT it supports, but I believe it's 1.0, not 2.0.

Best regards,
Fred





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Manage ComboBox Selections within JSP Ofirshani JSP Basics 1 March 23rd, 2007 08:01 AM
Dropdown list with multiple selections tombert ASP.NET 1.0 and 1.1 Basics 15 March 7th, 2007 07:38 PM
programmatically setting selections on a multiSELE rupen Javascript How-To 6 November 20th, 2006 10:14 AM
parameter: 6 selections maximum krema Reporting Services 0 February 20th, 2006 05:07 AM
C# ListBox Working With Multiple Selections pro-logic C# 1 October 9th, 2005 02:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.