的元素從xmlns:ns0="http://root"
命名空間移動到默認的命名空間。 用途:
<xsl:template match="ns0:*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
爲了使http://root
默認命名空間添加xmlns="http://root"
樣式表聲明。
因此,你可以嘗試這樣的事:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:ns0="http://root"
xmlns="http://root"
exclude-result-prefixes="ns0" >
<!-- Identity transform (e.g. http://en.wikipedia.org/wiki/Identity_transform#Using_XSLT -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<!-- match root element and fore notroot namespace to this -->
<xsl:template match="/*">
<Root xmlns:nm="http://notroot">
<xsl:apply-templates select="@*|node()" />
</Root>
</xsl:template>
<xsl:template match="content">
<content>
<xsl:apply-templates select="@*|node()" />
</content>
</xsl:template>
<!-- move attributes with prefix ns0 to default namespace -->
<xsl:template match="@ns0:*">
<xsl:attribute name="newns:{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<!-- move elements with prefix ns0 to default namespace -->
<xsl:template match="ns0:*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
+1。最後,添加'exclude-result-prefixes =「ns0」'來排除輸出'Root'元素上多餘的'xmlns:ns0'聲明。 –
謝謝!有用!你能解釋一下每個模板在做什麼嗎? – ohadinho
@ohadinho:我添加一些評論。 –