I'm trying to update just finalName, artifactId and name in pom.xml using xslt. I don't understand xslt transformation, but I found this: how to modify xml file using xslt which seems very easy to follow, so based on that I created this template:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="//artifactId">
<artifactId>aaaaaaaaaaaaaaaaaaaaaaaaaaa123</artifactId>
</xsl:template>
</xsl:stylesheet>
and building it using:
xsltproc template.xsl pom.xml > modified-pom.xml
however for some unknown to me reason, the xpath //artifactId
does not seem to be matched and replaced. I tried also: artifactId
only, project/artifactId
, /project/artifactId
etc. but none matches. Remembering lesson from using xmlstalet
which requires specific namespaces, this seems to be maybe somehow namespace related, but I really don't know how to fix it, as man xsltproc seems to be somehow broken/not reflecting command reality. Can someone help where is the error?
EDIT: added partially solved state, based on request from comment:
as an minimally working example input file , we might consider this pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>toUpdate</artifactId>
<name>toUpdate</name>
<properties>
<finalName>toUpdate</finalName>
</properties>
</project>
I'm using now template:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://maven.apache.org/POM/4.0.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/m:project/m:artifactId">
<artifactId>updated</artifactId>
</xsl:template>
<xsl:template match="/m:project/m:name">
<name>updated</name>
</xsl:template>
<xsl:template match="/m:project/m:properties/m:finalName">
<finalName>updated</finalName>
</xsl:template>
</xsl:stylesheet>
and replacing it using command: xsltproc template.xsl min.xml > out.xml
the output is:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId xmlns="" xmlns:m="http://maven.apache.org/POM/4.0.0">updated</artifactId>
<name xmlns="" xmlns:m="http://maven.apache.org/POM/4.0.0">updated</name>
<properties>
<finalName xmlns="" xmlns:m="http://maven.apache.org/POM/4.0.0">updated</finalName>
</properties>
</project>
where I'd like to get rid of part xmlns="" xmlns:m="http://maven.apache.org/POM/4.0.0"
if I update the replacement in template to: <m:finalName>updated</m:finalName>
I'm not getting the correct result, but instead:
<m:finalName xmlns:m="http://maven.apache.org/POM/4.0.0">updated</m:finalName>
trying to declare global namespace instead of m
also did not help me, as it even did not build.
Expected output is input with toUpdate
replaced with updated