Matching a simple tag in XSLT

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!
NGInterfaceAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
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">
NGInterfaceAuthor Commented:
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)...
Gertone (Geert Bormans)Information ArchitectCommented:
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
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Gertone (Geert Bormans)Information ArchitectCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NGInterfaceAuthor Commented:
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...
Carl TawnSystems and Integration DeveloperCommented:
Something like:

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

   </xsl:for-each>

This would only process MyRepeatingNode nodes that have a child OptionalNode1.
NGInterfaceAuthor Commented:
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...
Carl TawnSystems and Integration DeveloperCommented:
Ok:

   <xsl:for-each select="MyRepeatingNode">
       <xsl:if test="OptionalNode1">
           <xsl:value-of select="OptionalNode1" />
       </xsl:if>
   </xsl:for-each>
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.