1

是否有可能为以下问题提供更好的解决方案:

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

问候

4

1 回答 1

1

就这么简单

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 exclude-result-prefixes="xsi">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <specifications>
      <xsl:apply-templates select="*"/>
     </specifications>
 </xsl:template>    

 <xsl:template match="text[languageId='en-us']/description">
  <xsl:value-of select="."/>
 </xsl:template>

 <xsl:template match="/*[not(text[languageId='en-us']/description)]">
  <xsl:attribute name="xsi:nil">true</xsl:attribute>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

当此转换应用于提供的 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>

产生了想要的正确结果:

<specifications>some text en us</specifications>

当对这个 XML 文档应用相同的转换时

<product>
    <text>
        <languageId>en-us-XXX</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>

再次产生所需的正确结果:

<specifications xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
于 2012-11-19T18:06:03.543 回答