これはXSLTでは簡単です。
必要なすべての置換を実行する例を次に示します。
このソースXMLファイルを考えると:
<html xmlns:old="old:namespace">
<head>
<meta property="og:type" content="webcontent"/>
</head>
<view>Some View</view>
<body>
The Body here.
</body>
</html>
この変換は、要求されたすべての変更を実行します。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:h="some:h" xmlns:old="old:namespace" xmlns:new="new:new"
exclude-result-prefixes="h new old">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vnsH" select="document('')/*/namespace::h"/>
<xsl:variable name="vnsNew" select="document('')/*/namespace::new"/>
<xsl:template match="*">
<xsl:element name="{name()}" namespace="{namespace-uri()}">
<xsl:copy-of select="namespace::*[not(name()='old')]"/>
<xsl:if test="namespace::old">
<xsl:copy-of select="$vnsNew"/>
</xsl:if>
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{name()}" namespace="{namespace-uri()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="node()[not(self::*)]">
<xsl:copy/>
</xsl:template>
<xsl:template match="view"/>
<xsl:template match="/*">
<xsl:element name="{name()}">
<xsl:copy-of select="namespace::*[not(name()='old')]|$vnsH"/>
<xsl:if test="namespace::old">
<xsl:copy-of select="$vnsNew"/>
</xsl:if>
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="head|body">
<xsl:element name="h:{name()}" namespace="some:h">
<xsl:copy-of select="namespace::*[not(name()='old')]"/>
<xsl:if test="namespace::old">
<xsl:copy-of select="$vnsNew"/>
</xsl:if>
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
上記のXMLドキュメントに適用すると、必要な正しい結果が生成されます。
<html xmlns:h="some:h" xmlns:new="new:new">
<h:head>
<meta property="og:type" content="webcontent"/>
</h:head>
<h:body>
The Body here.
</h:body>
</html>
注意してください:
<view>
削除されました。
<head>
および<body>
に変換され<h:head>
ます<h:body>
。
これold
で、名前空間が名前空間に置き換えられましたnew
。