p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Identify element based on the element data/content (http://p2p.wrox.com/showthread.php?t=84963)

ROCXY September 6th, 2011 02:12 AM

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

mhkay September 6th, 2011 04:17 AM

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>


ROCXY September 6th, 2011 04:57 AM

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

mhkay September 6th, 2011 06:05 AM

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.

mhkay September 6th, 2011 06:27 AM

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>


ROCXY September 8th, 2011 02:07 AM

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.

samjudson September 8th, 2011 02:37 AM

@* matches against any attribute. Try changing that to just * instead to match elements.

ROCXY September 8th, 2011 02:48 AM

Hi Sam,

Thanks - it works fine.

Once again, thanks for the swift response.


All times are GMT -4. The time now is 12:09 AM.

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