Connecting Tech Pros Worldwide Help | Site Map

how to retrieve node and child values that has xsi:type value

Newbie
 
Join Date: Oct 2009
Posts: 2
#1: 3 Weeks Ago
I have the below input XML sent by APP1.

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <abc1:QWAARequest 
  3.   xmlns:abc1="http://www.mysite.com/myLink/v1/Test.xsd" 
  4.   xmlns:abc2="http://www.mysite.com/v1" 
  5.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  6.   xsi:schemaLocation="http://www.mysite.com/myLink/v1/Test.xsd Test.xsd">
  7.     <abc1:HostID>TEST</abc1:HostID>
  8.     <abc1:WorkForceEvent>
  9.         <abc2:ProjectElementHasCalendarEntry>
  10.             <abc2:relationshipType>ConfirmedSlot</abc2:relationshipType>
  11.             <abc2:CalendarEntry xsi:type="abc2:AppointmentEvent">
  12.                 <abc2:rescheduleReason>Standard Prefix</abc2:rescheduleReason>
  13.             </abc2:CalendarEntry>
  14.         </abc2:ProjectElementHasCalendarEntry>
  15.     </abc1:WorkForceEvent>
  16. </abc1:QWAARequest>
Another application APP2 sends a different XML file with different prefixes (say xyz1, xyz2 in place of abc1, abc2 in above xml) but with same namespace.

I am trying to build a generic xsl which can work with both xml files. Below is the same:

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" 
  3.                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  4.                 xmlns:prefix1="http://www.mysite.com/myLink/v1/Test.xsd"  
  5.                 xmlns:prefix2="http://www.mysite.com/v1" 
  6.                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  7.  
  8.   <xsl:template match="*">
  9.     <xsl:element name="{name()}">
  10.       <xsl:copy-of select="@*"/>
  11.       <xsl:apply-templates select="node() | text()"/>
  12.     </xsl:element>
  13.   </xsl:template>
  14.  
  15.   <xsl:template match="text()">
  16.     <xsl:copy-of select="."/>
  17.   </xsl:template>
  18.  
  19.   <xsl:template match="prefix1:*">
  20.     <xsl:element name="prefix1:{local-name()}">
  21.       <xsl:copy-of select="@*"/>
  22.       <xsl:apply-templates select="node() | text()"/>
  23.     </xsl:element>
  24.   </xsl:template>
  25.  
  26.   <xsl:template match="prefix2:*">
  27.     <xsl:element name="prefix2:{local-name()}">
  28.       <xsl:copy-of select="@*"/>
  29.       <xsl:apply-templates select="node() | text()"/>
  30.     </xsl:element>
  31.   </xsl:template>
  32.  
  33.   <xsl:template match="prefix1:QWAARequest">
  34.         <Output>
  35.       <xsl:value-of select="prefix1:HostID"/> |
  36.       <xsl:value-of select="prefix1:WorkForceEvent/prefix2:ProjectElementHasCalendarEntry/prefix2:relationshipType" /> |
  37.       <xsl:value-of select="prefix1:WorkForceEvent/prefix2:ProjectElementHasCalendarEntry[prefix2:relationshipType='ConfirmedSlot']/prefix2:CalendarEntry[@xsi:type='prefix2:AppointmentEvent']/prefix2:rescheduleReason" />
  38.     </Output>
  39.     </xsl:template>
  40. </xsl:stylesheet>
The above xsl works fine except where it tries to get the value for "rescheduleReason". I guess it is trying to look for exact value of 'prefix2:AppointmentEvent' but by xml has 'abc2:AppointmentEvent'.

Please suggest how to modify the xsl to interpret the value correctly.

I am using .Net 2.0 programming model to load the xml and xsl to generate the output.

Cheers
Rajani
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,623
#2: 3 Weeks Ago

re: how to retrieve node and child values that has xsi:type value


Quote:

Originally Posted by drajani View Post

The above xsl works fine except where it tries to get the value for "rescheduleReason". I guess it is trying to look for exact value of 'prefix2:AppointmentEvent' but by xml has 'abc2:AppointmentEvent'.

attribute values do not have a namespace (though the attribute nodes have). therefore xsi:type='prefix2:AppointmentEvent' does not exist in your original XML, no matter what the prefix2 namespace is. use [@xsi:type='abc2:AppointmentEvent'] as condition
Newbie
 
Join Date: Oct 2009
Posts: 2
#3: 3 Weeks Ago

re: how to retrieve node and child values that has xsi:type value


Thanks. That way I can never write a generic xsl. Is there any possibility to add a match condition in a template tag which can do a find & replace or something similar (as a workaround)?
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,623
#4: 2 Weeks Ago

re: how to retrieve node and child values that has xsi:type value


how do you mean that?
Reply

Tags
.net 2.0, generic xsl, xsi:type, xsl