Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

XSLT 1.0 – for-each with a choose variable not giving the right results

Input:

   <?xml version="1.0" encoding="UTF-8"?>
      <Project ID="123">
        <ProductPool>
          <Product Type="A" ID="123" DueDate="123" Name="ABC"/>
          <Product Type="B" ID="123" DueDate="123" Name="ABC"/>
          <Product Type="A" ID="123" DueDate="123" Name="ABC"/>
          <Product Type="B" ID="123" DueDate="123" Name="ABC"/>
          <Product Type="A" ID="123" DueDate="123" Name="ABC"/>   
        </ProductPool>
      </Project>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <xsl:element name="Product">
            <xsl:attribute name="ID">
                <xsl:value-of select="//Project/@ID"/>
            </xsl:attribute>
                <xsl:variable name="elem-name">
                    <xsl:choose>
                        <xsl:when test='//Product[@Type="A"]'>BoundComponent</xsl:when>
                        <xsl:otherwise>UnboundComponent</xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:for-each select="//Product">
                <xsl:element name="{$elem-name}">
                    <xsl:attribute name="ID">
                        <xsl:value-of select="@Name"/>
                    </xsl:attribute>
                    <xsl:attribute name="DueDate">
                        <xsl:value-of select="@DueDate"/>
                    </xsl:attribute>
                </xsl:element>
                </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?xml version="1.0"?>
<Product ID="123">
  <BoundComponent ID="ABC" Duedate="123"/>
  <BoundComponent ID="ABC" Duedate="123"/>
  <BoundComponent ID="ABC" Duedate="123"/>
  <BoundComponent ID="ABC" Duedate="123"/>
  <BoundComponent ID="ABC" Duedate="123"/>
</Product>

For some Reason, as soon as there is one Type="A" found in the entire Input, all Elements are called BoundComponent, my aim was to seperate the Output though, so each Product found would either be a Element called Bound or Unbound depending on the Type it has in the Input, i couldn’t figure out why that is since its already a for-each loop.
Any Ideas?

>Solution :

Your $elem-name variable is being bound to a value outside the loop. If you put the xsl:variable inside the loop, it will be bound to the appropriate value for each distinct Product. i.e.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <xsl:element name="Product">
            <xsl:attribute name="ID">
                <xsl:value-of select="//Project/@ID"/>
            </xsl:attribute>
            <xsl:for-each select="//Product">
                <xsl:variable name="elem-name">
                    <xsl:choose>
                        <xsl:when test='@Type="A"'>BoundComponent</xsl:when>
                        <xsl:otherwise>UnboundComponent</xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:element name="{$elem-name}">
                    <xsl:attribute name="ID">
                        <xsl:value-of select="@Name"/>
                    </xsl:attribute>
                    <xsl:attribute name="DueDate">
                        <xsl:value-of select="@DueDate"/>
                    </xsl:attribute>
                </xsl:element>
                </xsl:for-each>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading