xml - Delete namespace from xmlstarlet output -
background
looking extract elements following xml content:
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets"> <h:inputtext id="id"/> ... </ui:composition> extraction
all h:inputtext elements can selected using:
xmlstarlet sel -t -c "//h:inputtext" filename.xml problem
this produces following namespace-infested output:
<h:inputtext xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" id="id"/> question
how can namespaces suppressed output?
ideas
use regular expressions post-process; however:
- sed doesn't have non-greedy match;
- perl heavyweight (and require complex regex).
pipe through xmllint or xmlstarlet second pass, requires well-formed xml document.
using xmllint poses own set of namespace problems.
produce document comprised of ui:composition , h:inputtext elements:
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets"> <h:inputtext id="id"/> <h:inputtext id="id"/> </ui:composition> this tricky because h:inputtext elements can occur @ depth of document.
you use xslt. if want output h:inputtext as-is, won't able suppress namespace declaration binding prefix h: uri http://java.sun.com/jsf/html.
xslt 1.0
create input.xsl:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:h="http://java.sun.com/jsf/html"> <xsl:output omit-xml-declaration="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/"> <xsl:apply-templates select="//h:inputtext"/> </xsl:template> <xsl:template match="h:inputtext"> <xsl:copy> <xsl:copy-of select="@*"/> </xsl:copy> </xsl:template> </xsl:stylesheet> xmlstarlet command
xmlstarlet tr input.xsl filename.xml output
<h:inputtext xmlns:h="http://java.sun.com/jsf/html" id="id"/> you output inputtext in no namespace though...
xslt 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:h="http://java.sun.com/jsf/html" exclude-result-prefixes="h"> <xsl:output omit-xml-declaration="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="/"> <xsl:apply-templates select="//h:inputtext"/> </xsl:template> <xsl:template match="h:inputtext"> <inputtext> <xsl:copy-of select="@*"/> </inputtext> </xsl:template> </xsl:stylesheet> output
using same command line above:
<inputtext id="id"/> note: might need add <xsl:text>
</xsl:text> after </xsl:copy> (or </inputtext> in second example) explicitly add newline. otherwise xmlstartlet might output elements on single line. (it did me using xmlstarlet 1.6.1 , indent="yes" on xsl:output didn't help.)
jsf output
since jsf involved, consider:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:a4j="http://richfaces.org/a4j" exclude-result-prefixes="h f c ui a4j"> <xsl:output method="xml" omit-xml-declaration="yes" /> <xsl:strip-space elements="*"/> <xsl:template match="/"> <h:html> <xsl:apply-templates select="//h:inputtext"/> </h:html> <xsl:text>
</xsl:text> </xsl:template> <xsl:template match="h:inputtext"> <xsl:text>
</xsl:text> <h:inputtext> <xsl:copy-of select="@*"/> </h:inputtext> <xsl:text>
</xsl:text> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment