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 September 5th, 2011, 10:56 AM
Authorized User
 
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default Remove element from a XML file

Hi All,

I am trying to do few addition and correction to MathML elements.

Input:
<math xmlns="http://www.w3.org/1998/Math/MathML">
<semantics>
<mi>P</mi>
<mspace width="0"/>
<mo>&#x3d;</mo>
<annotation encoding="MathMagic">MMF....... more data</annotation>
</semantics>
</math>


Required output

<math xmlns="http://www.w3.org/1998/Math/MathML">
<semantics>
<mi>P</mi>
<mo>&#x3d;</mo>
</semantics>
</math>



XSL USED:
<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"/>


1) All the above describe the removal of an element - 2) following detail describes the request of additions of attributes

input
<mo>&#x3d;</mo>
<mi>&#x3d;</mi>
<mtext>&#x3d;</mtext>



Required output
<mo font="XYZ" fontsize="10.2">&#x3d;</mo>

Any help would be great full.
__________________
Thanks,
Rocxy.
 
Old September 5th, 2011, 12:16 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

In match pattern you don't need to use "//" at the start of a pattern so doing
Code:
<xsl:template match="mspace"/>
<xsl:template match="annotation"/>
suffices. However both your match or my simplification will only work if you use XSLT 2.0 and put xpath-default-namespace="http://www.w3.org/1998/Math/MathML" on the xsl: stylesheet element.

As for adding attributes, use e.g.
Code:
<xsl:template match="mo">
  <xsl:copy>
    <xsl:attribute name="font">XYZ</xsl:attribute>
    <xsl:attribute name="fontsize">10.2</xsl:atttribute>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>
__________________
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:
ROCXY (September 6th, 2011)
 
Old September 6th, 2011, 01:22 AM
Authorized User
 
Join Date: Dec 2005
Posts: 71
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi Martin

Thanks you so much.

Code:
    <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="mo">
        <xsl:copy>
            <xsl:attribute name="font">XYZ</xsl:attribute>
            <xsl:attribute name="fontsize">10.2</xsl:attribute>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>
__________________
Thanks,
Rocxy.





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT output to HTML - xml element text should look exactly as laid out in xml element DjGogga XSLT 4 July 21st, 2011 05:39 PM
Searching a XML file for a element ehsansad XSLT 2 November 12th, 2010 08:14 AM
Getting value of an element in an XML file semilemon C# 2005 5 April 13th, 2007 07:54 AM
Adding an Element with Attribute to XML file xergic Classic ASP XML 0 November 20th, 2004 08:26 AM
How to insert a new element in XML file? kmriyad C# 1 September 20th, 2004 09:18 AM





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