A XSLT newbie here. I have an XML block that can look like anything, however based on its element name, I need to be able to change its content. The problem is that the XML could be prefixed or not.
A prefixed XML might look like:
<POIS xmlns:tns="http://example.com/integration/docs"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tns:POI>
<tns:CTL>
<tns:transaction_date/>
<tns:record_qualifier/>
<tns:start_date>2012-10-12 </tns:start_date>
<tns:test_indicator>P</tns:test_indicator>
</tns:CTL>
</tns:POI>
</tns:POIS>
Or not Prefixed:
<POIS xmlns="http://example.com/integration/docs"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<POI>
<CTL>
<transaction_date/>
<record_qualifier/>
<start_date>2012-10-12</start_date>
<test_indicator>P</test_indicator>
</CTL>
</POI>
</POIS>
I want to change the content of the element that ends with _date when there is a value.
So a possible output will be:
<POIS xmlns:tns="http://example.com/integration/docs"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tns:POI>
<tns:CTL>
<tns:transaction_date/>
<tns:record_qualifier/>
<tns:start_date>20121012 </tns:start_date>
<tns:test_indicator>P</tns:test_indicator>
</tns:CTL>
</tns:POI>
</tns:POIS>
This is what I have so far:
The problem is that it complains with tns: namespace on the changed element when prefixed or put a blank namespace for non-prefixed XML element.
Any solutions? This is a utility transformation so I would like to keep it as generic as possible.
<xsl:stylesheet version="2.0"
xmlns:xp20="http://www.oracle.com/XSL/Transform/
java/oracle.tip.pc.services.functions.Xpath20"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()[ends-with(name(), '_date')]">
<xsl:choose>
<xsl:when test="(text())" >
<xsl:element name="{name()}" >
<xsl:value-of select ="xp20:format-dateTime(text(),'[Y0001][M01][D01]')" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<CTL>should end with</CTL>, missing</POI>closing tag, inconsistent prefixing on<POIS>tag. Please fix your XML.<xsl:element name="{name()}">to<xsl:element name="{local-name()}">...does that help?