无法识别XSLT中的<p>标记</p>

时间:2012-04-23 05:48:20

标签: xslt xslt-1.0 xslt-2.0

我正在研究XSLT。我无法找到xslt。我试过了。

来源:

 <?xml version="1.0" encoding="ISO-8859-1"?>
 <body>
<selectedComp>bodyParagraphText</selectedComp>
<value>
  <p xmlns="http://www.w3.org/1999/xhtml">abd</p>


  <p xmlns="http://www.w3.org/1999/xhtml"> </p>


  <p xmlns="http://www.w3.org/1999/xhtml">afh</p>


  <p xmlns="http://www.w3.org/1999/xhtml"> </p>


  <p xmlns="http://www.w3.org/1999/xhtml">AAA</p>


  <p xmlns="http://www.w3.org/1999/xhtml"> </p>


  <p xmlns="http://www.w3.org/1999/xhtml">ZZZ</p>
</value>
</body>

XSLT编写:

  <?xml version="1.0" encoding="utf-8"?>
   <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:widget="aaa">
  <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
    <xsl:for-each select="body/value/p">
       <xsl:element name= "widget:bodyParagraphText">
      <xsl:value-of select="."/>
      </xsl:element>
        </xsl:for-each>

      </xsl:template>


       </xsl:stylesheet>

但我没有得到任何东西。我最后得到了空的xml。

但需要输出:

   <widget:bodyParagraphText>
   <text>abd</text>
   </widget:bodyParagraphText>

   <widget:bodyParagraphText>
   <text> </text>
   </widget:bodyParagraphText>

   <widget:bodyParagraphText>
   <text>afh</text>
   </widget:bodyParagraphText>

   <widget:bodyParagraphText>
   <text> </text>
   </widget:bodyParagraphText>

   <widget:bodyParagraphText>
   <text>AAA</text>
   </widget:bodyParagraphText>

   <widget:bodyParagraphText>
   <text> </text>
   </widget:bodyParagraphText>

任何人都可以建议如何做到这一点。

谢谢。

1 个答案:

答案 0 :(得分:2)

p元素的名称空间为http://www.w3.org/1999/xhtml,您需要将其包含在XPath表达式中:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:widget="aaa"
        xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <xsl:for-each select="body/value/xhtml:p">
            <widget:bodyParagraphText>
                <text>
                    <xsl:value-of select="."/>
                </text>
            </widget:bodyParagraphText>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

请注意在根标记上添加了xmlns:xhtml属性,以及XPath表达式中的xhtml:前缀。

相关问题