Librak
Librak

Reputation: 141

change all occurence of <I> tag text into italic

this is my xml

    <P style="TEXT-ALIGN: center; MARGIN: 0pt">
        <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 8.5pt; FONT-WEIGHT: bold">Form</SPAN>
            <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 7pt; FONT-WEIGHT: bold">Form</SPAN>
            <SPAN style="FONT-FAMILY: 'COMIC SANS MS'; FONT-SIZE: 6.7pt; FONT-WEIGHT: bold">Form</SPAN>
            <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE:12pt; FONT-WEIGHT: bold">10-Q</SPAN>
            <SPAN style="FONT-FAMILY: 'CURSIVE'; FONT-SIZE:14pt">Hisasdf @ adsfkej % adsfkj-Q</SPAN>
            <SPAN style="FONT-FAMILY: 'Edwardian Script ITC'; FONT-SIZE:18pt">Singhaniadf kurmad-Q</SPAN>
            <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE:36pt"><U>anurag arya fateh ali khan-Q</U></SPAN>
            <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE:12pt"><U>rakesh kumar sinfhania</U></SPAN>
      </P>

    <P style="MARGIN: 0pt">
        <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 18pt">of advisory fees, estimated legal fees, a $0.6</SPAN>
        <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 12pt">million<U> provision for estimated settlement costs associated with shareholder litigation and other 
transition related costs. These amounts are included in the Company?s </U>sults of operations for the three and nine months ended September</SPAN>
    <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 18pt">30, 2010,respectively.</SPAN></P>

<P style="TEXT-ALIGN: center; MARGIN: 0pt">
    <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 12pt; FONT-WEIGHT: bold"><I>Washington,</I></SPAN>
    <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 12pt; FONT-WEIGHT: bold">D.C. &nbsp;&nbsp;&nbsp;&nbsp;20549</SPAN>
    <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 12pt"> 
    </SPAN>
</P>

   <I>of advisory fees, estimated legal fees, a $0.6</I>

i want to change all the occurence of <I>into italic

i also use this line in the match statement match="span/p/i"

and many other's as suggested by google to change text in italic ,but its not working what to do

Upvotes: 1

Views: 186

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243469

First, note that the provided fragment is not wellformed XML. What I did to make it into a well-formed XML document was:

  1. Wrapped it into a single top html element.

  2. Replaced all occurencies of the &nbsp; entity with the character entity &#xA0; (alternatively a small DTD can be prepended, that defines the entity)

Solution:

This most simple transformation (override of the identity rule):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="I">
  <i>
   <xsl:apply-templates/>
  </i>
 </xsl:template>
</xsl:stylesheet>

when applied on any well-formed XML document (in this example we use the well-formed XML document produced from the provided fragment):

<html>
        <P style="TEXT-ALIGN: center; MARGIN: 0pt">
            <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 8.5pt; FONT-WEIGHT: bold">Form</SPAN>
                <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 7pt; FONT-WEIGHT: bold">Form</SPAN>
                <SPAN style="FONT-FAMILY: 'COMIC SANS MS'; FONT-SIZE: 6.7pt; FONT-WEIGHT: bold">Form</SPAN>
                <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE:12pt; FONT-WEIGHT: bold">10-Q</SPAN>
                <SPAN style="FONT-FAMILY: 'CURSIVE'; FONT-SIZE:14pt">Hisasdf @ adsfkej % adsfkj-Q</SPAN>
                <SPAN style="FONT-FAMILY: 'Edwardian Script ITC'; FONT-SIZE:18pt">Singhaniadf kurmad-Q</SPAN>
                <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE:36pt"><U>anurag arya fateh ali khan-Q</U></SPAN>
                <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE:12pt"><U>rakesh kumar sinfhania</U></SPAN>
          </P>

        <P style="MARGIN: 0pt">
            <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 18pt">of advisory fees, estimated legal fees, a $0.6</SPAN>
            <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 12pt">million<U> provision for estimated settlement costs associated with shareholder litigation and other
    transition related costs. These amounts are included in the Company?s </U>sults of operations for the three and nine months ended September</SPAN>
        <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 18pt">30, 2010,respectively.</SPAN></P>

    <P style="TEXT-ALIGN: center; MARGIN: 0pt">
        <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 12pt; FONT-WEIGHT: bold"><I>Washington,</I></SPAN>
        <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 12pt; FONT-WEIGHT: bold">D.C. &#xA0;&#xA0;&#xA0;&#xA0;20549</SPAN>
        <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 12pt">
        </SPAN>
    </P>

       <I>of advisory fees, estimated legal fees, a $0.6</I>

</html>

produces the wanted, correct result (<I> is replaced by <i>):

<html>
   <P style="TEXT-ALIGN: center; MARGIN: 0pt">
      <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 8.5pt; FONT-WEIGHT: bold">Form</SPAN>
      <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 7pt; FONT-WEIGHT: bold">Form</SPAN>
      <SPAN style="FONT-FAMILY: 'COMIC SANS MS'; FONT-SIZE: 6.7pt; FONT-WEIGHT: bold">Form</SPAN>
      <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE:12pt; FONT-WEIGHT: bold">10-Q</SPAN>
      <SPAN style="FONT-FAMILY: 'CURSIVE'; FONT-SIZE:14pt">Hisasdf @ adsfkej % adsfkj-Q</SPAN>
      <SPAN style="FONT-FAMILY: 'Edwardian Script ITC'; FONT-SIZE:18pt">Singhaniadf kurmad-Q</SPAN>
      <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE:36pt">
         <U>anurag arya fateh ali khan-Q</U>
      </SPAN>
      <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE:12pt">
         <U>rakesh kumar sinfhania</U>
      </SPAN>
   </P>
   <P style="MARGIN: 0pt">
      <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 18pt">of advisory fees, estimated legal fees, a $0.6</SPAN>
      <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 12pt">million<U> provision for estimated settlement costs associated with shareholder litigation and other
    transition related costs. These amounts are included in the Company?s </U>sults of operations for the three and nine months ended September</SPAN>
      <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 18pt">30, 2010,respectively.</SPAN>
   </P>
   <P style="TEXT-ALIGN: center; MARGIN: 0pt">
      <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 12pt; FONT-WEIGHT: bold">
         <i>Washington,</i>
      </SPAN>
      <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 12pt; FONT-WEIGHT: bold">D.C.     20549</SPAN>
      <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 12pt"/>
   </P>
   <i>of advisory fees, estimated legal fees, a $0.6</i>
</html>

Upvotes: 1

Lukasz
Lukasz

Reputation: 7662

You forgot to add the important block called identity transform to your XSLT:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xsl:template match="node()|@*" >
        <xsl:copy>
             <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
</xsl:template>

<xsl:template match="I">
            <i> <xsl:value-of select="."/></i>
        </xsl:template>

</xsl:stylesheet>

EDIT - Some clarification:

I have applied my XSLT to the following XML. Note that I added <body></body>, as according to the W3C standard XML can have only one root element.

<body>
<P style="TEXT-ALIGN: center; MARGIN: 0pt">
            <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 8.5pt; FONT-WEIGHT: bold">
            <I>Form tesindf is the good one m trying it</I>
            </SPAN>
            <SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 8.5pt; FONT-WEIGHT: bold"><I>Form tesindf is the good one m trying it</I></SPAN>
        </P>
<I>of advisory fees, estimated legal fees, a $0.6</I>
</body>

Upvotes: 1

Related Questions