Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 12th, 2012, 04:04 AM
Registered User
 
Join Date: Oct 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default Transforming attribute value to element name

Hello all,

I am trying to pick up an attribute value and using that as an element name in my output, but I fail to see which kind of function I can use for that. The XML in my input document looks like this (only showing the interesting stuff):

Code:
<p>
   <pPr>
      <pStyle val="Heading1"/>
   </pPr>
   <r>
      <t>How to install the module</t>
   </r>
</p>
<p>
   <pPr>
      <pStyle val="Heading2"/>
   </pPr>
   <r>
      <t>Preparations</t>
   </r>
</p>
What I would need as output is:

Code:
<Heading1>How to install the module</Heading1>
<Heading2>Preparations</Heading2>
Maybe I am asking for the obvious - being an XSLT newbie - but I do not have the time to read through the 3 books on XSLT I have. If someone can point me to the type of functionality I should be looking for in Michael Kay's book that would be wonderful, too.

Thanks

Jang
 
Old October 12th, 2012, 05:00 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well with the help of xsl: element you can construct an element
Code:
<xsl:template match="p[pPr/pStyle/@val]">
  <xsl:element name="{pPr/pStyle/@val}">
    <xsl:value-of select="r/t"/>
  </xsl:element>
</xsl:template>
Note that your input snippet does not show any namespace declarations but if the real input has some then you need to adapt the code.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
4everJang (October 12th, 2012)
 
Old October 12th, 2012, 05:06 AM
Registered User
 
Join Date: Oct 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

So it seems I can use the XPath expressions almost anywhere in XSLT? I was thinking they would only be valid for a 'match' or 'select'.

I was expecting things to be more difficult than they really are. Thanks.
 
Old October 12th, 2012, 05:12 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you look at the syntax in http://www.w3.org/TR/xslt#section-Cr...th-xsl:element then you will see
Code:
<!-- Category: instruction -->
<xsl:element
  name = { qname }
  namespace = { uri-reference }
  use-attribute-sets = qnames>
  <!-- Content: template -->
</xsl:element>
The curly braces {} indicate that for the attribute you can use an attribute value template where you enclose an XPath expression in curly braces to compute the attribute value.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old October 18th, 2012, 10:07 AM
Registered User
 
Join Date: Oct 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

<xsl:template match="/">
<xsl:apply-templates select="//pPr"/>
</xsl:template>
<xsl:template match="pPr">
<xsl:element name="{pStyle/@val}">
<xsl:value-of select="../r/t"/>
</xsl:element>
</xsl:template>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Transforming attribute with several values moggi XSLT 4 August 21st, 2010 05:26 AM
How to add an attribute to each element bartnowa XSLT 3 November 8th, 2007 12:12 PM
value-of within the HTML element attribute BrendonMelville XSLT 1 March 14th, 2006 11:32 AM
Attribute for first element only mehdi62b XML 1 January 2nd, 2006 06:26 AM
How can I get the element node of an attribute cdias XSLT 2 March 15th, 2004 10:34 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.