Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
| Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML 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 October 17th, 2013, 02:52 AM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Use an attributes value in a conditional statement in XPATH

I've been struggling with building this code for about 6 hours now and each forum I consult do not seem to have a viable solution. Hoping to get some help here.

I have an XML that references attributes of idOwner. I want to use the value of one of the attributes in a conditional statement in an XSLT.

Here is a snippet of the XML:
Code:
<Id idOwner="Onboarding_Event"> 
     <IdValue>US Onboarding</IdValue> 
</Id>
Code:
<xsl:choose>
<xsl:when test="@idOwner = 'Onboarding_Event' and hr:IdValue/text() = 'US Onboarding'">
<xsl:if test="@idOwner = 'Managers_Email_ID' and hr:IdValue !=''"> 
	<Person>
	  <Name>Manager</Name>
		  <Value><xsl:value-of select="hr:IdValue"/></Value>
            </Person>
</xsl:if>

<xsl:if test="@idOwner = 'Onboarding_Co' and hr:IdValue !=''"> 
	<Person>
	  <Name>Onboarding Coordinator</Name>
		<Value><xsl:value-of select="hr:IdValue"/></Value>
            </Person>
</xsl:if>
<xsl:if test="@idOwner = 'HR_Rep_email_ID' and hr:IdValue !=''">
	<Person>
	  <Name>HR Representative</Name>
   	             <Value><xsl:value-of select="hr:IdValue"/></Value>
	</Person>
</xsl:if>
</xsl:when>

<xsl:otherwise>											
  <xsl:if test="@idOwner = 'Managers_Email_ID' and hr:IdValue !=''"> 
        <Person>
	  <Name>EMEA Manager</Name>
	  <Value>Test</Value>
        </Person>
  </xsl:if>

   <xsl:if test="@idOwner = 'Onboarding_Co' and hr:IdValue !=''"> 
	<Person>
		<Name>EMEA Onboarding Coordinator</Name>
		<Value>Test</Value>
	</Person>
    </xsl:if>

	<xsl:if test="@idOwner = 'HR_Rep_email_ID' and hr:IdValue !=''">
		<Person>
		  <Name>EMEA HR Representative</Name>
			<Value>Test</Value>
		</Person>
			</xsl:if>
	</xsl:otherwise>
				
</xsl:choose>
No matter how I build the When statement it always defaults to the <Otherwise> statement. Its as if the When statement is never evaluated.

any help would be greatly appreciated.

Last edited by SRTrice; October 17th, 2013 at 03:00 AM..
 
Old October 17th, 2013, 03:43 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>I've been struggling with building this code for about 6 hours now and each forum I consult do not seem to have a viable solution.

I wonder if you're using the most effective strategy for solving your problem. Given that you are on a Wrox site, I would expect you to tell us how long you have spent looking for a solution in Wrox's wonderful technical books.

Your code seems very strange, because you will only go into the "when" branch if @idOwner = 'Onboarding_Event', but you will only execute any of the contained "if" expressions if @idOwner is some other value. So I would expect it to always execute the when branch, and none of the "if" expressions within the "when" branch.

I think the most likely explanation for your problems is something to do with context. You haven't shown enough of your source and stylesheet for us to see the full context, which is often a telltale sign that a poster doesn't understand the importance of context. Are you sure the Id element shown is the context item? Are you sure the namespace context is correct? If so, show us enough code so that we can be sure of this too.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old October 17th, 2013, 03:58 AM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

well here's the thing. the customer is asking for something that I do not have a case study for but perhaps more background will indeed help.
The product that will use this XSLT invokes specific events for employees to complete. They can have multiple events and each event has its own definition.
Event 1 name = event1
Event 2 name = event2

People involved with Event1 have labels called:
Event1 Coordinator
Event1 Manager

And the same for Event2 respectively.
I need to build logic in an XSLT that determines first the name of the event and then based on that specific name populate the correct corresponding people.

typically i only have to make small edits to existing xslt and we have tons of examples. but in this one case they are asking for customization that I can't figure out. I am a novice :) but learning.
I hope that helps.

here is my full xml:
Code:
<Candidate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ns.hr-xml.org/2007-04-15"> 
<CandidateProfile> 
<PersonalData> 
		<PersonName> 
				<LegalName>test</LegalName> 
				<MiddleName></MiddleName> 
				<FamilyName>lane</FamilyName> 
		</PersonName> 
		<ContactMethod> 
				<Location>home</Location> 
				<Telephone> 
						<FormattedNumber>1234567</FormattedNumber> 
				</Telephone> 
				<InternetEmailAddress>test email</InternetEmailAddress > 
				<PostalAddress> 
						<CountryCode>United Kingdom</CountryCode> 
						<PostalCode>12345</PostalCode> 
						<Region></Region> 
						<Municipality></Municipality> 
						<DeliveryAddress> 
								<AddressLine>117 Pennypacker Road</AddressLine> 
								<StreetName></StreetName> 
						</DeliveryAddress> 
				</PostalAddress> 
		</ContactMethod> 
		<ContactMethod> 
				<Location>onPerson</Location> 
				<Telephone> 
						<FormattedNumber></FormattedNumber> 
				</Telephone> 
		</ContactMethod> 
		<BiologicalDescriptors> 
				<GenderCode></GenderCode> 
		</BiologicalDescriptors> 
</PersonalData> 
<EmploymentHistory> 
		<EmployerOrg> 
				<EmployerOrgName></EmployerOrgName> 
				<PositionHistory> 
						<Title></Title> 
						<OrgName> 
								<OrganizationName></OrganizationName> 
						</OrgName> 
						<StartDate> 
								<Year></Year> 
						</StartDate> 
						<EndDate> 
								<Year></Year> 
						</EndDate> 
				</PositionHistory> 
		</EmployerOrg> 
</EmploymentHistory> 
<EmploymentHistory> 
		<EmployerOrg> 
				<EmployerOrgName></EmployerOrgName> 
				<PositionHistory> 
						<Title></Title> 
						<OrgName> 
								<OrganizationName></OrganizationName> 
						</OrgName> 
						<StartDate> 
								<Year></Year> 
						</StartDate> 
						<EndDate> 
								<Year></Year> 
						</EndDate> 
				</PositionHistory> 
		</EmployerOrg> 
</EmploymentHistory> 
<EducationHistory> 
		<SchoolOrInstitution> 
				<SchoolName>University of Fun</SchoolName> 
				<Degree degreeType="ASME"> 
						<DegreeName>Mechanical</DegreeName> 
						<DegreeDate> 
								<Year>2013</Year> 
						</DegreeDate> 
						<DegreeMeasure> 
								<EducationalMeasure> 
										<MeasureSystem>GPA</MeasureSystem> 
										<MeasureValue> 
												<StringValue></StringValue> 
										</MeasureValue> 
								</EducationalMeasure> 
						</DegreeMeasure> 
				</Degree> 
		</SchoolOrInstitution> 
</EducationHistory> 
<EducationHistory> 
		<SchoolOrInstitution> 
				<SchoolName></SchoolName> 
				<Degree degreeType=""> 
						<DegreeName></DegreeName> 
						<DegreeDate> 
								<Year></Year> 
						</DegreeDate> 
						<DegreeMeasure> 
								<EducationalMeasure> 
										<MeasureSystem></MeasureSystem> 
										<MeasureValue> 
												<StringValue></StringValue> 
										</MeasureValue> 
								</EducationalMeasure> 
						</DegreeMeasure> 
				</Degree> 
		</SchoolOrInstitution> 
</EducationHistory> 
<EducationHistory> 
		<SchoolOrInstitution> 
				<SchoolName></SchoolName> 
				<Degree degreeType=""> 
						<DegreeName></DegreeName> 
						<DegreeDate> 
								<Year></Year> 
						</DegreeDate> 
						<DegreeMeasure> 
								<EducationalMeasure> 
										<MeasureSystem></MeasureSystem> 
										<MeasureValue> 
												<StringValue></StringValue> 
										</MeasureValue> 
								</EducationalMeasure> 
						</DegreeMeasure> 
				</Degree> 
		</SchoolOrInstitution> 
</EducationHistory> 
<UserArea> 
		<Id idOwner="Hiring_Bonus"> 
				<IdValue>Hiring</IdValue> 
		</Id> 
		<Id idOwner="Salary"> 
				<IdValue>5000</IdValue> 
		</Id> 
		<Id idOwner="Is_this_person_a_for"> 
				<IdValue>Test Case</IdValue> 
		</Id> 
		<Id idOwner="Start_Date"> 
				<IdValue>2013-10-01</IdValue> 
</UserArea> 
</CandidateProfile> 
<UserArea> 
<Id idOwner="Manager"> 
		<IdValue>some manager</IdValue> 
</Id> 
<Id idOwner="Managers_Email_ID"> 
		<IdValue>email address</IdValue> 
</Id> 
<Id idOwner="HR_Representative"> 
		<IdValue>test</IdValue> 
</Id> 
<Id idOwner="Onboarding_Co"> 
		<IdValue>Onboarding Coordinator</IdValue> 
</Id> 
<Id idOwner="Onboarding_Event"> 
		<IdValue>US Onboarding</IdValue> 
</Id> 
<Id idOwner="HR_Rep_email_ID"> 
		<IdValue>test email</IdValue> 
</Id> 

</UserArea> 
</Candidate>
And here is the portion of the XSLT that i need to build and what i've done so far - that fails. It always defaults to the otherwise tag no matter what is being passed on the xml. so it recognizes my Choose statement but never seems to evaluate the first When statement.

Code:
<Event>
<xsl:for-each select="hr:UserArea/hr:Id"> 
	<xsl:if test="@idOwner = 'Onboarding_Event' and hr:IdValue !=''">
		<Name><xsl:value-of select="hr:IdValue"/></Name>
	</xsl:if>	
</xsl:for-each>

<!-- Start Date -->
<xsl:for-each select="hr:CandidateProfile/hr:UserArea/hr:Id">
<xsl:if test="@idOwner = 'Start_Date' and hr:IdValue !=''">
	<Date>
	  <Name>Start</Name>
			<Value><xsl:value-of select="hr:IdValue"/></Value>
	</Date>
</xsl:if>

<!-- Category: Type of Hire is in the Candidate Profile/ User Area -->
	<xsl:if test="@idOwner = 'TypeOfHire' and hr:IdValue !=''">
		<Category>
		  <Name>Type of Hire</Name>
			<Value><xsl:value-of select="hr:IdValue"/></Value>
			<Type>Code</Type>
		</Category>
	</xsl:if>

</xsl:for-each>

<!-- People -->
<!-- User Area -->
<xsl:for-each select="hr:UserArea/hr:Id"> 
<!--THIS IS WHAT FAILS:	-->	
<xsl:choose>
<xsl:when test="@idOwner = 'Onboarding_Event' and hr:IdValue = 'US Onboarding'"> 
	  <xsl:if test="@idOwner = 'Managers_Email_ID' and hr:IdValue !=''"> 
		<Person>
		  <Name>Manager</Name>
		  <Value><xsl:value-of select="hr:IdValue"/></Value>
		</Person>
	  </xsl:if>

	   <xsl:if test="@idOwner = 'Onboarding_Co' and hr:IdValue !=''"> 
		<Person>
		  <Name>Onboarding Coordinator</Name>
		  <Value><xsl:value-of select="hr:IdValue"/></Value>
		</Person>
		</xsl:if>

	  <xsl:if test="@idOwner = 'HR_Rep_email_ID' and hr:IdValue !=''">
		<Person>
		  <Name>HR Representative</Name>
		  <Value><xsl:value-of select="hr:IdValue"/></Value>
		</Person>
	  </xsl:if>
</xsl:when>
<xsl:otherwise>											
	  <xsl:if test="@idOwner = 'Managers_Email_ID' and hr:IdValue !=''"> 
		<Person>
		  <Name>EMEA Manager</Name>
		  <Value>Test</Value>
		</Person>
	  </xsl:if>

	   <xsl:if test="@idOwner = 'Onboarding_Co' and hr:IdValue !=''"> 
		<Person>
		  <Name>EMEA Onboarding Coordinator</Name>
		  <Value>Test</Value>
		</Person>
		</xsl:if>

	  <xsl:if test="@idOwner = 'HR_Rep_email_ID' and hr:IdValue !=''">
		<Person>
		  <Name>EMEA HR Representative</Name>
		  <Value>Test</Value>
		</Person>
	  </xsl:if>
</xsl:otherwise>

</xsl:choose>
</xsl:for-each>
        </Event>
 
Old October 17th, 2013, 04:05 AM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
you will only go into the "when" branch if @idOwner = 'Onboarding_Event', but you will only execute any of the contained "if" expressions if @idOwner is some other value. So I would expect it to always execute the when branch, and none of the "if" expressions within the "when" branch.
I believe this is what is occurring... but i can't figure out how to fix it because those additional if statements are needed to determine what name should be applied to the people.

Shucks.. I think you may have actually helped me figure it out by that statement as I re-read. Is it perhaps that "if" statements in the "when" branch need to be given the full path to reference the value since I am at a different @idOwner node once I'm actually in the "when" statement?
 
Old October 17th, 2013, 04:15 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The context for evaluating the xsl:if instructions is exactly the same as the context for the xsl:choose and xsl:when.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old October 17th, 2013, 04:20 AM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Can you elaborate a bit on that last comment?
I'm not sure if you are saying that's why it is broken or if it was an answer to my prior comment. forgive me
I've used this logic before for many xslts but never when it has to reference an attribute.




Similar Threads
Thread Thread Starter Forum Replies Last Post
XPath multiple conditional statement geek.shrek XSLT 2 January 5th, 2010 07:53 PM
Xpath - Selecting attributes bonekrusher XSLT 3 October 1st, 2008 12:27 PM
Get NodesList by Xpath with Attributes Filters AbrarNazeer XML 5 March 26th, 2008 01:02 AM
Xpath expressions with attributes rbooth XSLT 3 November 11th, 2007 04:51 AM
Conditional IF statement iloveoatmeal Classic ASP Basics 5 July 20th, 2005 08:33 PM





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