установка значений по умолчанию для пустых узлов

Я должен преобразовать часть XML, так, чтобы значение каждого узла в списке, который я указываю, было установлено к "0"

например:

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

был бы преобразован в

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

Как это может быть выполнено с помощью XSLT? Я попробовал некоторые примеры, которые я нашел, но ни один не работает как ожидалось

Спасибо

1
задан azathoth 7 May 2010 в 19:48
поделиться

1 ответ

Это преобразование:

<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>

при применении к предоставленному XML-документу :

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

дает желаемый правильный результат :

<contract>
    <customerName>foo</customerName>
    <contractID>0</contractID>
    <customerID>912</customerID>
    <countryCode>0</countryCode>
    <cityCode>7823</cityCode>
</contract>
2
ответ дан 3 September 2019 в 00:44
поделиться
Другие вопросы по тегам:

Похожие вопросы: