2012-04-11 189 views
1

我使用PHP的DOMDocument的XSLT转换一个XML到另一个,我需要使用额外的标签 - mytag中产生的文档标签像定义自定义标签

<mytag:full-text>...</mytag:full-text> 

来定义这个标签我tryed建设像这样:

<xsl:namespace-alias stylesheet-prefix="mytag" result-prefix="mytag"/> 

,所以我得到一个错误

Warning: DOMDocument::load() [domdocument.load]: Namespace prefix mytag on full-text is not defined 

我做错了什么?

+0

你似乎使用“标签”来表示“命名空间前缀”和“元素”。前者特别不准确。如果你学习了一些基本的XML术语,我认为你会让自己的工作更容易,并且可以与他人进行交流以获得帮助。例如。看到http://www.cafeconleche.org/books/effectivexml/chapters/00.html – LarsH 2012-04-11 18:01:28

回答

2

下面是一个完整的代码示例如何做到这一点

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:mytag="my:tag" exclude-result-prefixes="mytag"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="b"> 
    <b> 
    <mytag:full-text>Some full-text here</mytag:full-text> 
    </b> 
</xsl:template> 
</xsl:stylesheet> 

将此转换应用于以下XML文档时:

<a><b/></a> 

想要的,正确的结果(b下添加的新元素)产生:

<a> 
    <b> 
     <mytag:full-text xmlns:mytag="my:tag">Some full-text here</mytag:full-text> 
    </b> 
</a> 
+0

this:exclude-result-prefixes =“mytag”工作正常! – shershen 2012-04-12 07:04:14

+0

@shershen:不客气。 – 2012-04-12 11:40:52

1

尝试增加的xmlns:mytag = “some_namespace” 你的XSLT的根,所以你得到这样的事情

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:mytag="some_namespace"> 
+0

这个错误已经消失,但现在我得到属性xmlns:mytag =“http:// ...”结果中的每个根标签xml文件 – shershen 2012-04-11 12:51:05

+0

这是预期的行为。为了使输出成为有效的XML,'mytag'的名称空间前缀必须在XML文档中的某个位置声明,否则'mytag'没有意义。如果您不希望将其添加到所有子节点,则可以手动将其添加到输出的根节点。 – 2012-04-11 13:12:14

+0

@shershen:我不知道“root tag”是什么意思 - 可能是最外层元素的开始标记?根据你的意思,你可以在'xsl:stylesheet'元素上使用'exclude-result-prefixes =“*”'。另一个问题是,为什么要避免在某些元素上使用xmlns:mytag声明?它不应该伤害任何东西,如果你要使用命名空间,一些声明是必要的。如果您拥有您认为不必要的名称空间声明,并且无法通过排除结果前缀解决,请提供详细信息。 – LarsH 2012-04-11 17:55:28