【问题标题】:xslt: xsl:value-of cannot doesnt work inside <div xmlns="http://www.w3.org/1999/xhtml">xslt: xsl:value-of 不能在 <div xmlns="http://www.w3.org/1999/xhtml">
【发布时间】:2016-09-24 23:05:48
【问题描述】:

我的数据的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>

我想使用 xslt 提取 &lt;p&gt; 标记内的“我的数据”并将其格式化为另一种 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>

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

关于我应该如何从&lt;div&gt; 中提取&lt;p&gt; 的任何想法?

【问题讨论】:

    标签: xml xslt


    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      相关资源
      最近更新 更多