copying all but one type of Nodes in XSLT

Discussion in 'XML' started by Wizfrog, Jul 14, 2007.

  1. Wizfrog

    Wizfrog Guest

    Hello,

    I'm unsing well-formed XML HTML tags as my formatted "Description"
    content in a XML Database, so I have stuff of the sort:

    <Description lang='en'><b>This is bold Text</b> and this is not<i>and
    this is italic text</i></Description>

    So far, nothing too dramatic, when I want to reproduce this as an HTML
    output, I used the <xsl:copy-of> command to copy the whole node over
    to my HTML output.

    Now here comes where I have an issue:

    I want to use a specific tag <spec name='specname'/> to insert
    "Specifications" related to the product WITHIN the formatted text...
    so it could be anywhere under <Description> or a <b> tag, or a <i>
    tag ...

    The idea is that products are built out of other products, and I want
    to be able to have the specification propagate through to the higher
    level products, so I only want to "reference" the spec name in the
    Description, and pull it out from the lower level product at the time
    of output.

    So, my problem is to "fill in" my <spec name='specname'/> tag with its
    content coming from somewhere else.
    The <xsl:copy-of> can no longer be used here, and it doesn't do the
    job, but then is there an easy way to "transfer" all of the XML/HTML
    and just replace the tag i want to replace?

    I obviously need to parse the whole thing, but tags can be anything,
    anywhere, (from the selection of formatting tags <b>, <i>, <u>, <br/>,
    <p>, <sub>, <sup> etc...)

    I'm not so proficient in XPath to figure out what would be the path I
    want to select to transfer all tags but my <spec> tag, and when
    bumping into one, replacing it with my data.

    any insight?

    Suggestion appreciated!

    Thanks
     
    Wizfrog, Jul 14, 2007
    #1
    1. Advertising

  2. Wizfrog wrote:

    > So, my problem is to "fill in" my <spec name='specname'/> tag with its
    > content coming from somewhere else.
    > The <xsl:copy-of> can no longer be used here, and it doesn't do the
    > job, but then is there an easy way to "transfer" all of the XML/HTML
    > and just replace the tag i want to replace?
    >
    > I obviously need to parse the whole thing, but tags can be anything,
    > anywhere, (from the selection of formatting tags <b>, <i>, <u>, <br/>,
    > <p>, <sub>, <sup> etc...)


    It is rather easy, start with the identity transformation template
    <xsl:template match="@* | node()">
    <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
    </xsl:template>
    then add other templates as needed, for instance
    <xsl:template match="spec[@name = 'specname']">
    <!-- do what is needed here for those spec elements -->
    </xsl:template>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jul 14, 2007
    #2
    1. Advertising

  3. Wizfrog

    Wizfrog Guest

    Re: copying all but one type of Nodes in XSLT

    On Jul 14, 4:32 am, Martin Honnen <> wrote:
    > Wizfrog wrote:
    > > So, my problem is to "fill in" my <spec name='specname'/> tag with its
    > > content coming from somewhere else.
    > > The <xsl:copy-of> can no longer be used here, and it doesn't do the
    > > job, but then is there an easy way to "transfer" all of the XML/HTML
    > > and just replace the tag i want to replace?

    >
    > > I obviously need to parse the whole thing, but tags can be anything,
    > > anywhere, (from the selection of formatting tags <b>, <i>, <u>, <br/>,
    > > <p>, <sub>, <sup> etc...)

    >
    > It is rather easy, start with the identity transformation template
    > <xsl:template match="@* | node()">
    > <xsl:copy>
    > <xsl:apply-templates select="@* | node()"/>
    > </xsl:copy>
    > </xsl:template>
    > then add other templates as needed, for instance
    > <xsl:template match="spec[@name = 'specname']">
    > <!-- do what is needed here for those spec elements -->
    > </xsl:template>
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/


    Hi,

    I tried this, but it always seem to use the @* | node() by default,
    and never gets to the match='spec[...]' template even when getting to
    a <spec> tag...

    any way to filter out the specific tag?
     
    Wizfrog, Jul 14, 2007
    #3
  4. Wizfrog

    Wizfrog Guest

    Re: copying all but one type of Nodes in XSLT

    I tried this:

    <xsl:template match="@* | node()">
    <xsl:choose>
    <xsl:when test="@name">
    print Spec here
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>

    and it works fine, but I am not sure why the following doesn't:

    <xsl:template match="@* | node()">
    <xsl:choose>
    <xsl:when test="spec">
    print Spec here
    </xsl:when>
    <xsl:eek:therwise>
    <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
    </xsl:eek:therwise>
    </xsl:choose>
    </xsl:template>

    ideally I need to process more than 1 attribute from the <spec> tag,
    so i'd like to trigger it on that node name, not on the attribute

    any idea?
     
    Wizfrog, Jul 14, 2007
    #4
  5. Re: copying all but one type of Nodes in XSLT

    Wizfrog wrote:
    > I tried this, but it always seem to use the @* | node() by default,
    > and never gets to the match='spec[...]' template even when getting to
    > a <spec> tag...


    In that case, the spec element isn't matching. Either the predicate is
    wrong or you've got namespaces involved. Think about why, or give us
    enough information to diagnose.


    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Jul 14, 2007
    #5
  6. Re: copying all but one type of Nodes in XSLT

    Wizfrog wrote:
    > <xsl:when test="@name">


    True when the current node has a name attribute, ie the <spec>.

    > <xsl:when test="spec">


    True when the current node has a spec element child, ie the <spec>'s
    parent.

    xsl:when is not the best way to do this -- use templates -- but if you
    must, you could try
    <xsl:when test="self::spec">


    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Jul 14, 2007
    #6
    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. loveNUNO
    Replies:
    2
    Views:
    969
    loveNUNO
    Nov 20, 2003
  2. Tjerk Wolterink
    Replies:
    3
    Views:
    479
    Marrow
    Oct 22, 2004
  3. R
    Replies:
    4
    Views:
    3,072
    Joris Gillis
    Mar 11, 2005
  4. Al Hatch
    Replies:
    3
    Views:
    1,019
    Johannes Koch
    Jun 5, 2006
  5. Eilsa@Office

    How to load all nodes in a tree but expand 2 level only?

    Eilsa@Office, Jul 27, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    129
    Eilsa@Office
    Jul 27, 2004
Loading...

Share This Page