i need to exit for each loop immediately once condition gets true. and want to return current index where loop breaks. below is my sample xml and xslt
<xsl:stylesheet xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="Data">
<xsl:element name="Data">
<xsl:call-template name="TempData" />
</xsl:element>
</xsl:template>
<xsl:template name="TempData">
<xsl:element name="TempData">
<xsl:for-each select="/Data/Subject/SubTest">
<xsl:choose>
<xsl:when test="@System='OK' and @SubFlag!='1'">
<xsl:attribute name="CurrentSubFlag">
<xsl:value-of select="position()" />
</xsl:attribute>
</xsl:when>
</xsl:choose>
</xsl:for-each>
<xsl:copy-of select="TempData/*" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
<Data>
<Subject>
<SubTest SubFlag="1" System="OK"> </SubTest>
<SubTest SubFlag="csdcd" System="OK"> </SubTest>
<SubTest SubFlag="zxczx" System="OK"> </SubTest>
</Subject>
<TempData CurrentSubFlag="abc"/>
</Data>
output xml should be
<Data>
<Subject>
<SubTest SubFlag="1" System="OK"> </SubTest>
<SubTest SubFlag="csdcd" System="OK"> </SubTest>
<SubTest SubFlag="zxczx" System="OK"> </SubTest>
</Subject>
<TempData CurrentSubFlag="2"/>
</Data>
if input is
<Data>
<Subject>
<SubTest SubFlag="1" System="OK"> </SubTest>
<SubTest SubFlag="1" System="OK"> </SubTest>
<SubTest SubFlag="1" System="OK"> </SubTest>
</Subject>
<TempData CurrentSubFlag="abc"/>
</Data>
then output will be same CurrentSubFlag="abc"
xsl:for-each
is not a loop, and the result you need can be accomplished without a loop or recursion.
Given:
XML
<Data>
<Subject>
<SubTest SubFlag="1" System="OK"> </SubTest>
<SubTest SubFlag="csdcd" System="OK"> </SubTest>
<SubTest SubFlag="zxczx" System="OK"> </SubTest>
</Subject>
<TempData CurrentSubFlag="abc"/>
</Data>
the following stylesheet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="TempData">
<xsl:variable name="index" select="count(/Data/Subject/SubTest[not(@SubFlag=1 and @System='OK')][1]/preceding-sibling::SubTest) + 1" />
<TempData CurrentSubFlag="{$index}"/>
</xsl:template>
</xsl:stylesheet>
will return:
Result
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Subject>
<SubTest SubFlag="1" System="OK"/>
<SubTest SubFlag="csdcd" System="OK"/>
<SubTest SubFlag="zxczx" System="OK"/>
</Subject>
<TempData CurrentSubFlag="2"/>
</Data>
To accommodate your added condition, try:
<xsl:template match="TempData">
<xsl:variable name="fail" select="/Data/Subject/SubTest[not(@SubFlag=1 and @System='OK')]" />
<xsl:choose>
<xsl:when test="$fail">
<TempData CurrentSubFlag="{count($fail[1]/preceding-sibling::SubTest) + 1}"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다