Source Xml :
<Lot>
<LotDetails>
<GCode>Ship</GCode>
<ProductQuantity>2</ProductQuantity>
</LotDetails
<LotDetails>
<GCode>Reject</GCode>
<ProductQuantity>4</ProductQuantity>
</LotDetails>
<LotDetails>
<GCode>Gross</GCode>
<ProductQuantity>3</ProductQuantity>
</LotDetails>
<LotDetails>
<GCode>Acceptable</GCode>
<ProductQuantity>5</ProductQuantity>
</LotDetails>
</Lot>
<Lot>
<LotDetails>
<GCode>Ship</GCode>
<ProductQuantity>2</ProductQuantity>
</LotDetails
<LotDetails>
<GCode>Reject</GCode>
<ProductQuantity>4</ProductQuantity>
</LotDetails>
<LotDetails>
<GCode>Gross</GCode>
<ProductQuantity>3</ProductQuantity>
</LotDetails>
<LotDetails>
<GCode>Acceptable1</GCode>
<ProductQuantity>5</ProductQuantity>
</LotDetails>
</Lot>
<LotDetails>
<GCode>Ship1</GCode>
<ProductQuantity>2</ProductQuantity>
</LotDetails
<LotDetails>
<GCode>Reject</GCode>
<ProductQuantity>4</ProductQuantity>
</LotDetails>
<LotDetails>
<GCode>Gross</GCode>
<ProductQuantity>3</ProductQuantity>
</LotDetails>
<LotDetails>
<GCode>Acceptable1</GCode>
<ProductQuantity>5</ProductQuantity>
</LotDetails>
</Lot>
TargetXml Field :
<Lot><LotQty>5</LotQty></Lot>
<Lot> <LotQty>2</LotQty></Lot>
<Lot><LotQty>3</LotQty> </Lot>
Here the condition to check to map the target field is :
If LotDetails/GCode = "Acceptable" and the corresponding ProductQuantity not (null and zero)
<LotQty> --> 5 [taken from the corresponding ProductQuantity ]
else if LotDetails/GCode = "Ship" and the corresponding ProductQuantity not (null and zero)
<LotQty> --> 2
else if LotDetails/GCode = "Gross" and the corresponding ProductQuantity not (null and zero)
<LotQty> --> 3
else
<LotQty> --> 0
Here the LotQty should happen only once for a particular iteration of <Lot> based on any of the conditions
if any first condition is satisfied then the map LotQTY from the corresponding ProductQuantity ?
When we use for each LotDetails- we get the qty correctly but it produces multiple results for LotQTY inside each loop as below
<Lot>
<LotQty>5</LotQty>
<LotQty>2</LotQty>
<LotQty>3</LotQty>
</Lot>
<Lot>
<LotQty>2</LotQty>
<LotQty>3</LotQty>
</Lot>
<Lot>
<LotQty>3</LotQty>
</Lot>
When i only use IF conditions with out for each the qty is always selected from the first matched condition
<Lot><LotQty>5</LotQty></Lot>
<Lot> <LotQty>5</LotQty></Lot>
<Lot><LotQty>5</LotQty> </Lot
What i need is as below is only one lotQty for each lot based on first satisfied condition with the corresponding qty
<Lot><LotQty>5</LotQty></Lot>
<Lot><LotQty>2</LotQty></Lot>
<Lot><LotQty>3</LotQty></Lot>
Not sure on the template mach usage a being a beginner on xslt usage
如何根据相应的LotDetails / GCode获取正确的LotDetails / ProductQuantity而不使用a? 从if中动态传递满意条件的任何方法,并使用该LotDetails选择正确的ProductQuantity或从当前节点中选择ProductQuantity?
Please let know xslt experts on the best way to do this ?
答案 0 :(得分:0)
考虑以下示例:
<强> XML 强>
<Lots>
<Lot>
<LotDetails>
<GCode>Ship</GCode>
<ProductQuantity>2</ProductQuantity>
</LotDetails>
<LotDetails>
<GCode>Reject</GCode>
<ProductQuantity>4</ProductQuantity>
</LotDetails>
<LotDetails>
<GCode>Gross</GCode>
<ProductQuantity>3</ProductQuantity>
</LotDetails>
<LotDetails>
<GCode>Acceptable</GCode>
<ProductQuantity>5</ProductQuantity>
</LotDetails>
</Lot>
<Lot>
<LotDetails>
<GCode>Ship</GCode>
<ProductQuantity>2</ProductQuantity>
</LotDetails>
<LotDetails>
<GCode>Reject</GCode>
<ProductQuantity>4</ProductQuantity>
</LotDetails>
<LotDetails>
<GCode>Gross</GCode>
<ProductQuantity>3</ProductQuantity>
</LotDetails>
<LotDetails>
<GCode>Acceptable</GCode>
</LotDetails>
</Lot>
<Lot>
<LotDetails>
<GCode>Ship</GCode>
</LotDetails>
<LotDetails>
<GCode>Reject</GCode>
<ProductQuantity>4</ProductQuantity>
</LotDetails>
<LotDetails>
<GCode>Gross</GCode>
<ProductQuantity>3</ProductQuantity>
</LotDetails>
<LotDetails>
<GCode>Acceptable</GCode>
<ProductQuantity>0</ProductQuantity>
</LotDetails>
</Lot>
</Lots>
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="*"/>
<xsl:template match="/Lots">
<xsl:copy>
<xsl:for-each select="Lot">
<xsl:copy>
<Quantity>
<xsl:choose>
<xsl:when test="LotDetails[GCode='Acceptable' and ProductQuantity > 0]">
<xsl:value-of select="LotDetails[GCode='Acceptable']/ProductQuantity"/>
</xsl:when>
<xsl:when test="LotDetails[GCode='Ship' and ProductQuantity > 0]">
<xsl:value-of select="LotDetails[GCode='Ship']/ProductQuantity"/>
</xsl:when>
<xsl:when test="LotDetails[GCode='Gross' and ProductQuantity >0]">
<xsl:value-of select="LotDetails[GCode='Gross']/ProductQuantity"/>
</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</Quantity>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<强>结果强>
<?xml version="1.0" encoding="UTF-8"?>
<Lots>
<Lot>
<Quantity>5</Quantity>
</Lot>
<Lot>
<Quantity>2</Quantity>
</Lot>
<Lot>
<Quantity>3</Quantity>
</Lot>
</Lots>