Вот мой (упрощенный для в этом случае сценарий) XML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="test_1.xsl" type="text/xsl"?>
<doc xmlns="http://www.foo.org">
<div>
<title>Mr. Title</title>
<paragraph>This is one paragraph.
</paragraph>
<paragraph>Another paragraph.
</paragraph>
</div>
</doc>
И вот мой XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:foo="http://www.foo.org">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="foo:doc">
<xsl:element name="newdoc" namespace="http://www/w3.org/1999/xhtml">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="foo:div">
<segment title="{foo:title}">
<xsl:apply-templates/>
</segment>
</xsl:template>
<xsl:template match="foo:title">
<xsl:element name="h2">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="foo:paragraph">
<xsl:element name="p">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
В результате получается следующее:
<newdoc xmlns="http://www/w3.org/1999/xhtml">
<segment xmlns="" title="Mr. Title">
<h2>Mr. Title</h2>
<p>This is one paragraph.
</p>
<p>Another paragraph.
</p>
</segment>
</newdoc>
, и это здорово, за исключением xmlns = "" в элементе сегмента, который, похоже, не определяет пространство имен для сам и все его дети. Как я могу заставить его не добавлять это?
Замечание: я также пробовал преобразовать первый узел с помощью
<xsl:template match="mydoc:doc">
<html xmlns="http://www/w3.org/1999/xhtml">
<xsl:apply-templates/>
</html>
</xsl:template>
, но это дает тот же эффект.
Спасибо, полезные люди!