By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,144 Members | 1,286 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,144 IT Pros & Developers. It's quick & easy.

Value into Optional Attribute?

P: n/a
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 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a


Re********@aol.com 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/
Jun 15 '06 #2

P: n/a
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 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.