Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 6th, 2011, 02:12 AM
Authorized User
Points: 345, Level: 6
Points: 345, Level: 6 Points: 345, Level: 6 Points: 345, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2005
Location: , , .
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default Identify element based on the element data/content

Hi All,

Below is the entity value of 'equal to' symbol. The same symbol can appear in any of the elements/nodes

Objective:
Identify a node based on the data value '=' and add the node with attributes say @font and @fontsize.


Input can be :
Code:
<mo>&#x3d;</mo>
 
<anyelement>&#x3d;</anyelement>

Required output

Code:
<mo font="XYZ" fontsize="10.2">&#x3d;</mo>
 
<anyelement font="XYZ" fontsize="10.2">&#x3d;</anyelement>
Any help would be great full
__________________
Thanks,
Rocxy.
Reply With Quote
  #2 (permalink)  
Old September 6th, 2011, 04:17 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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Code:
&#x3d;
is just a long-winded way of writing "=": no sane XML application will treat the content
Code:
<x>&#x3d;</x>
differently from
Code:
<x>=</x>
, or care about which representation is used in the output. Is that what your question is about?

Apart from that, it's trivial:

Code:
<xsl:template match="@*[. = '=']">
  <xsl:copy>
    <xsl:attribute name="font">XYZ</xsl:attribute>
    <xsl:attribute name="font-size">12.6</xsl:attribute>
    <xsl:text>=</xsl:text>
  </xsl:copy>
</xsl:template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #3 (permalink)  
Old September 6th, 2011, 04:57 AM
Authorized User
Points: 345, Level: 6
Points: 345, Level: 6 Points: 345, Level: 6 Points: 345, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2005
Location: , , .
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default How can I apply for 'non-keyboard' characters?

Dear Kay,

Thanks for the code and adivse.

Actually the question was: How do I add @attributes and its value to an unknown element; where as the unknown element can be found using a specific content that appears inside the element, in the above case it was "=" symbol inside a element 'mo'.

I have one more question - how can I treat non-keyboard characters.

Any help would be great full
__________________
Thanks,
Rocxy.
Reply With Quote
  #4 (permalink)  
Old September 6th, 2011, 06:05 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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

XSLT doesn't know or care what characters are on your keyboard. I've just acquired a MAC which I'm surprised to discover has no "#" on the keyboard. There's a question about how to enter a "#" using such a keyboard (answer: ALT+£) but that's nothing to do with XSLT.

When you're editing XML, then as a convenience instead of finding special key combinations for non-keyboard characters, or using a screen-based soft keyboard, XML allows you to use a numeric character reference in the actual data. But there's almost no difference in practice from entering the real character.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #5 (permalink)  
Old September 6th, 2011, 06:27 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: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

OK, so you want to include the namespace at the article level if and only if it is use somewhere (at any depth) within the article? It's not really necessary to be so fussy about where the namespace declarations go, but it you are you can do it.

Code:
<xsl:template match="article">
  <article>
    <xsl:if test=".//@*[starts-with(name(), 'xlink:')]">
      <xsl:copy-of select="namespace::xlink"/>
    </xsl:if>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #6 (permalink)  
Old September 8th, 2011, 02:07 AM
Authorized User
Points: 345, Level: 6
Points: 345, Level: 6 Points: 345, Level: 6 Points: 345, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2005
Location: , , .
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Dear Kay

The following code is not working

Code:
<xsl:template match="@*[. = '=']">
  <xsl:copy>
    <xsl:attribute name="font">XYZ</xsl:attribute>
    <xsl:attribute name="font-size">12.6</xsl:attribute>
    <xsl:text>=</xsl:text>
  </xsl:copy>
</xsl:template>
Here is my complete XSLT.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    xpath-default-namespace="http://www.w3.org/1998/Math/MathML"
    exclude-result-prefixes="xs xd"
    version="2.0">
   
    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"></xsl:copy-of>
            <xsl:apply-templates></xsl:apply-templates>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="mspace"/>
    <xsl:template match="annotation"/>
    
    <xsl:template match="@*[. = '&#x3d;']">
        <xsl:copy>
            <xsl:attribute name="font">XYZ</xsl:attribute>
            <xsl:attribute name="font-size">12.6</xsl:attribute>
            <xsl:text>&#x3d;</xsl:text>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>
Here below is the input
Code:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="http://www.w3.org/Math/XSL/mathml.xsl"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN"  "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><title>Basic XHTML+MathML Document</title></head>
<body>
<math xmlns="http://www.w3.org/1998/Math/MathML">
    <semantics>
      <mstyle >
        <mo>[</mo>
        <mo>&#x3d;</mo>
        <mo>]</mo>
      </mstyle>
    </semantics>
  </math>
</body>
</html>
Here below is the output
Code:
<?xml version="1.0" encoding="UTF-8"?><html xmlns="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:pref="http://www.w3.org/2002/Math/preference" xml:lang="en">
   <head profile="">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>Basic XHTML+MathML Document</title>
   </head>
   
   <body>
      
      
      <math xmlns="http://www.w3.org/1998/Math/MathML" overflow="scroll">
         
         <semantics definitionURL="" encoding="">
            
            <mstyle>
               
               <mo>[</mo>
               
               <mo>=</mo>
               
               <mo>]</mo>
               
            </mstyle>
            
         </semantics>
         
      </math>
      
   </body>
   
</html>
XSLT Processor: Saxon-PE 9.2.1.2

Any help would be greatly appreciated.
__________________
Thanks,
Rocxy.
Reply With Quote
  #7 (permalink)  
Old September 8th, 2011, 02:37 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

@* matches against any attribute. Try changing that to just * instead to match elements.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
The Following User Says Thank You to samjudson For This Useful Post:
ROCXY (September 8th, 2011)
  #8 (permalink)  
Old September 8th, 2011, 02:48 AM
Authorized User
Points: 345, Level: 6
Points: 345, Level: 6 Points: 345, Level: 6 Points: 345, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2005
Location: , , .
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi Sam,

Thanks - it works fine.

Once again, thanks for the swift response.
__________________
Thanks,
Rocxy.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to MOVE sub-element to preceding-sibling’s last sub-element lkd XSLT 6 March 15th, 2011 11:15 AM
Chapter 9: Element[user control] is not a known element Arya BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 December 20th, 2009 07:31 AM
Defining restrictions based on element/attribute values aldwinenriquez XML 3 May 13th, 2009 03:55 AM
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
Match element based on attribute of child? transom XSLT 1 September 11th, 2005 03:26 PM



All times are GMT -4. The time now is 12:56 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.