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 28th, 2006, 09:10 AM
Registered User
 
Join Date: Nov 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Quest: How to select the second tag?!

Need help with this one.

I have the following XML code (IMS Enterprise standard):

<person>
    <sourcedid>
      <sourcedidtype>"Old"</sourcedidtype>
      <source>NO-FS</source>
      <id>123</id>
    </sourcedid>
    <sourcedid>
      <sourcedidtype>"New"</sourcedidtype>
      <source>NO-FS</source>
      <id>789</id>
    </sourcedid>
    <userid>dunn</userid>
</person>

How can I make my XSLT mapper file select the sourcedid/id where sourcedidtype = "New"? By using a xsl:if or a xsl:when the ordinary way I only retrieve the old sourcedid/id since this one is listed first.

[u]My XSLT so far...</u>

<xsl:template match="person">
<ims_person>
   <sourcedid_id>
      ???
   </sourcedid_id>
   <sourcedid_source>
      <xsl:value-of select="sourcedid/source" />
   </sourcedid_source>
   <userid_id>
      <xsl:value-of select="userid" />
   </userid_id>
</ims_person>
</xsl:template>

Please help me if you have any good suggestions!

PS. Not sure if I need to split up the <sourcedid_id> tag into <sourcedid> and <id> in this case...?
 
Old November 28th, 2006, 09:21 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>How can I make my XSLT mapper file select the sourcedid/id where sourcedidtype = "New"


<xsl:variable name="new">"New"</xsl:variable>
<xsl:value-of select="sourcedid[sourcedidtype=$new]/source" />

(the variable is the easiest way of dealing with the fact that the value is in quotes).

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

Thank you Michael!

That code worked perfectly!

Remember me to cheer for England in the next World Cup :o)

Best regards
gnesland
-Norway-
 
Old November 28th, 2006, 10:05 AM
Registered User
 
Join Date: Nov 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Awww... forgot one thing.

The XML-file will contain both person objects like the example above, but it will also contain object with only a single sourcedid/id tag, and no sourcedid/sourcedidtype tag. The code should therefore be capable to also select the ID when there is no "Old" or "New" sourcedidtype.

The example below illutrates two different objects that may occur in the same file:

<person>
   <sourcedid>
      <source>NO-FS</source>
      <id>111</id>
   </sourcedid>
   <userid>phillip</userid>
</person>
<person>
    <sourcedid>
      <sourcedidtype>"Old"</sourcedidtype>
      <source>NO-FS</source>
      <id>123</id>
    </sourcedid>
    <sourcedid>
      <sourcedidtype>"New"</sourcedidtype>
      <source>NO-FS</source>
      <id>789</id>
    </sourcedid>
    <userid>dunn</userid>
</person>

How can I handle both cases so that I retrieve the single ID when the object contains a single Id, and the New one when the object contains two IDs?


 
Old November 28th, 2006, 11:09 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

These are very basic questions, and I wonder whether you are really going the right way about learning the language. Have you been reading any XSLT books and studying the examples?

A good way to tackle this would be to have two template rules, one for person elements with a single sourcedid, another for those with two.

<xsl:template match="person[count(sourcedid)=1]">
   <sourcedid_source>
      <xsl:value-of select="sourcedid/source" />
   </sourcedid_source>
</xsl:template>

<xsl:template match="person[count(sourcedid)=2]">
   <sourcedid_source>
      <xsl:variable name="new">"New"</xsl:variable>
      <xsl:value-of select="sourcedid[sourcedidtype=$new]/source" />
   </sourcedid_source>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to select only the parent tag and ignore the c Prem Rajesh Kumar XSLT 3 November 6th, 2007 12:53 PM
get displayed name of the <select> tag nikeshkm HTML Code Clinic 1 October 20th, 2006 11:21 PM
can't read value from select tag using javascript kumar_kumar Javascript 2 February 9th, 2005 11:18 PM
Dynamic Select Tag fs22 Javascript How-To 1 December 15th, 2004 08:32 PM
Getting results from Select Multiple HTML tag dpjoff PHP How-To 9 February 2nd, 2004 04:03 PM





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