Php 7个条件中超过3个条件的XSLT
我试图在for each循环中得到cond1-3,因为输出标记是相同的Php 7个条件中超过3个条件的XSLT,php,xml,xslt,soap,xslt-1.0,Php,Xml,Xslt,Soap,Xslt 1.0,我试图在for each循环中得到cond1-3,因为输出标记是相同的 <?xml version="1.0"?> <root> <stuff> <morestuff/> </stuff> <conds> <cond1>yes</cond1> <cond2>yes</cond2> <cond3>yes</cond3>
<?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>
666
F
s
L
777
F
s
L
因此,当输入xml包含666和777的产品时,cond1-3是yes,这应该是正确的输出
我试着这样处理,但效果不太好
<?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>
777
这会导致输出为空。
如果有可能获得3次、2次或1次重复,我如何才能正确地实现这个循环?所需转换的规则并不完全清楚。你喜欢这个工作吗
<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>
F
s
L
下面的代码应该生成正确的输出,但看起来不太好:fsl
@Mofty不是我的“好”代码生成相同的输出?对我来说不是这样的,所以我在一个单独的模板中编写它并调用it@Mofty我不认为这是必要的,但你没有向我们展示整个画面。很好的逻辑,加上一个。