?
Solved

Matching a simple tag in XSLT

Posted on 2006-03-23
8
Medium Priority
?
247 Views
Last Modified: 2013-11-19
This is an easy question (and 500 points because I'm in a real hurry...): I have this XML document that looks like:

<?xml version="1.0" encoding="UTF-8"?>
  <DocRoot xmlns="http://namespace1"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://namespace1
                               http://namespace1/schema1.xsd">
  <Node1>...
...etc.

and I wrote a simple XSLT document like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet    
                            xmlns="http://namespace1"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://namespace1
                               http://namespace1/schema1.xsd">
     <xsl:template match="/">
         <html>
             <head>
                 <title>Tiny Output</title>
             </head>
             <body>
                 <xsl:for-each select="DocRoot">
                     <b>hello</b>
                     <br></br>
                 </xsl:for-each>
             </body>
         </html>
     </xsl:template>
   
</xsl:stylesheet>

The purpose is to jump-start my project by figuring out how to match an arbitrary node (in this case, the DocRoot element of the XML document), but when I process this in oXygen, it does not work: it seems that "DocRoot" was never matched, so there is no "hello" in the html output of the process. Any help will be appreciated!
0
Comment
Question by:NGInterface
  • 3
  • 3
  • 2
8 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16276366
If you open your XSL in internet explorer you should get a message saying:

   "Reference to undeclared namespace prefix: 'xsl'. Error processing resource "

This is because you are missing the xsl namespace from your xsl:stylesheet tag. It should read:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                            xmlns="http://namespace1"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://namespace1
                               http://namespace1/schema1.xsd">
0
 

Author Comment

by:NGInterface
ID: 16277293
Actually, I do have that xsl namespace definition, but forgot to paste it in the question...it still seem to miss out the part where I actually want to select value from (all the other parts of the output, such as <html>, etc. are there except for the <b>hello</b><br></br> part)...
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16278188
here is your problem

xmlns="http://namespace1"

this is the default namespace in your source XML,
but cannot be the default namespace in your resulttree,
because the html output is that

try in the XSLT
xmlns:n1="http://namespace1"

and address the node like this
<xsl:for-each select="n1:DocRoot">

that will work
cheers
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 1400 total points
ID: 16278210
this stylesheet works

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:n1="http://namespace1"
     exclude-result-prefixes="n1">
    <xsl:template match="/">
        <html>
            <head>
                <title>Tiny Output</title>
            </head>
            <body>
                <xsl:for-each select="n1:DocRoot">
                    <b>hello</b>
                    <br></br>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
 </xsl:stylesheet>

note the exclude-result-prefixes="n1"
that is to make sure that the n1 namespace node doesn't make it to the resulttree
you would have a xmlns:n1="http://namespace1" in your output, that is not used

cheers

Geert
0
 

Author Comment

by:NGInterface
ID: 16281932
Okay this works! One more question: suppose there are many nodes like:

<MyRepeatingNode>
   <Subnode1/>
   <OptionalNode1>SomeValue</OptionalNode1>
   ...
</MyRepeatingNode>
<MyRepeatingNode>
   <Subnode1/>
   <OptionalNode1>SomeValue</OptionalNode1>
   ...
</MyRepeatingNode>
<MyRepeatingNode>
   <Subnode1/>
   <OptionalNode1>SomeValue</OptionalNode1>
   ...
</MyRepeatingNode>

What if <OptionalNode1/> appears only in some of the <MyRepeatingNode>s and not others -- how do I ensure that, if <OptionalNode1> is missing, then <xsl:value-of select="ns1:MyRepeatingNode/OptionalNode1"> does not appear in the final output (that is, conform to the minOccurs="0" in the XSD of the output XML...)? I don't want extraneous elements in the final output when it is unnecessary to include them...
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16281964
Something like:

   <xsl:for-each select="MyRepeatingNode[OptionalNode1]">

   </xsl:for-each>

This would only process MyRepeatingNode nodes that have a child OptionalNode1.
0
 

Author Comment

by:NGInterface
ID: 16282218
Actually, I want to process all MyRepeatingNodes, but for some of the MyRepeatingNOdes, I want to suppress the output pf <OptionalNode1> when it doesn't exist within the parent MyRepeatingNode...
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 600 total points
ID: 16282253
Ok:

   <xsl:for-each select="MyRepeatingNode">
       <xsl:if test="OptionalNode1">
           <xsl:value-of select="OptionalNode1" />
       </xsl:if>
   </xsl:for-each>
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

850 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