开发者

setting default values for empty nodes

I need to transform a piece of XML, so that the value of every node in a list I specify is set to "0"

for example:

<contract>
 <customerName>foo</customerName>
 <contractID />
 <customerID>912</customerID>
 <countryCode/>
 <cityCode>7823</cityCode>
</contract>

would be transformed into

<contract>
 <customerName>foo</customerName>
 <contractID>0</contractID>
 <customerID>912</customerID>
 <countryCode>0</contractID>
 <cityCode>7823</开发者_StackOverflow中文版cityCode>
</contract>

How can this be accomplished using XSLT? I have tried some examples I found but none works as expected

Thank you


This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="*[not(node())]">
  <xsl:copy>0</xsl:copy>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<contract>
 <customerName>foo</customerName>
 <contractID />
 <customerID>912</customerID>
 <countryCode/>
 <cityCode>7823</cityCode>
</contract>

produces the wanted, correct result:

<contract>
    <customerName>foo</customerName>
    <contractID>0</contractID>
    <customerID>912</customerID>
    <countryCode>0</countryCode>
    <cityCode>7823</cityCode>
</contract>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜