473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Using XPath with non-standard XML

23
Here is the outline of the XML data that we are getting from an external source. I would like to use XPath in an xslt to get to the elements that I need. Right now I am only able to get to "/soap:Envelope/soap:Body/n1:ProcessMessage". Anything after that, I'm not able to get it to recognized as a valid path. Is there a proper way to get into the "ns1:CreditDecision" element using XPath rather than using for-each loops with "child::*"?

Thanks for your help!

Expand|Select|Wrap|Line Numbers
  1. <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  2.     <soap:Body>
  3.         <ProcessMessage xmlns="http://localhost/site/ProcessCreditDecision">
  4.             <sPayload>
  5.                 <ns1:ProcessCreditDecision xmlns:ns1="http://www.starstandards.org/STAR">
  6.                     <ns1:ApplicationData></ns1:ApplicationData>
  7.                     <ns1:DataArea>
  8.                         <ns2:Process xmlns:ns2="http://www.openapplications.org/oagis"></ns2:Process>
  9.                         <ns1:CreditDecision>
  10.                             <ns1:Header></ns1:Header>
  11.                             <ns1:Detail>
  12.                                 <ns1:Field1>field 1</ns1:Field1>
  13.                                 <ns1:Field2>field 2</ns1:Field2>
  14.                                 <ns1:Field3>field 3</ns1:Field3>
  15.                                 <ns1:Field4>field 4</ns1:Field4>
  16.                                 <ns1:Field5>field 5</ns1:Field5>
  17.                                 <ns1:Field6>field 6</ns1:Field6>
  18.                             </ns1:Detail>
  19.                         </ns1:CreditDecision>
  20.                     </ns1:DataArea>
  21.                 </ns1:ProcessCreditDecision>
  22.             </sPayload>
  23.         </ProcessMessage>
  24.     </soap:Body>
  25. </soap:Envelope>
  26.  
Oct 23 '08 #1
5 2596
Dormilich
8,658 Expert Mod 8TB
usually with "//ns1:CreditDecision". does it give you an error if you try that?

regards
Oct 24 '08 #2
ianoble
23
I've supplied more detailed xml below, because when I use "//ns1:CreditDecision" for the match property of my template, I get all of the data in the ApplicationData section outputted before the data in my template. Do I need to create another template to account for anything outside of the data I am trying to match?

XML:
Expand|Select|Wrap|Line Numbers
  1. <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  2.     <soap:Body>
  3.         <ProcessMessage xmlns="http://localhost/site/ProcessCreditDecision">
  4.             <sPayload>
  5.                 <ns1:ProcessCreditDecision xmlns:ns1="http://www.starstandards.org/STAR">
  6.                     <ns1:ApplicationData>
  7.                         <ns1:Sender>
  8.                             <ns1:Component>LoanCenter</ns1:Component> 
  9.                             <ns1:Task>CreditDecision</ns1:Task> 
  10.                             <ns1:ReferenceId>00000094022</ns1:ReferenceId> 
  11.                             <ns1:CreatorNameCode>APPRO</ns1:CreatorNameCode> 
  12.                             <ns1:SenderNameCode>XX</ns1:SenderNameCode> 
  13.                         </ns1:Sender>
  14.                         <ns1:CreationDateTime>2008-05-15T14:30:33Z</ns1:CreationDateTime> 
  15.                         <ns1:BODId>40008351</ns1:BODId> 
  16.                         <ns1:Destination>
  17.                             <ns1:DestinationNameCode>XX</ns1:DestinationNameCode> 
  18.                         </ns1:Destination>
  19.                     </ns1:ApplicationData>
  20.                     <ns1:DataArea>
  21.                         <ns2:Process xmlns:ns2="http://www.openapplications.org/oagis"></ns2:Process>
  22.                         <ns1:CreditDecision>
  23.                             <ns1:Header></ns1:Header>
  24.                             <ns1:Detail>
  25.                                 <ns1:Field1>field 1</ns1:Field1>
  26.                                 <ns1:Field2>field 2</ns1:Field2>
  27.                                 <ns1:Field3>field 3</ns1:Field3>
  28.                                 <ns1:Field4>field 4</ns1:Field4>
  29.                                 <ns1:Field5>field 5</ns1:Field5>
  30.                                 <ns1:Field6>field 6</ns1:Field6>
  31.                             </ns1:Detail>
  32.                         </ns1:CreditDecision>
  33.                     </ns1:DataArea>
  34.                 </ns1:ProcessCreditDecision>
  35.             </sPayload>
  36.         </ProcessMessage>
  37.     </soap:Body>
  38. </soap:Envelope>
  39.  
XSLT:
Expand|Select|Wrap|Line Numbers
  1.     <xsl:template name="CreditDecision" match="//ns1:CreditDecision" >
  2.         <html>
  3.             <head>
  4.                 <title>test xml</title>
  5.             </head>
  6.             <body style="font-family:arial;font-size:10pt;">
  7.                 <div>
  8.                     <xsl:for-each select="child::*"><!--CreditDecision//-->
  9.                         <xsl:if test="name()='ns1:Detail'">
  10.                             <xsl:for-each select="child::*">
  11.                                 <h2><xsl:value-of select="replace(name(),'ns1:','')" /></h2>
  12.                                 <div style="padding-left:30px;">
  13.                                 <xsl:if test="*[not(*)]">
  14.                                     <xsl:for-each select="child::*">
  15.                                         <xsl:choose>
  16.                                             <xsl:when test="*[not(*)]"><!--If the element has children//-->
  17.                                                 <div>
  18.                                                 <h4><xsl:value-of select="replace(name(),'ns1:','')" /></h4>
  19.                                                 <xsl:for-each select="child::*">
  20.                                                     <xsl:choose>
  21.                                                         <xsl:when test="*[not(*)]"><!--If the element has children//-->
  22.                                                             <div>
  23.                                                             <h4><xsl:value-of select="replace(name(),'ns1:','')" /></h4>
  24.                                                             <xsl:for-each select="child::*">
  25.                                                                 <xsl:call-template name="main" />
  26.                                                             </xsl:for-each>
  27.                                                             </div>
  28.                                                         </xsl:when>
  29.                                                         <xsl:otherwise>
  30.                                                             <xsl:call-template name="main" />
  31.                                                         </xsl:otherwise>
  32.                                                     </xsl:choose>
  33.                                                 </xsl:for-each>
  34.                                                 </div>
  35.                                             </xsl:when>
  36.                                             <xsl:otherwise>
  37.                                                 <xsl:call-template name="main" />
  38.                                             </xsl:otherwise>
  39.                                         </xsl:choose>
  40.                                     </xsl:for-each>
  41.                                 </xsl:if>
  42.                                 </div>
  43.                             </xsl:for-each>
  44.                         </xsl:if>
  45.                     </xsl:for-each>
  46.                 </div>
  47.             </body>
  48.         </html>
  49.     </xsl:template>
  50.     <xsl:template name="main">
  51.         <div>
  52.             <span style="font-weight:bold;width:175px;text-align:right;padding-right:5px;">
  53.                 <xsl:value-of select="name()"/>
  54.             </span>
  55.             <span>
  56.                 <xsl:value-of select="."/>
  57.             </span>
  58.         </div>
  59.     </xsl:template>
  60.  
Oct 24 '08 #3
jkmyoung
2,057 Expert 2GB
Can we see where you declare namespace ns1?

Be careful of using generic namespace names like ns1, ns2 etc... It doesn't really give you any other information.

xmlns:ns1="http://www.starstandards.org/STAR"
Oct 24 '08 #4
ianoble
23
Can we see where you declare namespace ns1?

Be careful of using generic namespace names like ns1, ns2 etc... It doesn't really give you any other information.

xmlns:ns1="http://www.starstandards.org/STAR"
Sure:

Expand|Select|Wrap|Line Numbers
  1. <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
  2.     xmlns:ns1="http://www.starstandards.org/STAR" xmlns:ns2="http://www.openapplications.org/oagis" xmlns:n1="http://www.approsystems.com/CUDL/ProcessCreditDecision">
  3.  
Oct 24 '08 #5
Dormilich
8,658 Expert Mod 8TB
instead of using replace(name(),'ns1:','') you can use the much more comfortable local-name().
I have no idea why the ApplicationData are printed, but it occurs only when a template (//ns1:*) is called. worked around it with <xsl:for-each> (assuming you want the field element names)
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="iso-8859-1" ?>
  2. <xsl:stylesheet version="1.0" 
  3.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  4.     xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
  5.     xmlns:ns1="http://www.starstandards.org/STAR" 
  6.     xmlns:ns2="http://www.openapplications.org/oagis" 
  7.     xmlns:n1="http://www.approsystems.com/CUDL/ProcessCreditDecision">
  8.  
  9.     <xsl:template match="/" >
  10.         <html>
  11.             <head>
  12.                 <title>test xml</title>
  13.             </head>
  14.             <body>
  15.                 <div>
  16.                     <xsl:for-each select="//ns1:Detail/*">
  17.                         <xsl:value-of select="local-name(.)"/>
  18.                     </xsl:for-each>
  19.                 </div>
  20.             </body>
  21.         </html>
  22.     </xsl:template>
  23.  
  24. </xsl:stylesheet>
regards
Oct 25 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
4
by: Timo Nentwig | last post by:
Hi! Seems that * cannot stand for non-existing nodes, i.e. /html/*/title will not match <html> <title>won't match</title>
14
by: inquirydog | last post by:
Hi- One frusterating thing for me with xsl is that I don't know how to make xslt throw some sort of exception when a value-of path does not exist. For instance, suppose I have the following...
3
by: Johannes Koch | last post by:
Hi there, I'd like to apply an xpath to both HTML and XHTML documents. First I create a DOM document with a Java DOM parser, then apply the xpath with Xalan's XPathAPI class. The problem is that...
6
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows...
4
by: Andreas Håkansson | last post by:
I have a price of XML that looks like this <Root> <SomeNode> ..... </SomeNode> <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> ... </Signature> </Root>
4
by: eric.goforth | last post by:
In an xsl stylesheet, I have <xsl:when test="string-length(//mystuff) &gt; 0"> <xsl:attribute name="someattribute">blahblahblah</xsl:attribute> </xsl:when> In the xml that mystuff is several...
6
by: Derek Hart | last post by:
I bring in an xml file into vb.net by using xmlDoc.LoadXml(XMLString) - I run xpath statements against the xml file to grab data from it, so I use, as an example, //Vehicles/Vehicles/@make to get...
3
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; We have a TreeView that represents an xml file (or it's schema is a more accurate statement). We want to have a double click on a node in the tree generate the XPath to get to that node. ...
4
by: syed.akhlaq | last post by:
Hi, Does anyone know how can I validate XPath expressions using xsd schema? Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.