xslt

XSLT - How to loop through each entry, but print out one line


First I have to say, that I'm no expert in XML/XSLT transforming. So please be patient. I have the following XML structure.

<cac:InvoiceLine>
    <cbc:ID>1</cbc:ID>
    <cbc:Note>Some remark text</cbc:Note>
    <cbc:Note>Charge: 1234567*</cbc:Note>
    <cbc:Note>Quantity: 1000*</cbc:Note>
    <cbc:Note>Charge: 987654*</cbc:Note>
    <cbc:Note>Quantity: 5000*</cbc:Note>
    ...
</cac:InvoiceLine>

The Charge and Quantity are connected. Charge 1234567 has a Quantity of 1000 kg. Now I want to loop through the list and print all connected data in one (!) line like:

1000 kg Charge 1234567

5000 kg Charge 987654

I've tried the following code, but actually this code is printing a new line for each <cbc:Note> field.

<xsl:for-each select="./cbc:Note">
  <xsl:choose>
    <xsl:when test="contains(.,'Charge:')">
      <xsl:value-of select="substring-before(substring-after(.,'Charge:'),'*')"/>
      <br />
    </xsl:when>
    <xsl:when test="contains(.,'Menge:')">
      <xsl:value-of select="substring-before(substring-after(.,'Quantity:'),'*')"/>
      <br />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="."/>
      <br />
    </xsl:otherwise>
  </xsl:choose>
</xsl:for-each>

Any ideas how to solve this?


Solution

  • Try something like:

    <xsl:template match="cac:InvoiceLine">
        <xsl:for-each select="cbc:Note[starts-with(., 'Charge: ')]">
            <xsl:value-of select="substring-before(substring-after(following-sibling::cbc:Note[1], 'Quantity: '), '*')"/>
            <xsl:text> kg </xsl:text>
            <xsl:value-of select="substring-before(., '*')"/>
            <br/>
        </xsl:for-each>
    </xsl:template>
    

    You can see it working here.