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

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

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

谢谢。

最佳答案

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: 前缀。

关于xslt - 无法识别 XSLT 中的 <p> 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10275427/

相关文章:

xslt - 根据两个属性值的组合选择唯一节点

xslt - 使用 XSL 连接重复的标签

xslt - XSLT 转换中的动态文档类型(正确使用结果文档指令)

java - 将不同的 xml 转换为另一个 xml

javascript - 我的 javascript 代码中的字符串比较有什么问题?

xslt - 比较 XSLT <xsl :if>? 中的字符串时如何指定 "not equals to"

html - 将新闻列表转换为按年和月分组的 HTML 菜单

xslt - 检查 XSLT 中的字符串是否为 Null 或空

xml - 如何使用 XSLT 获取 XML 属性的首字母?

java - JDK 6 是否支持 XSLT 2.0?