当在根节点中声明了名称空间时,xslt脚本不起作用 [英] xslt script doesn't work when a namespace is declared in the root node

查看:90
本文介绍了当在根节点中声明了名称空间时,xslt脚本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的xslt代码不适用于下面的XML文件.但是,如果我从xml文件的对象"节点中删除名称空间属性,则它可以按预期工作.如何在根节点中具有名称空间属性的情况下修复xslt代码,使其能够正常工作?第一次发布,如果格式搞乱了,抱歉.谢谢

The xslt code below does not work for the XML file below. However, if I remove the namespace attributes from the "objects" node in the xml file, it works as expected. How can I fix the xslt code so that it will work when I have the namespace attributes in the root node? First posting, sorry if formatting messed up. Thanks

XML文件:

<objects xmlns="http://www.spicefactory.org/parsley"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<object id="models" type="blah">

</object>
</objects>

XSLT代码:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="no"/>

<xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>
 <xsl:template match="object">
  <object new_attr="attr value ">
    <xsl:apply-templates select="node()|@*"/>
  </object>
 </xsl:template>
</xsl:stylesheet>

推荐答案

您需要在xslt中声明并使用相同的xml名称空间:

You needs declare and use the same xml namespace in the xslt:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:sfp="http://www.spicefactory.org/parsley">

<xsl:output method="xml" indent="no"/>

<xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="sfp:object">
  <sfp:object new_attr="attr value ">
    <xsl:apply-templates select="node()|@*"/>
  </sfp:object>
 </xsl:template>
</xsl:stylesheet>

这篇关于当在根节点中声明了名称空间时,xslt脚本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆