问题描述:

Source XML:

<A>

<B>

<Yes>Y</Yes>

<Address>

<Type>Present</Type>

<DoorNo>354</DoorNo>

<Line>ABCD</Line>

</Address>

<Address>

<Type>Permanent</Type>

<DoorNo>354</DoorNo>

<Line>ABCD</Line>

</Address>

</B>

<B>

<Yes>N</Yes>

<Address>

<Type>Present</Type>

<DoorNo>354</DoorNo>

<Line>ABCD</Line>

</Address>

<Address>

<Type>Permanent</Type>

<DoorNo>354</DoorNo>

<Line>ABCD</Line>

</Address>

</B>

<D>

<Address>

<Type>Office</Type>

<DoorNo>354</DoorNo>

<Line>ABCD</Line>

</Address>

</D>

</A>

My Code using for-each:

<Address>

<xsl:for-each select="A/B[Yes = 'Y']">

<xsl:for-each select="Address">

<xsl:if test="DoorNo = ../../D/Address/DoorNo">

<xsl:value-of select="Line"/>

</xsl:if>

</xsl:for-each>

</xsl:for-each>

</Address>

output Should be:

<Address>

ABCD

</Address>

As in my If Condition, When the DoorNo of A/B/Address is equal to DoorNo of A/D/Address in turn tag value should be 'Y' of A/B, then the output Must be as follows:

If Present and Permanent address is same then only one of the address should be displayed in output.

else The IF CONDITION has to be satisfied.

From two days i'm trying to replace for-each with recursive templates with a Count variables But i'm not able to get ONE ADDRESS.

I'm trying this with Recursive templates as follows.

if addresses with same DoorNo value are more than '1' Then Count <= 1 should work and only First Address's Line has to be displayed. Multiples address values must be avoided.

Thanks in Advance for those who try solution for this AND

Thanks a Ton for those who solve it.

网友答案:

It's hard to be sure what it is you're trying to do. Is this what you want? This will output one address line for each B element whose [Yes = 'Y']:

<Address> 
<xsl:for-each select="A/B[Yes = 'Y']">
    <xsl:for-each select="Address[DoorNo = ../../D/Address/DoorNo][1]">
        <xsl:value-of select="Line"/>
    </xsl:for-each>
</xsl:for-each>
</Address>
网友答案:

The title of your question seems unrelated to the problem you then describe.

The benefit from using XSLT template rules don't come from the ability to handle one specific source document better, it comes from the reusability of the resulting code across a range of different source documents that vary from each other to a greater or lesser extent. So the question in your title is the wrong question; to know how much effort to put into making a stylesheet general and reusable, we need to know about the class of documents it will have to handle now or in the future, not just about one specific example input.

相关阅读:
Top