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 February 21st, 2007, 07:40 AM
Registered User
 
Join Date: Feb 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT Help Needed

Hi I'm trying to transform the following XML using the following XSLT so that if a Company name is populated then only it and not the Individual's name are transformed, and the Individual's name transformed all other times.

But the transformation keeps skipping the Company name regardless of whether it is populated or not; unless the following line is added in addition to the company name tags already in the XML:
<CompanyName>Company X</CompanyName>


Could someone please help me?
I'm sure I've missed something minor, but this is driving me mad.

Thanks
Jason

XML
Code:
<?xml version="1.0"?>
<Letter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
  <CompanyName xmlns="http://www.xyz.org/ABC">Company X</CompanyName>
  <Title xmlns="http://www.xyz.org/ABC">Mr</Title>
  <FirstName xmlns="http://www.xyz.org/ABC">Joe</FirstName>
  <MiddleName xmlns="http://www.xyz.org/ABC">John</MiddleName>
  <LastName xmlns="http://www.xyz.org/ABC">Bloggs</LastName>
</Letter>
XSLT
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" exclude-result-prefixes="xx" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xx="http://www.xyz.org/ABC" xmlns="http://www.xyz.org/XYZ">
  <xsl:template match="Letter">
    <Details>
      <Clients>
        <Client>
          <xsl:apply-templates select="xx:Client"/>
          <ClientName>
        <xsl:choose>
            <xsl:when test="/Letter/CompanyName!=''">    
                <Business>
                    <xsl:apply-templates select="xx:CompanyName"/>
                </Business>
            </xsl:when>
            <xsl:otherwise>
                <Individual>
                    <xsl:apply-templates select="xx:Title"/>
                    <xsl:apply-templates select="xx:FirstName"/>
                    <xsl:apply-templates select="xx:MiddleName"/>
                    <xsl:apply-templates select="xx:LastName"/>
                </Individual>
            </xsl:otherwise>
                  </xsl:choose>
    </ClientName>
        </Client>
      </Clients>
    </Details>
  </xsl:template>



<xsl:template match="xx:CompanyName">
        <BusinessName>
            <xsl:value-of select ="/Letter/xx:CompanyName"/>
        </BusinessName>
</xsl:template>

<xsl:template match="xx:Title">
          <IndividualTitle>
            <xsl:value-of select ="."/>
        </IndividualTitle>
</xsl:template>

<xsl:template match="xx:FirstName">
        <IndividualFirstName>
            <xsl:value-of select ="."/>
        </IndividualFirstName>
</xsl:template>

<xsl:template match="xx:LastName">
        <IndividualLastName>
            <xsl:value-of select ="."/>
        </IndividualLastName>
</xsl:template>

<xsl:template match="xx:MiddleName">
        <IndividualMiddleName>
            <xsl:value-of select ="."/>
        </IndividualMiddleName>
</xsl:template>
</xsl:stylesheet>
 
Old February 21st, 2007, 07:57 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The test should be

<xsl:when test="/Letter/xx:CompanyName!=''">


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 21st, 2007, 08:08 AM
Registered User
 
Join Date: Feb 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, I just tried that. however it is still avoiding the CompanyName value from the XML.
Giving the result of:
Code:
<?xml version="1.0" encoding="utf-16"?>
<Details xmlns="http://www.xyz.org/XYZ">
   <Clients>
      <Client>
         <ClientName>
            <Individual>
               <IndividualTitle>Mr</IndividualTitle>
               <IndividualFirstName>Joe</IndividualFirstName>
               <IndividualMiddleName>John</IndividualMiddleName>
               <IndividualLastName>Bloggs</IndividualLastName>
            </Individual>
         </ClientName>
      </Client>
   </Clients>
</Details>
Whereas I would like the result of:
Code:
<?xml version="1.0" encoding="utf-16"?>
<Details xmlns="http://www.xyz.org/XYZ">
   <Clients>
      <Client>
         <ClientName>
            <Business>
               <BusinessName>Company X</BusinessName>
            </Business>
         </ClientName>
      </Client>
   </Clients>
</Details>
 
Old February 21st, 2007, 08:40 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I can't debug code that I can't see.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 21st, 2007, 03:32 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

You left it out of the construct:

Code:
<xsl:choose>
            <xsl:when test="/Letter/CompanyName!=''">    
                <Business>
                    <xsl:apply-templates select="xx:CompanyName"/>
                </Business>
            </xsl:when>
            <xsl:otherwise>
                <Individual>
                    <xsl:apply-templates select="xx:Title"/>
                    <xsl:apply-templates select="xx:FirstName"/>
                    <xsl:apply-templates select="xx:MiddleName"/>
                    <xsl:apply-templates select="xx:LastName"/>
                </Individual>
            </xsl:otherwise>
                  </xsl:choose>
You are using <xsl:otherwise> so its one or the other....

Post your new code.



 
Old February 21st, 2007, 04:10 PM
Registered User
 
Join Date: Feb 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
I'm not sure exactly what you mean, I've used xsl:otherwise because there are 2 outputs that I'm after:
(The previously displayed outputs were manually created)

1. If Company details are provided (even if Individual details are provided) I only want the Company details transformed.
eg.
<Clients>
   <Client>
      <ClientName>
         <Business>
           <BusinessName>Company X</BusinessName>
         </Business>
      </ClientName>
   </Client>
</Clients>

2. If only Individual details are provided then only these are transformed i.e. no blank Company tags
eg.
<Clients>
   <Client>
      <ClientName>
         <Individual>
            <IndividualTitle>Mr</IndividualTitle>
            <IndividualFirstName>Joe</IndividualFirstName>
            <IndividualMiddleName>John</IndividualMiddleName>
            <IndividualLastName>Bloggs</IndividualLastName>
         </Individual>
      </ClientName>
   </Client>
</Clients>

But I'm only getting Output 2 even if the company Details are provided.

Input XML
Code:
<?xml version="1.0"?>
<Letter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
  <CompanyName xmlns="http://www.xyz.org/ABC">Company X</CompanyName> <!-- As Company is here I should get Output 1, but I'm not.
  <Title xmlns="http://www.xyz.org/ABC">Mr</Title>
  <FirstName xmlns="http://www.xyz.org/ABC">Joe</FirstName>
  <MiddleName xmlns="http://www.xyz.org/ABC">John</MiddleName>
  <LastName xmlns="http://www.xyz.org/ABC">Bloggs</LastName>
</Letter>
XSLT
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" exclude-result-prefixes="xx" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xx="http://www.xyz.org/ABC" xmlns="http://www.xyz.org/XYZ">
  <xsl:template match="Letter">
    <Details>
      <Clients>
        <Client>
          <xsl:apply-templates select="xx:Client"/>
          <ClientName>
        <xsl:choose>
            <xsl:when test="/Letter/xx:CompanyName!=''">    
                <Business>
                    <xsl:apply-templates select="xx:CompanyName"/>
                </Business>
            </xsl:when>
            <xsl:otherwise>
                <Individual>
                    <xsl:apply-templates select="xx:Title"/>
                    <xsl:apply-templates select="xx:FirstName"/>
                    <xsl:apply-templates select="xx:MiddleName"/>
                    <xsl:apply-templates select="xx:LastName"/>
                </Individual>
            </xsl:otherwise>
                  </xsl:choose>
    </ClientName>
        </Client>
      </Clients>
    </Details>
  </xsl:template>



<xsl:template match="xx:CompanyName">
        <BusinessName>
            <xsl:value-of select ="/Letter/xx:CompanyName"/>
        </BusinessName>
</xsl:template>

<xsl:template match="xx:Title">
          <IndividualTitle>
            <xsl:value-of select ="."/>
        </IndividualTitle>
</xsl:template>

<xsl:template match="xx:FirstName">
        <IndividualFirstName>
            <xsl:value-of select ="."/>
        </IndividualFirstName>
</xsl:template>

<xsl:template match="xx:LastName">
        <IndividualLastName>
            <xsl:value-of select ="."/>
        </IndividualLastName>
</xsl:template>

<xsl:template match="xx:MiddleName">
        <IndividualMiddleName>
            <xsl:value-of select ="."/>
        </IndividualMiddleName>
</xsl:template>
</xsl:stylesheet>
 
Old February 21st, 2007, 05:38 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

I am not sure where the problem is,. I ran your code and got the following result:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<Details xmlns="http://www.xyz.org/XYZ">
    <Clients>
        <Client>
            <ClientName>
                <Business>
                    <BusinessName>Company X</BusinessName>
                </Business>
            </ClientName>
        </Client>
    </Clients>
</Details>
 
Old February 21st, 2007, 06:07 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I ahree with bonecrusher. Your XSLT code is correct, if you are not getting the right output then you are either making a mistake in the way you run it or you are using a buggy XSLT processor.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 22nd, 2007, 02:02 AM
Registered User
 
Join Date: Feb 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for all your help.
I don't know why it was not transforming correctly for me, and I've tried a couple of different tools.

Anyway after banging my head against a brick wall, I discovered that by replacing the line
<xsl:when test="/Letter/xx:CompanyName!=''">
with
<xsl:when test="string-length(/Letter/xx:CompanyName)!=0">

It now works exactly the way I wanted it to.

Cheers
Jason

 
Old February 22nd, 2007, 04:27 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Please do the community a service by telling us which XSLT processor has this nasty bug, and by notifying it to the vendor.

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
Help needed on XSLT baruprasad XSLT 8 October 21st, 2008 09:03 AM
help needed in XSLT ddeokarb XSLT 1 November 22nd, 2007 10:41 AM
xslt help desperately needed daula7 XSLT 2 May 10th, 2006 12:09 PM
help needed regarding xslt pradeep.mallavarapu XSLT 1 April 19th, 2006 05:58 AM
help needed for xslt rameshnarayan XSLT 2 September 19th, 2005 03:58 AM





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