当前位置: 代码迷 >> XML/SOAP >> 关于设置默认命名空间后xslt转换显示的有关问题
  详细解决方案

关于设置默认命名空间后xslt转换显示的有关问题

热度:315   发布时间:2012-03-19 22:03:05.0
关于设置默认命名空间后xslt转换显示的问题
xml实例文件test.xml内容为
XML code
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="env.xslt"?>
<document xmlns="http://www.fyswords.com.cn/xml">
    <title>title</title>
    <department>dept</department>
    <region>region</region>
    <pass_date>2011-09-05</pass_date>
    <enforce_date>2011-09-05</enforce_date>
    <body>
        <chapter index="1">
            <text>chapter text</text>
            <clause index="1">
                <text>clause text</text>
            </clause>
        </chapter>
    </body>
</document>



转化用的xslt文件env.xslt为
XML code
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns="http://www.fyswords.com.cn/xml" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-16" indent="yes"/>

<xsl:template match="/">
 <html:html>
  <html:head>
   <html:title>
       <xsl:value-of select="/document/title"/>
   </html:title>
  </html:head>
  <html:body>
  <html:h2><xsl:value-of select="/document/title" /></html:h2>
  <html:p>发布部门:<xsl:value-of select="/document/department" /></html:p>
  <html:p>发布日期:<xsl:value-of select="/document/pass_date" /></html:p>
  <html:p>执行日期:<xsl:value-of select="/document/enforce_date" /></html:p>
  <xsl:apply-templates select="/document/body/chapter"/>
  </html:body>
 </html:html>
</xsl:template>

<xsl:template match="chapter">
    <html:h3><xsl:value-of select="text"/></html:h3>
    <xsl:apply-templates select="clause"/>
</xsl:template>

<xsl:template match="clause">
    <html:p><xsl:value-of select="text"/></html:p>
</xsl:template>

</xsl:stylesheet>



两个文件设置了相同的默认命名空间
XML code
xmlns="http://www.fyswords.com.cn/xml"


显示转换后的test.xml文件只有我输入的文本内容“发布部门:发布日期:执行日期:”,没有实质内容

------解决方案--------------------
xml文件不用改。你只需改xslt即可

<xsl:stylesheet xmlns:xx="http://www.fyswords.com.cn/xml" xmlns:html="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">


发布部门:<xsl:value-of select="/xx:document/xx:department" />

等,