XSLT初心者はこちら。何にでも似たXMLブロックがありますが、その要素名に基づいて、その内容を変更できる必要があります。問題は、XMLにプレフィックスが付いているかどうかです。
接頭辞付きのXMLは次のようになります。
<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>
または接頭辞なし:
<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>
値があるときに_dateで終わる要素の内容を変更したい。
したがって、可能な出力は次のようになります。
<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>
これは私がこれまでに持っているものです:
問題は、接頭辞が付いている場合は変更された要素のtns:名前空間に文句を言うか、接頭辞のないXML要素に空白の名前空間を置くことです。
解決策はありますか?これはユーティリティ変換なので、できるだけ一般的なものにしておきたいと思います。
<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>