2012-11-19 71 views
1

是否有可能有以下一個更好的解決方案:XSL:檢查項目是否存在於元素清潔解決方案中?

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

像這樣簡單

<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"/> 
+0

感謝您的回答! – flashbeir

+0

@flashbeir,不客氣。 –

相關問題