Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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 February 13th, 2008, 04:45 AM
Authorized User
 
Join Date: Jul 2007
Location: New Delhi, Delhi, India.
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default String to attribute and vice versa

Hi all,

Is there any better/quickest way of converting string of tag to its its value and later while re-transforming back it to orginal converting those attribute values back to string. Let me be more clear with a dummy example. Below is what my xml looks like:

<details>
<code>XXX</code>
<issn type="print">0000-0000</issn>
<issn type="electronic">0000-0000</issn>
<det>Xvvvv TTTTT v 145- 149</det>
</details.

All I am trying to do is to convert all tag string to dummy attribute value like below.

<details>
<code dummy="XXX"/>
<issn type="print" dummy="0000-0000"/>
<issn type="electronic" dummy="0000-0000"/>
<det dummy="Xvvvv TTTTT v 145- 149">
</details.


Well converting string to attribute values and vice versa in XSLT is not an issue for me but I am trying to have smart way of doing this as there are hundreds of elements (at different levels) in my actual whose string needs to suppressed in view and needs to be reverted back again re-transforming with another XSLT.

My worry is if start suppressing all these elements at different level I will end up with a giant stylesheet. That's the reason I am looking for smart way of achieving this.

At this stage I think I need to do this trick with <xsl:call-template>, but unfortunately I have never used it earlier. Can somebody guide whether I am on right direction or not or help me with snippet of code.

Sorry for asking without trying it and thanks in advance for any help.

Pankaj















Pankaj
__________________
Pankaj
 
Old February 13th, 2008, 05:24 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Assuming you don't have any mixed content in your document, you can do the conversion using:

<xsl:template match="*[text()]">
  <xsl:copy>
  <xsl:copy-of select="@*"/>
  <xsl:attribute name="dummy">
    <xsl:value-of select="."/>
  </xsl:attribute>
  </xsl:copy>
</xsl:template>

<xsl:template match="*[not(text())]">
  <xsl:copy>
  <xsl:copy-of select="@*"/>
  <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 13th, 2008, 06:16 AM
Authorized User
 
Join Date: Jul 2007
Location: New Delhi, Delhi, India.
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well I do have the mixed content in my xml and also I want to perform this on certain elements and not on all.

Your code seems to be applying on each and every element of xml document.

Correct me if I am wrong.

Thanks.

Pankaj

Pankaj
 
Old February 13th, 2008, 06:25 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you only want to apply it to some elements then change "*[text()]" to match the element you want to perform it on.

And by 'mixed content' we mean something like this: <detail>Some text <i>goes here</i>.</detail>

<detail> is considered to have mixed content, i.e. some of its child elements are text() nodes, and some are elements (in this case it has two text nodes and the <i> element as its children).

/- Sam Judson : Wrox Technical Editor -/
 
Old February 13th, 2008, 06:30 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

My code was applying one rule for elements with text nodes, another for elements without (I should have added <xsl:strip-space elements="*"/> to avoid complications with whitespace text nodes). That should handle the example you've shown us.

If your rules are more complex than that then you will need to use more specific match patterns.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 13th, 2008, 06:42 AM
Authorized User
 
Join Date: Jul 2007
Location: New Delhi, Delhi, India.
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Sam and Michael. I think I need to match the element along with exact path.

Sam in case of mixed content case I think I need to use string() instead of text(). Will take work or there is any other content-markup sort of function. Content can be with entities too.

Thanks


Pankaj




Similar Threads
Thread Thread Starter Forum Replies Last Post
Query to change rows into columns and vice versa miamikk SQL Language 0 November 13th, 2007 06:41 PM
Numbers to words and vice versa? code_lover SQL Language 0 January 7th, 2007 04:50 PM
Word to XML and vice versa using C# asp.net veeruu ASP.NET 1.0 and 1.1 Professional 2 July 21st, 2006 07:41 PM
passing php variable to javascript and vice versa onmcv Beginning PHP 0 March 24th, 2005 01:00 PM
converting c/c++ programs into vb.net vice versa viper063 VB.NET 2002/2003 Basics 1 October 20th, 2004 05:26 PM





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