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

September 6th, 2011, 02:12 AM
|
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
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>=</mo>
<anyelement>=</anyelement>
Required output
Code:
<mo font="XYZ" fontsize="10.2">=</mo>
<anyelement font="XYZ" fontsize="10.2">=</anyelement>
Any help would be great full
__________________
Thanks,
Rocxy.
|
|

September 6th, 2011, 04:17 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
is just a long-winded way of writing "=": no sane XML application will treat the content
differently from
, 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
|
|

September 6th, 2011, 04:57 AM
|
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
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.
|
|

September 6th, 2011, 06:05 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

September 6th, 2011, 06:27 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

September 8th, 2011, 02:07 AM
|
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
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="@*[. = '=']">
<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>
</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>=</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.
|
|

September 8th, 2011, 02:37 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
@* matches against any attribute. Try changing that to just * instead to match elements.
|
|
The Following User Says Thank You to samjudson For This Useful Post:
|
ROCXY (September 8th, 2011)
|
|

September 8th, 2011, 02:48 AM
|
|
Authorized User
|
|
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
|
|
Hi Sam,
Thanks - it works fine.
Once again, thanks for the swift response.
__________________
Thanks,
Rocxy.
|
|
 |