I want to have possibility to remove or replace unnecessary text inside values in elements in xml file and I want use an XSLT transformation to do that.
In this example file i want to remove specific tags like br, prefix, suffix, bulletlist or replace them into specific text or simple character inside value of Value elements.
There will be many Product elements. And I don't want change structure of this file, so this should be like copy, but with with logic to remove specific texts.
I tried to use templates and copy, but somehow I cannot connect them together.
If any of you could help me with or give me hints that I should follow, I would appreciate.
<ProductInfo>
<Products>
<Product>
<Name>xyz</Name>
<Values>
<Value><br/>test</Value> <-- remove <br/>
<Value><prefix>test2</prefix></Value> <-- replace <prefix> and </prefix> with " ." (white space and dot)
<Value><suffix/>test3</Value> <-- remove <suffix/>
<Value><list/><bulletlist>test4</bulletlist></Value> <-- replace <bulletlist> with "<ul>, </bulletlist> with </ul>"
</Values>
</Product>
<Product>
<Name>xyz</Name>
<Values>
<Value><br/>test</Value> <-- remove <br/>
<Value><prefix>test2</prefix></Value> <-- replace <prefix> and </prefix> with " ." (white space and dot)
<Value><suffix/>test3</Value> <-- remove <suffix/>
<Value><list/><bulletlist>test4</bulletlist></Value> <-- replace <bulletlist> with "<ul>, </bulletlist> with </ul>"
</Values>
</Product>
</Products>
</ProductInfo>
Output file should look like:
<ProductInfo>
<Products>
<Product>
<Name>xyz</Name>
<Values>
<Value>test</Value>
<Value> .test2 .</Value>
<Value>test3</Value>
<Value><ul>test4</ul></Value>
</Values>
</Product>
<Product>
<Name>xyz</Name>
<Values>
<Value>test</Value>
<Value> .test2 .</Value>
<Value>test3</Value>
<Value><ul>test4</ul></Value>
</Values>
</Product>
</Products>
</ProductInfo>
To get the result you show, you would need to do:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- remove br, suffix, list altogether-->
<xsl:template match="br | suffix | list"/>
<!-- replace prefix tags with text, keep contents -->
<xsl:template match="prefix">
<xsl:text> .</xsl:text>
<xsl:apply-templates/>
<xsl:text> .</xsl:text>
</xsl:template>
<!-- rename bulletlist -->
<xsl:template match="bulletlist">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:template>
</xsl:stylesheet>
Note that this assumes that both suffix
and list
(and of course br
) are empty. If this assumption is incorrect and you want to keep their contents, change the template that handles them to:
<!-- remove br, suffix, list wrappers, keep contents -->
<xsl:template match="br | suffix | list">
<xsl:apply-templates/>
</xsl:template>