XSL: XPath copying all nodes that are not in a namespace

Discussion in 'XML' started by Tjerk Wolterink, Oct 21, 2004.

  1. Hello i have xml code like this:

    <page:page xmlns:page="namespacefor page">
    <page:section>
    <page:header>
    <b>Hello</b>There
    </page:header>
    <page:content>
    --- HTML CODE like:
    <i>Y</i>es i like bla bal <center>bla</center><img> alblalba
    </page:content>
    </page:section>
    </page:page>


    Now i want to convert this page:page xml page to a xhtml page,
    but i want all the xhtml tags like <b> to stay intact, how can i select
    these tags to becopied in the xsl.
    My xsl looks now like this:


    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:page="http://www.wolterinkwebdesign.com/xml/page"
    xmlns:menu="http://www.wolterinkwebdesign.com/xml/menu">

    [cut]

    <!--
    ! Matches a text section.
    ! Converts the page block to a html styled section of text
    !-->
    <xsl:template match="page:section">
    <h1><xsl:value-of select="page:header"/></h1>
    <div>
    <xsl:apply-templates/>
    </div>
    </xsl:template>

    <!--
    ! Matches the content element in a section element
    !-->
    <xsl:template match="page:content">
    <xsl:apply-templates/>
    </xsl:template>

    <!--
    ! Matches all xhtml tags and copies them
    !-->
    <xsl:template match=".[namespace-uri('')!=page]">
    <xsl:copy-of select="."/>
    </xsl:template>

    [cut]
    </xsl:stylesheet>



    Well i tried to copy all elemetns who are not in the page namespace but
    this xpath expression: .[namespace-uri('')!=page] does not work.

    How should i copy all xhtml tags??

    The output should be like this:
    <html>
    <h1>
    <b>Hello</b>There
    </h1>
    <div><p>

    --- HTML CODE like:
    <i>Y</i>es i like bla bal <center>bla</center><img> alblalba
    </p></div>
    </html>


    Pleaz help me
     
    Tjerk Wolterink, Oct 21, 2004
    #1
    1. Advertising

  2. Tjerk Wolterink

    Marrow Guest

    Re: XPath copying all nodes that are not in a namespace

    Hi,

    First off, namespaces are matched by their URI rather than their prefix - so
    even if you use the 'page' prefix in both XML and XSL they won't match
    unless the namespace URIs are also the same, so...
    either...
    <page:page xmlns:page="namespacefor page">
    needs to be...
    <page:page xmlns:page="http://www.wolterinkwebdesign.com/xml/page">

    or...
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:page="http://www.wolterinkwebdesign.com/xml/page"
    xmlns:menu="http://www.wolterinkwebdesign.com/xml/menu">
    needs to be...
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:page="namespacefor page"
    xmlns:menu="http://www.wolterinkwebdesign.com/xml/menu">


    and to the main issue, your template for catching elements not bound to the
    'page' namespace probably needs to look something like...

    <!--
    ! Matches all xhtml tags and copies them
    !-->
    <xsl:template match="*[namespace-uri(.) != namespace::page]">
    <xsl:copy-of select="."/>
    </xsl:template>


    HTH
    Marrow
    http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
    http://www.topxml.com/Xselerator



    "Tjerk Wolterink" <> wrote in message
    news:4177d0ff$0$45946$...
    > Hello i have xml code like this:
    >
    > <page:page xmlns:page="namespacefor page">
    > <page:section>
    > <page:header>
    > <b>Hello</b>There
    > </page:header>
    > <page:content>
    > --- HTML CODE like:
    > <i>Y</i>es i like bla bal <center>bla</center><img> alblalba
    > </page:content>
    > </page:section>
    > </page:page>
    >
    >
    > Now i want to convert this page:page xml page to a xhtml page,
    > but i want all the xhtml tags like <b> to stay intact, how can i select
    > these tags to becopied in the xsl.
    > My xsl looks now like this:
    >
    >
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > xmlns:page="http://www.wolterinkwebdesign.com/xml/page"
    > xmlns:menu="http://www.wolterinkwebdesign.com/xml/menu">
    >
    > [cut]
    >
    > <!--
    > ! Matches a text section.
    > ! Converts the page block to a html styled section of text
    > !-->
    > <xsl:template match="page:section">
    > <h1><xsl:value-of select="page:header"/></h1>
    > <div>
    > <xsl:apply-templates/>
    > </div>
    > </xsl:template>
    >
    > <!--
    > ! Matches the content element in a section element
    > !-->
    > <xsl:template match="page:content">
    > <xsl:apply-templates/>
    > </xsl:template>
    >
    > <!--
    > ! Matches all xhtml tags and copies them
    > !-->
    > <xsl:template match=".[namespace-uri('')!=page]">
    > <xsl:copy-of select="."/>
    > </xsl:template>
    >
    > [cut]
    > </xsl:stylesheet>
    >
    >
    >
    > Well i tried to copy all elemetns who are not in the page namespace but
    > this xpath expression: .[namespace-uri('')!=page] does not work.
    >
    > How should i copy all xhtml tags??
    >
    > The output should be like this:
    > <html>
    > <h1>
    > <b>Hello</b>There
    > </h1>
    > <div><p>
    >
    > --- HTML CODE like:
    > <i>Y</i>es i like bla bal <center>bla</center><img> alblalba
    > </p></div>
    > </html>
    >
    >
    > Pleaz help me
     
    Marrow, Oct 21, 2004
    #2
    1. Advertising

  3. Re: XPath copying all nodes that are not in a namespace

    Marrow Thanks,

    i know namespaces are bound to the URI, i posted it wrong,
    the namespace is use is: http://www.wolterinkwebdesign.com/xml/page

    But just a question because i'm curiuous:
    The uri does not have to exists does, it?
    URI's are just used for namespaces to have an unique identifier.
    Am i right?
    Or do developers often place their dtd, or schema's in the uri path??

    Marrow wrote:
    > Hi,
    >
    > First off, namespaces are matched by their URI rather than their prefix - so
    > even if you use the 'page' prefix in both XML and XSL they won't match
    > unless the namespace URIs are also the same, so...
    > either...
    > <page:page xmlns:page="namespacefor page">
    > needs to be...
    > <page:page xmlns:page="http://www.wolterinkwebdesign.com/xml/page">
    >
    > or...
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > xmlns:page="http://www.wolterinkwebdesign.com/xml/page"
    > xmlns:menu="http://www.wolterinkwebdesign.com/xml/menu">
    > needs to be...
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > xmlns:page="namespacefor page"
    > xmlns:menu="http://www.wolterinkwebdesign.com/xml/menu">
    >
    >
    > and to the main issue, your template for catching elements not bound to the
    > 'page' namespace probably needs to look something like...
    >
    > <!--
    > ! Matches all xhtml tags and copies them
    > !-->
    > <xsl:template match="*[namespace-uri(.) != namespace::page]">
    > <xsl:copy-of select="."/>
    > </xsl:template>
    >
    >
    > HTH
    > Marrow
    > http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
    > http://www.topxml.com/Xselerator
    >
    >
    >
    > "Tjerk Wolterink" <> wrote in message
    > news:4177d0ff$0$45946$...
    >
    >>Hello i have xml code like this:
    >>
    >><page:page xmlns:page="namespacefor page">
    >><page:section>
    >><page:header>
    >><b>Hello</b>There
    >></page:header>
    >><page:content>
    >>--- HTML CODE like:
    >><i>Y</i>es i like bla bal <center>bla</center><img> alblalba
    >></page:content>
    >></page:section>
    >></page:page>
    >>
    >>
    >>Now i want to convert this page:page xml page to a xhtml page,
    >>but i want all the xhtml tags like <b> to stay intact, how can i select
    >>these tags to becopied in the xsl.
    >>My xsl looks now like this:
    >>
    >>
    >><xsl:stylesheet version="1.0"
    >> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >>xmlns:page="http://www.wolterinkwebdesign.com/xml/page"
    >>xmlns:menu="http://www.wolterinkwebdesign.com/xml/menu">
    >>
    >>[cut]
    >>
    >><!--
    >> ! Matches a text section.
    >> ! Converts the page block to a html styled section of text
    >> !-->
    >><xsl:template match="page:section">
    >><h1><xsl:value-of select="page:header"/></h1>
    >><div>
    >><xsl:apply-templates/>
    >></div>
    >></xsl:template>
    >>
    >><!--
    >> ! Matches the content element in a section element
    >> !-->
    >><xsl:template match="page:content">
    >><xsl:apply-templates/>
    >></xsl:template>
    >>
    >><!--
    >> ! Matches all xhtml tags and copies them
    >> !-->
    >><xsl:template match=".[namespace-uri('')!=page]">
    >><xsl:copy-of select="."/>
    >></xsl:template>
    >>
    >>[cut]
    >></xsl:stylesheet>
    >>
    >>
    >>
    >>Well i tried to copy all elemetns who are not in the page namespace but
    >>this xpath expression: .[namespace-uri('')!=page] does not work.
    >>
    >>How should i copy all xhtml tags??
    >>
    >>The output should be like this:
    >><html>
    >><h1>
    >><b>Hello</b>There
    >></h1>
    >><div><p>
    >>
    >>--- HTML CODE like:
    >><i>Y</i>es i like bla bal <center>bla</center><img> alblalba
    >></p></div>
    >></html>
    >>
    >>
    >>Pleaz help me

    >
    >
    >
     
    Tjerk Wolterink, Oct 21, 2004
    #3
  4. Tjerk Wolterink

    Marrow Guest

    Re: XPath copying all nodes that are not in a namespace

    Hi,

    > But just a question because i'm curiuous:
    > The uri does not have to exists does, it?
    > URI's are just used for namespaces to have an unique identifier.
    > Am i right?


    Yes, you are right - URI stands for unique resource identifier. Nothing has
    to be there at all. People use URLs as URIs generally because this gives
    some garauntee of uniqueness - and they tend to choose URIs based on their
    organisations URL because they have some control over it.
    (see also: http://www.w3.org/TR/REC-xml-names/#ns-decl).

    > Or do developers often place their dtd, or schema's in the uri path??


    Some do and some don't. So the general rule is - don't rely on the URI to
    be a URL at which something will be found. The mechinisms for pointing to
    DTD/Schemas is entirely different.

    Cheers
    Marrow

    "Tjerk Wolterink" <> wrote in message
    news:41781478$0$61769$...
    > Marrow Thanks,
    >
    > i know namespaces are bound to the URI, i posted it wrong,
    > the namespace is use is: http://www.wolterinkwebdesign.com/xml/page
    >
    > But just a question because i'm curiuous:
    > The uri does not have to exists does, it?
    > URI's are just used for namespaces to have an unique identifier.
    > Am i right?
    > Or do developers often place their dtd, or schema's in the uri path??
    >
    > Marrow wrote:
    > > Hi,
    > >
    > > First off, namespaces are matched by their URI rather than their

    prefix - so
    > > even if you use the 'page' prefix in both XML and XSL they won't match
    > > unless the namespace URIs are also the same, so...
    > > either...
    > > <page:page xmlns:page="namespacefor page">
    > > needs to be...
    > > <page:page xmlns:page="http://www.wolterinkwebdesign.com/xml/page">
    > >
    > > or...
    > > <xsl:stylesheet version="1.0"
    > > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > > xmlns:page="http://www.wolterinkwebdesign.com/xml/page"
    > > xmlns:menu="http://www.wolterinkwebdesign.com/xml/menu">
    > > needs to be...
    > > <xsl:stylesheet version="1.0"
    > > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > > xmlns:page="namespacefor page"
    > > xmlns:menu="http://www.wolterinkwebdesign.com/xml/menu">
    > >
    > >
    > > and to the main issue, your template for catching elements not bound to

    the
    > > 'page' namespace probably needs to look something like...
    > >
    > > <!--
    > > ! Matches all xhtml tags and copies them
    > > !-->
    > > <xsl:template match="*[namespace-uri(.) != namespace::page]">
    > > <xsl:copy-of select="."/>
    > > </xsl:template>
    > >
    > >
    > > HTH
    > > Marrow
    > > http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
    > > http://www.topxml.com/Xselerator
    > >
    > >
    > >
    > > "Tjerk Wolterink" <> wrote in message
    > > news:4177d0ff$0$45946$...
    > >
    > >>Hello i have xml code like this:
    > >>
    > >><page:page xmlns:page="namespacefor page">
    > >><page:section>
    > >><page:header>
    > >><b>Hello</b>There
    > >></page:header>
    > >><page:content>
    > >>--- HTML CODE like:
    > >><i>Y</i>es i like bla bal <center>bla</center><img> alblalba
    > >></page:content>
    > >></page:section>
    > >></page:page>
    > >>
    > >>
    > >>Now i want to convert this page:page xml page to a xhtml page,
    > >>but i want all the xhtml tags like <b> to stay intact, how can i select
    > >>these tags to becopied in the xsl.
    > >>My xsl looks now like this:
    > >>
    > >>
    > >><xsl:stylesheet version="1.0"
    > >> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    > >>xmlns:page="http://www.wolterinkwebdesign.com/xml/page"
    > >>xmlns:menu="http://www.wolterinkwebdesign.com/xml/menu">
    > >>
    > >>[cut]
    > >>
    > >><!--
    > >> ! Matches a text section.
    > >> ! Converts the page block to a html styled section of text
    > >> !-->
    > >><xsl:template match="page:section">
    > >><h1><xsl:value-of select="page:header"/></h1>
    > >><div>
    > >><xsl:apply-templates/>
    > >></div>
    > >></xsl:template>
    > >>
    > >><!--
    > >> ! Matches the content element in a section element
    > >> !-->
    > >><xsl:template match="page:content">
    > >><xsl:apply-templates/>
    > >></xsl:template>
    > >>
    > >><!--
    > >> ! Matches all xhtml tags and copies them
    > >> !-->
    > >><xsl:template match=".[namespace-uri('')!=page]">
    > >><xsl:copy-of select="."/>
    > >></xsl:template>
    > >>
    > >>[cut]
    > >></xsl:stylesheet>
    > >>
    > >>
    > >>
    > >>Well i tried to copy all elemetns who are not in the page namespace but
    > >>this xpath expression: .[namespace-uri('')!=page] does not work.
    > >>
    > >>How should i copy all xhtml tags??
    > >>
    > >>The output should be like this:
    > >><html>
    > >><h1>
    > >><b>Hello</b>There
    > >></h1>
    > >><div><p>
    > >>
    > >>--- HTML CODE like:
    > >><i>Y</i>es i like bla bal <center>bla</center><img> alblalba
    > >></p></div>
    > >></html>
    > >>
    > >>
    > >>Pleaz help me

    > >
    > >
    > >
     
    Marrow, Oct 22, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hannes Heckner
    Replies:
    1
    Views:
    771
    Martin Honnen
    Oct 13, 2003
  2. Replies:
    1
    Views:
    3,613
    A. Bolmarcich
    May 27, 2005
  3. Al Hatch
    Replies:
    3
    Views:
    977
    Johannes Koch
    Jun 5, 2006
  4. Wizfrog
    Replies:
    5
    Views:
    2,292
    Joe Kesselman
    Jul 14, 2007
  5. eggie5
    Replies:
    1
    Views:
    531
    Joseph Kesselman
    Oct 2, 2007
Loading...

Share This Page