将<i>标签文本的所有出现更改为斜体</i>

时间:2012-02-27 07:08:44

标签: html xml xslt

这是我的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>的所有出现更改为斜体

我也在匹配语句match="span/p/i"

中使用此行 和谷歌建议用斜体改变文字的许多其他内容,但它无法正常工作

2 个答案:

答案 0 :(得分:1)

您忘记将名为identity transform的重要块添加到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>

编辑 - 一些澄清:

我已将XSLT应用于以下XML。请注意,我添加了<body></body>,因为根据W3C标准,XML只能有一个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>

答案 1 :(得分:1)

首先,请注意提供的片段是不是格式良好的XML。我所做的是将其打造成格式良好的XML文档:

  1. 将其包装到单个顶级html元素中。

  2. &nbsp;实体的所有出现位置替换为字符实体&#xA0;(或者可以添加一个小的DTD,用于定义实体)

  3. <强>解决方案

    这个最简单的转换(覆盖身份规则):

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

    应用于任何格式良好的XML文档(在此示例中,我们使用从提供的片段生成的格式良好的XML文档):

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

    生成想要的正确结果<I>替换为<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>