Home > Software design >  XSLT 1.0 - for-each with a choose variable not giving the right results
XSLT 1.0 - for-each with a choose variable not giving the right results

Time:06-09

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:

<?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?

CodePudding user response:

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>
  • Related