2011-05-02 37 views
0

我想使用XMLT动态生成一个XML文件,该文件将另一个XML文件作为输入。我可以在此转换输出中消除名称空间声明吗?

<?xml version="1.0" encoding="ISO-8859-1"?> 

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

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

<xsl:template match="data"> 
    <xsl:apply-templates select="books" /> 
</xsl:template> 

<xsl:template match="books"> 
    <books> 
    <xsl:apply-templates select="chapters" /> 
    </books> 
</xsl:template> 

<xsl:template match="chapters"> 
    <chapter name="{@name}" source="{@source}" 
    xmlns:xi="http://www.w3c.org/2001/XInclude"> 
     <xsl:apply-templates select="pages" /> 
    </chapter> 
</xsl:template> 

<xsl:template match="pages" xmlns:xi="http://www.w3.org/2001/XInclude"> 
    <xi:include> 
     <xsl:value-of select="@name" /> 
    </xi:include> 
</xsl:template> 

</xsl:stylesheet> 

我希望的输出如下。

<chapter 
xmlns:xi="http://www.w3c.org/2001/XInclude" name="chapter1"> 
    <xi:include>page1</xi:include> 
    <xi:include>page2</xi:include> 
    <xi:include>page3</xi:include> 
</chapter> 

不幸的是,我的代码生成下面的输出。

<chapter 
xmlns:xi="http://www.w3c.org/2001/XInclude" name="chapter1"> 
    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude">page1</xi:include> 
    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude">page2</xi:include> 
    <xi:include xmlns:xi="http://www.w3.org/2001/XInclude">page3</xi:include> 
</chapter> 

有没有一种方法可以消除命名空间的实际URI,因为我可以使用xi:include tages without them?

回答

2

移动xmlns:xi="http://www.w3.org/2001/XInclude"xsl:stylesheet元素:

<xsl:stylesheet version="1.0" 
xmlns:xi="http://www.w3.org/2001/XInclude" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

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

<xsl:template match="data"> 
    <xsl:apply-templates select="books" /> 
</xsl:template> 

<xsl:template match="books"> 
    <books> 
    <xsl:apply-templates select="chapters" /> 
    </books> 
</xsl:template> 

<xsl:template match="chapters"> 
    <chapter name="{@name}" source="{@source}"> 
     <xsl:apply-templates select="pages" /> 
    </chapter> 
</xsl:template> 

<xsl:template match="pages"> 
    <xi:include> 
     <xsl:value-of select="@name" /> 
    </xi:include> 
</xsl:template> 

</xsl:stylesheet> 
+0

超爽。非常感谢你。 – lightonphiri 2011-05-02 16:27:06

2

请注意,您正在使用两个不同的命名空间的URI

http://www.w3.org/2001/XInclude 

http://www.w3c.org/2001/XInclude 

如果他们同样的,namespace fixup process会照顾到这一点。

此输入:

<books> 
    <chapters name="chapter1"> 
     <pages name="page1"/> 
     <pages name="page2"/> 
     <pages name="page3"/> 
    </chapters> 
</books> 

这个样式表(你有修改错字):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" /> 
    <xsl:template match="data"> 
     <xsl:apply-templates select="books" /> 
    </xsl:template> 
    <xsl:template match="books"> 
     <books> 
      <xsl:apply-templates select="chapters" /> 
     </books> 
    </xsl:template> 
    <xsl:template match="chapters"> 
     <chapter name="{@name}" source="{@source}" 
     xmlns:xi="http://www.w3c.org/2001/XInclude"> 
      <xsl:apply-templates select="pages" /> 
     </chapter> 
    </xsl:template> 
    <xsl:template match="pages" xmlns:xi="http://www.w3c.org/2001/XInclude"> 
     <xi:include> 
      <xsl:value-of select="@name" /> 
     </xi:include> 
    </xsl:template> 
</xsl:stylesheet> 

输出:

<books> 
    <chapter name="chapter1" source="" 
    xmlns:xi="http://www.w3c.org/2001/XInclude"> 
     <xi:include>page1</xi:include> 
     <xi:include>page2</xi:include> 
     <xi:include>page3</xi:include> 
    </chapter> 
</books> 
+0

@Alejandro:谢谢你。 – lightonphiri 2011-05-02 16:35:32

+0

@phiri:不客气。 – 2011-05-02 16:37:40

相关问题