XSL:检查元素 - 清洁解决方案中是否存在项目?

时间:2012-11-19 16:05:36

标签: xslt-1.0

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

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

此致

1 个答案:

答案 0 :(得分: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"/>