java - Prefixing xml elements that are missing namespace -
i receiving pregenerated xml document string , need prefix xml elements prefix missing. example
input:
<a xmlns:b="http://www.b.com"> <element1>test</element1> <b:element2>test</b:element2> </a>
output:
<c:a xmlns:b="http://www.b.com" c="http://www.c.com"> <c:element1>test</c:element1> <b:element2>test</b:element2> </c:a>
i have document parsed node, can't figure how add prefix element missing prefix. going fall on regex string replacement, think should possible java xml api, i'm stuck.
the following xslt 1.0 transformation move no-namespace elements namespace prefix="c", uri="http://www.c.com".
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:template match="*[namespace-uri()='']"> <xsl:element name="c:{local-name()}" namespace="http://www.c.com"> <xsl:copy-of select="@*"/> <xsl:apply-templates/> </xsl:element> </xsl:template> <xsl:template match="*"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:apply-templates/> </xsl:copy> </xsl:template> </xsl:transform>
Comments
Post a Comment