2015-10-05 65 views
1

我試圖讓cond1-3一個for-each循環,因爲輸出代碼均相同,XSLT的for-each超過3開出的條件的7

<?xml version="1.0"?> 
<root> 
<stuff> 
    <morestuff/> 
</stuff> 
<conds> 
    <cond1>yes</cond1> 
    <cond2>yes</cond2> 
    <cond3>yes</cond3> 
    <cond4>yes</cond4> 
    <cond5>yes</cond5> 
    <cond6>yes</cond6> 
    <cond7>yes</cond7> 
</conds> 
<stuff> 
    <morestuff/> 
</stuff> 

所以輸出必須是這樣的

<?xml version="1.0"?> 
<soapheader/> 
<soapbody> 
<somebiprostuff> 
    <m:Element xsi:type="komp:CT_c"> 
    <v:Leistung> 
     <v:ArtID xsi:type="dt:STE_d" /> 
     <v:Wert /> 
     <v:Werteinheit /> 
    </v:Leistung> 
    <komp:ArtID xsi:type="dt:STE_something">666</komp:ArtID> 
    <komp:Gefahr> 
     <komp:Gefahr xsi:type="dt:STE_something">F</komp:Gefahr> 
    </komp:Gefahr> 
    <komp:Gefahr> 
     <komp:Gefahr xsi:type="dt:STE_something">S</komp:Gefahr> 
    </komp:Gefahr> 
    <komp:Gefahr> 
     <komp:Gefahr xsi:type="dt:STE_something">L</komp:Gefahr> 
    </komp:Gefahr> 
</m:Element> 
<m:Element xsi:type="komp:CT_c"> 
    <v:Leistung> 
     <v:ArtID xsi:type="dt:STE_d" /> 
     <v:Wert /> 
     <v:Werteinheit /> 
    </v:Leistung> 
    <komp:ArtID xsi:type="dt:STE_something">777</komp:ArtID> 
    <komp:Gefahr> 
     <komp:Gefahr xsi:type="dt:STE_something">F</komp:Gefahr> 
    </komp:Gefahr> 
    <komp:Gefahr> 
     <komp:Gefahr xsi:type="dt:STE_Gsomething">S</komp:Gefahr> 
    </komp:Gefahr> 
    <komp:Gefahr> 
     <komp:Gefahr xsi:type="dt:STE_something">L</komp:Gefahr> 
    </komp:Gefahr> 
</m:Element> 
</somebiprostuff> 
</soapbody> 

所以當輸入XML包含了666和777,並cond1-3的產品是肯定的,這應該是正確的輸出。

我試圖解決這種方式,但它沒有工作非常出色

<?xml version="1.0" encoding="utf-8"?> 
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
<soapheader/> 
<soapbody> 
<somebiprostuff> 
<xsl:call-template name="element"/> 
</somebiprostuff> 
</soapbody> 
</xsl:template> 

<xsl:temaplate name="element"> 
<m:Element xsi:type="komp:CT_c"> 
<v:Leistung> 
<v:ArtID xsi:type="dt:STE_d" /> 
<v:Wert /> 
<v:Werteinheit /> 
</v:Leistung> 
<komp:ArtID xsi:type="dt:STE_something">777</komp:ArtID> 
<xsl:for-each select="//cond1 = 'yes' or //cond2 = 'yes' or //cond3 = 'yes'"> 
<komp:Gefahr> 
<komp:Gefahr xsi:type="dt:STE_xy"> 
<xsl:choose> 
<!-- cond1 --> 
<xsl:when test="//cond1 = 'yes'"> 
<xsl:value-of select="'F'"/> 
</xsl:when> 
<!-- cond2 --> 
<xsl:when test="//cond2 = 'yes'"> 
<xsl:value-of select="'S'"/> 
</xsl:when> 
<!-- cond3 --> 
<xsl:when test="//cond3 = 'yes'"> 
<xsl:value-of select="'L'"/> 
</xsl:when> 
</xsl:choose> 
</komp:Gefahr> 
</komp:Gefahr> 
</xsl:for-each> 
<xsl:/template> 

這導致空輸出。 我怎樣才能得到這個循環的權利,如果有可能獲得3,2或1重複

回答

2

所需的轉換規則並不完全清楚。像這樣的東西適合你嗎?

<xsl:template match="conds"> 
    <result> 
     <xsl:apply-templates select="cond1 | cond2 | cond3"/> 
    </result> 
</xsl:template> 

<xsl:template match="cond1 | cond2 | cond3"> 
    <xsl:if test=".='yes'"> 
     <t:x> 
      <t:x xsi:type="dt:STE_xy"> 
       <xsl:choose> 
        <xsl:when test="self::cond1">F</xsl:when> 
        <xsl:when test="self::cond2">S</xsl:when> 
        <xsl:when test="self::cond3">L</xsl:when> 
       </xsl:choose> 
      </t:x> 
     </t:x> 
    </xsl:if> 
</xsl:template> 
+0

下面的代碼應該產生右輸出,但不看起來不錯: '的 ˚F 取值 ' – Mofty

+0

大號@Mofty不我的「很好「的代碼生成相同的輸出? –

+0

沒有爲我工作,所以我寫在一個單獨的模板,並稱它 – Mofty