Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Use if conditions (or when ) for an attribute inside a template in xslt
    text
    copied!<p>HI All,</p> <p>How to use the if condition for an attribute to print different value when its looped everytime. I have an attribute called "AttributeName". This shall take different name when it is looped for each time (total 5 times).</p> <p>I tried few things.. but it didnt work out. My Input XML will not have any data for this. All I have to do is to print different value each time when the loop goes till 5 times.</p> <p>Input XML:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;/Solution&gt; &lt;Solution&gt; &lt;ID&gt;22060000000000000000000002&lt;/ID&gt; &lt;Title&gt;ABCD&lt;/Title&gt; &lt;Observations&gt; &lt;Observation&gt;DEF&lt;/Observation&gt; &lt;/Observations&gt; &lt;ProblemDescription&gt; 1234&lt;/ProblemDescription&gt; &lt;ProblemCause&gt;ADDD&lt;/ProblemCause&gt; &lt;RepairProcedures&gt; &lt;RepairProcedure&gt;XYZ&lt;/RepairProcedure&gt; &lt;/RepairProcedures&gt; &lt;ScenarioExplanation&gt; &lt;Scenario&gt;JIJIJIJIJI&lt;/Scenario&gt; &lt;Scenario&gt;SCENARIO1.&lt;/Scenario&gt; &lt;/ScenarioExplanation&gt; &lt;DocumentReferences&gt; &lt;DocRef&gt;NO DATA&lt;/DocRef&gt; &lt;/DocumentReferences&gt; &lt;/Solution&gt; &lt;/Solutions&gt; </code></pre> <p>XSLT:</p> <p> </p> <p> </p> <pre><code>&lt;xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/&gt; &lt;xsl:stylesheet&gt;&lt;xsl:template&gt; &lt;xsl:element name="Tables"&gt; &lt;!-- Tables code here --&gt; &lt;/xsl:element&gt; &lt;xsl:element name="Relationships"&gt; &lt;xsl:for-each select="Solutions/Solution"&gt; &lt;xsl:if test="RepairProcedures/RepairProcedure!= '' "&gt; &lt;xsl:for-each select="RepairProcedures/RepairProcedure"&gt; &lt;xsl:variable name="vNumCols" select="5"/&gt; &lt;xsl:element name="Relationship"&gt; &lt;xsl:for-each select="position() &amp;lt; vNumCols"&gt; &lt;xsl:attribute name="Action"&gt;&lt;xsl:value-of select="'Insert'"/&gt;&lt;/xsl:attribute&gt; &lt;xsl:attribute name="DestinationKey"&gt;&lt;xsl:value-of select="1"/&gt;&lt;/xsl:attribute&gt; &lt;xsl:attribute name="RelationshipSpec"&gt;&lt;xsl:value-of select="'TableName'"/&gt;&lt;/xsl:attribute&gt; &lt;xsl:attribute name="SourceKey"&gt;&lt;xsl:value-of select="1"/&gt;&lt;/xsl:attribute&gt; &lt;xsl:attribute name="RelCommonKey"&gt;&lt;xsl:value-of select="1"/&gt;&lt;/xsl:attribute&gt; &lt;xsl:attribute name="AttributeName"&gt; &lt;xsl:if test="position() = 1"&gt;&lt;xsl:value-of select="CPComments"/&gt;&lt;/xsl:if&gt; &lt;xsl:if test="position() = 2"&gt;&lt;xsl:value-of select="CPConflict"/&gt;&lt;/xsl:if&gt; &lt;/xsl:attribute&gt; &lt;xsl:attribute name="AttributeValue"&gt;&lt;xsl:value-of select="1"/&gt;&lt;/xsl:attribute&gt; &lt;/xsl:for-each&gt; &lt;/xsl:element&gt; &lt;/xsl:for-each&gt; &lt;/xsl:if&gt; &lt;/xsl:for-each&gt; &lt;/xsl:element&gt; &lt;/xsl:element&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>I am looping through repairprocedures/repairprocedure is to get the "RelationshipSpec" text.</p> <p>Expected output</p> <pre><code>&lt;Relationships&gt; &lt;Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="CPComments" AttributeValue=""/&gt; &lt;Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="CPConflict" AttributeValue=""/&gt; &lt;Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="CPUserID" AttributeValue=""/&gt; &lt;Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="ProcessID" AttributeValue=""/&gt; &lt;Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="ProductId" AttributeValue=""/&gt; &lt;/Relationships&gt; </code></pre> <p>The only value changed in these 5 rows is "AttributeName". I am trying to print 5 rows for one relation with AttributeName being different.</p> <p>Please help me in doing this.</p> <p>I am using XSLT 1.0 </p> <p>Thank you, Ramm</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload