Solved

error when calling apply templates when node contains attributes

Posted on 2013-06-06
5
262 Views
Last Modified: 2013-06-06
I have an xml where a node may or may not include attributes.

<A>
<Envelope>
      <Body>
            <TravelRS EchoToken="String" TimeStamp="2013-04-29T19:02:31" xmlns:xs="http://www.w3.org/2001/XMLSchema">
                  <TravelItinerary> ... </TravelItinerary>
      </Body>
</Envelope>
</A>


My xsl does an apply-template to loop through the TravelRS. However, if there are attributes included it does not go through into the code.  How can I work around that?

This is what my xsl has

<xsl:when test="Envelope/Body/TravelRS">
      <xsl:apply-templates select="Envelope/Body/TravelRS"/>
</xsl:when>
0
Comment
Question by:badtz7229
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 39226627
I am not really sure I understand

but are you not mixing up attributes with namespace declarations?

Is the attribute like this? xmlns="foo"
then you need to post a real piece of XML with the namespace (the attributes) in it

likely you can do this in your stylesheet
xmlns:ns1="foo"
('foo' being the namespace copied from the source)

and do
      <xsl:apply-templates select="ns1:Envelope/ns1:Body/ns1:TravelRS"/>

note that you can drop the when clause
      <xsl:apply-templates select="ns1:Envelope/ns1:Body/ns1:TravelRS"/>
will do nothing if the nodes aren't there
0
 

Author Comment

by:badtz7229
ID: 39226748
Gertone:

I didn't realize, but it's not the attributes that is causing the apply-templates to be skipped, but instead it's the xmlns:xs="http://www.w3.org/2001/XMLSchema" line which causes failure.

If i remove this line then success.

I have it as follows:

<TravelRS EchoToken="String" TimeStamp="2013-04-29T19:02:31"                   xmlns="http://webservices..com/XML/2001/08"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

How can i call my template while maintaining those 3 xmlns prefixes?
0
 

Author Closing Comment

by:badtz7229
ID: 39226858
u were right, it was namespaces. i added code to remove these before my xsl is executed. thanks.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39226876
I am not sure about what you say...
but this line
xmlns="http://webservices..com/XML/2001/08"
puts all elements in a default namespace.
and will break te stylesheet snippet you just showed us

xmlns:xs="http://www.w3.org/2001/XMLSchema"
does not hurt unless you have a xs:prefixed-element

Can you post the entire stylesheet and an example element

Do you need the stylesheet to work for both with and without the namespaces?
If that is the case, use one of the following approaches
- use XSLT2 and put a *: in front of each element in each XPath
- make two passes, one to get rid of the namespaces in any case and then the stylesheet you already have. If this approach makes sense, I will make the first stylesheet for you

If it only needs to work in case of namespace declarations... you can hardwire the namespace as I did with the ns1:
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39226924
missed your comment prior to my last comment

anyway, you are welcome
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What kind of framework must i use to do that ? 2 48
DTD and JAVA versions 1 59
Need help with a stick header please 12 33
Displaying Text in Columns in HTML 3 32
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question