Using one template to process different nodes?

Discussion in 'XML' started by afc46220@yahoo.com, Dec 19, 2005.

  1. Guest

    Hello,

    I am a novice with XSL, and can't quite figure out how to come up with
    a solution for the following problem. Let's assume we have the
    following set of XML nodes:

    <DataBlocks>
    <AlphaBlock>
    <AlphaBlock.Name>Alpha<AlphaBlock.Name/>
    <AlphaBlock.Value>1<Alpha.Block.Value/>
    <AlphaBlock/>
    <BetaBlock>
    <BetaBlock.Name>Beta</BetaBlock.Name/>
    <BetaBlock.Value>2<BetaBlock.Block.Value/>
    <BetaBlock/>
    <DataBlocks/>

    Alpha and Beta blocks contain information that can be processed in
    identical way, therefore I intend to utilize one template, something
    like:

    <xsl:template match="DataBlocks//AlphaBlock | DataBlocks//BetaBlock">
    ....
    <xsl:template/>

    Within that template, I want to create a variable, which would hold the
    name of the node:

    <xsl:variable name="BlockType">
    <xsl:choose>
    <xsl:when test = "contains(current(),
    'BetaBlock')>BetaBlock</xsl:when>
    <xsl:eek:therwise>AlphaBlock</xsl:eek:therwise>
    </xsl:choose>
    </xsl:variable>

    My goal is then to use that variable to access both AlphaBlock.Value
    and BetaBlock.Value nodes in something like:

    Value is <xsl:value-of select="concat($BlockType,'.Value')"/>, but
    certainly it does not work.

    I would appreciate your help.

    Thanks.

    Alex.
     
    , Dec 19, 2005
    #1
    1. Advertising

  2. In article <>,
    <> wrote:

    >Alpha and Beta blocks contain information that can be processed in
    >identical way, therefore I intend to utilize one template, something
    >like:
    >
    ><xsl:template match="DataBlocks//AlphaBlock | DataBlocks//BetaBlock">
    >...
    ><xsl:template/>


    Yes, that's reasonable.

    >Within that template, I want to create a variable, which would hold the
    >name of the node:
    >
    ><xsl:variable name="BlockType">
    > <xsl:choose>
    > <xsl:when test = "contains(current(),
    >'BetaBlock')>BetaBlock</xsl:when>
    > <xsl:eek:therwise>AlphaBlock</xsl:eek:therwise>
    > </xsl:choose>
    ></xsl:variable>


    You are testing whether the the text content of the element contains the
    string "BetaBlock" which doesn't correspond to what you said you wanted.
    The simplest way to get the name of the element into the variable is
    to do something like

    <xsl:variable name="BlockType" select="name()"/>

    >My goal is then to use that variable to access both AlphaBlock.Value
    >and BetaBlock.Value nodes in something like:
    >
    >Value is <xsl:value-of select="concat($BlockType,'.Value')"/>, but
    >certainly it does not work.


    You can't dynamically create a pattern to match. But you can do
    something like this:

    <xsl:value-of select="*[name() = concat($BlockType, '.Value')"/>

    That you are having to do this suggests that the design of your
    document is not optimal. Why not simply have

    <AlphaBlock>
    <Name>Alpha</Name>
    <Value>1</Value>
    <AlphaBlock/>

    Or even <Block type="Alpha">

    -- Richard
     
    Richard Tobin, Dec 19, 2005
    #2
    1. Advertising

  3. Guest

    Richard,

    Thanks. I will try your suggestion tomorrow.

    As for the design of the original XML document, I can't argue - it's
    far from being optimized, but it's a legacy document, and its structure
    can't be changed at this point.

    Alex.
     
    , Dec 20, 2005
    #3
  4. In article <>,
    <> wrote:
    >As for the design of the original XML document, I can't argue - it's
    >far from being optimized, but it's a legacy document, and its structure
    >can't be changed at this point.


    If you're going to have to do a lot of work on these documents, you
    might consider writing a stylesheet to first convert it into a more
    tractable form, rather than constantly dealing with the original.

    -- Richard
     
    Richard Tobin, Dec 20, 2005
    #4
  5. JAPISoft Guest

    <xsl:variable name="BlockType" select="local-name(.)"/> is enough

    <xsl:value-of select="*[local-name(.)=concat( $BlockType, '.Name' )]"/>

    <xsl:value-of select="*[local-name(.)=concat( $BlockType, '.Value' )]"/>

    Best regards,

    A.Brillant
    EditiX - XML Editor and XSLT Debugger
    http://www.editix.com

    wrote:
    > Hello,
    >
    > I am a novice with XSL, and can't quite figure out how to come up with
    > a solution for the following problem. Let's assume we have the
    > following set of XML nodes:
    >
    > <DataBlocks>
    > <AlphaBlock>
    > <AlphaBlock.Name>Alpha<AlphaBlock.Name/>
    > <AlphaBlock.Value>1<Alpha.Block.Value/>
    > <AlphaBlock/>
    > <BetaBlock>
    > <BetaBlock.Name>Beta</BetaBlock.Name/>
    > <BetaBlock.Value>2<BetaBlock.Block.Value/>
    > <BetaBlock/>
    > <DataBlocks/>
    >
    > Alpha and Beta blocks contain information that can be processed in
    > identical way, therefore I intend to utilize one template, something
    > like:
    >
    > <xsl:template match="DataBlocks//AlphaBlock | DataBlocks//BetaBlock">
    > ...
    > <xsl:template/>
    >
    > Within that template, I want to create a variable, which would hold the
    > name of the node:
    >
    > <xsl:variable name="BlockType">
    > <xsl:choose>
    > <xsl:when test = "contains(current(),
    > 'BetaBlock')>BetaBlock</xsl:when>
    > <xsl:eek:therwise>AlphaBlock</xsl:eek:therwise>
    > </xsl:choose>
    > </xsl:variable>
    >
    > My goal is then to use that variable to access both AlphaBlock.Value
    > and BetaBlock.Value nodes in something like:
    >
    > Value is <xsl:value-of select="concat($BlockType,'.Value')"/>, but
    > certainly it does not work.
    >
    > I would appreciate your help.
    >
    > Thanks.
    >
    > Alex.
    >
     
    JAPISoft, Dec 21, 2005
    #5
    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. asd
    Replies:
    3
    Views:
    440
    Arnaud Berger
    May 23, 2005
  2. gavnosis
    Replies:
    0
    Views:
    524
    gavnosis
    Aug 2, 2003
  3. Timo Nentwig

    selecting nodes between other nodes

    Timo Nentwig, Jun 16, 2004, in forum: XML
    Replies:
    1
    Views:
    409
    Patrick TJ McPhee
    Jun 17, 2004
  4. Replies:
    1
    Views:
    578
    Soren Kuula
    Aug 24, 2006
  5. Replies:
    5
    Views:
    556
    Thomas J. Gritzan
    Oct 6, 2006
Loading...

Share This Page