xslt:xsl:value-of不能在<div xmlns =“http://www.w3.org/1999/xhtml”>中运行

时间:2016-05-26 19:22:57

标签: xml xslt

我的数据的xml格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<table_data name="dbTest">
    <row>
        <test name='temp'>
            <div xmlns="http://www.w3.org/1999/xhtml" >
                 <p> My data </p>
                 <ul><li>One</li><li>Two</li></ul>      
            </div>
        </test>
        <test name='temp2'>
                <div xmlns="http://www.w3.org/1999/xhtml" >
                     <p> Other data </p>
                     <ul><li>One</li><li>Two</li></ul>      
                </div>
        </test>
     </row>
</table_data>

我想提取&#34;我的数据&#34;使用xslt在<p>标记内,并将其格式化为另一种xml格式。我的.xslt文件如下:

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

<xsl:template match="/">
<newformat>
  <xsl:for-each select="table_data/row">       
        <text><xsl:value-of select="test[@name='temp']/div/p"/></text>
  </xsl:for-each>
</newformat>
  </xsl:template>
</xsl:stylesheet>

我没有看到任何结果。但是,如果我只使用<xsl:value-of select="test[@name='temp']"/>,我会在div中看到<p><ul>标记内的所有内容。

关于如何从<p>中仅提取<div>的任何想法?

1 个答案:

答案 0 :(得分:1)

XML中的div元素及其后代位于命名空间中。您必须使用绑定到同一名称空间的前缀来选择它们:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.w3.org/1999/xhtml"
exclude-result-prefixes="x">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>

<xsl:template match="/">
    <newformat>
        <xsl:for-each select="table_data/row">       
            <text>
                <xsl:value-of select="test[@name='temp']/x:div/x:p"/>
            </text>
        </xsl:for-each>
    </newformat>
</xsl:template>

</xsl:stylesheet>