Value into Optional Attribute?

Discussion in 'XML' started by ReGenesis0@aol.com, Jun 15, 2006.

  1. Guest

    Hey all- I'm just astarting out, and this is what I'm running into--

    given XML

    <container>
    <subject>
    <name>John Smith</name>
    <age>22</age>
    </subject>
    <subject>
    <name>Mariko Suzuhara</name>
    </subject>
    </container>


    How do I transform this to-

    <test_group>
    <subject age="22">John Smith</subject>
    <subject>Mariko Suzuhara</subject>
    </test_group>

    Such that the Age attribute remains undeclared if it was not present in
    the original.
    I feel liek there should be an elegant and somple solution to this that
    does not involve xsl:choose (since these blocks are actually div's with
    more complex content that would be duplicated.)
    I've butted my head up against online help and several books, and have
    concluded (since I've run across no one asking the same question and
    beign shot down) that I'm simply asking the Wrong Question, and am
    probably missing something obvious. I don't think it stems from having
    a Pull mentality though...

    ....help?

    -Derik
     
    , Jun 15, 2006
    #1
    1. Advertising

  2. wrote:


    > <container>
    > <subject>
    > <name>John Smith</name>
    > <age>22</age>
    > </subject>
    > <subject>
    > <name>Mariko Suzuhara</name>
    > </subject>
    > </container>
    >
    >
    > How do I transform this to-
    >
    > <test_group>
    > <subject age="22">John Smith</subject>
    > <subject>Mariko Suzuhara</subject>
    > </test_group>



    <xsl:template match="subject">
    <xsl:copy />
    <xsl:apply-templates select="age" />
    <xsl:apply-templates select="name" />
    </xsl:copy>
    </xsl:template>

    <xsl:template match="age">
    <xsl:attribute name="age"><xsl:value-of select="." /></xsl:attribute>
    </xsl:template>

    <xsl:template match="name">
    <xsl:value-of select="." />
    </xsl:template>



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Jun 15, 2006
    #2
    1. Advertising

  3. Guest

    Martin Honnen wrote:

    > <xsl:template match="age">
    > <xsl:attribute name="age"><xsl:value-of select="." /></xsl:attribute>
    > </xsl:template>


    ....

    *smacks forehead*

    thank you. ;-)

    -Derik
     
    , Jun 15, 2006
    #3
    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. Gazza
    Replies:
    1
    Views:
    1,174
    Henry S. Thompson
    Dec 5, 2005
  2. Replies:
    2
    Views:
    1,097
    Henry S. Thompson
    Mar 6, 2006
  3. Replies:
    1
    Views:
    4,416
    Joe Kesselman
    Aug 2, 2006
  4. Replies:
    8
    Views:
    535
  5. Jeremy S
    Replies:
    6
    Views:
    707
    Gregory A. Beamer
    Jun 15, 2009
Loading...

Share This Page