是否有可能为以下问题提供更好的解决方案:
输入xml
<product>
<text>
<languageId>en-us</languageId>
<description>some text en us</description>
</text>
<text>
<languageId>en-gb</languageId>
<description>some text en gb</description>
</text>
<text>
<languageId>en-us</languageId>
</text>
<product>
输出xml
<specifications>some text en us</specifications>
因此,如果存在 languageId = en-us 的描述并且存在文本,则该文本将放置在输出 xml 中,否则元素接收属性值 xsi:nil=true
xslt 版本必须是 1.0
XSLT
<ns0:specifications>
<!-- First loop, check if en-us is present, if so, check if there is a text! -->
<!-- If the 2 requirements are met, then this Txt element is used -->
<xsl:for-each select="s0:text">
<xsl:if test="translate(s0:LanguageId/text(),$smallcase,$uppercase)=translate('en-us',$smallcase,$uppercase)">
<xsl:if test="s0:Txt!=''">
<xsl:value-of select="s0:Txt/text()" />
</xsl:if>
</xsl:if>
</xsl:for-each>
<!-- Second loop, checks are the same. This loop is needed because xsl variables are immutable. If there is a better solution, just change the code!! -->
<!-- If the 2 requirements are met, then the variable is marked as true, else it's empty -->
<xsl:variable name="isEnUsPresent">
<xsl:for-each select="s0:text">
<xsl:if test="translate(s0:LanguageId/text(),$smallcase,$uppercase)=translate('en-us',$smallcase,$uppercase)">
<xsl:if test="s0:Txt!=''">
<xsl:value-of select="1" />
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<!-- if the variable is empty, set the attribute value xsi:nil=true like below -->
<xsl:choose>
<xsl:when test="$isEnUsPresent=''">
<xsl:attribute name="xsi:nil">
<xsl:value-of select="'true'" />
</xsl:attribute>
</xsl:when>
</xsl:choose>
</ns0:specifications>
问候