Value into Optional Attribute?

R

ReGenesis0

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
 
M

Martin Honnen

<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>
 
R

ReGenesis0

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

....

*smacks forehead*

thank you. ;-)

-Derik
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,002
Messages
2,570,260
Members
46,858
Latest member
FlorrieTuf

Latest Threads

Top