0

I need to use a XSLT file to transform a xml to csv. Thats easy enough.

I also need to count distinct values from a node and insert them to each line. This where I don't know what to do.

The node I need to count you can see in the example below. It is called < Id >.

I basically want to count all of the id's that are unique, and insert the result into each orderline.

Since my example have two < id > nodes with the value 1 and one < id > node with the value 4, the result would be 2.

Thanks in advance for any help at all.

XML:

<?xml version="1.0" encoding="utf-8"?>
<Picked xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <OrderLines>
        <OrderLine>
            <OrderId>1345
            </OrderId>
            <Missions>
                <Mission>
                    <ContainerIds>
                        <Container>
                            <Id>1
                            </Id>
                        </Container>
                    </ContainerIds>
                </Mission>
            </Missions>
        </OrderLine>
        <OrderLine>
            <OrderId>13456
            </OrderId>
            <Missions>
                <Mission>
                    <ContainerIds>
                        <Container>
                            <Id>1
                            </Id>
                        </Container>
                    </ContainerIds>
                </Mission>
            </Missions>
        </OrderLine>
        <OrderLine>
            <OrderId>134567
            </OrderId>
            <Missions>
                <Mission>
                    <ContainerIds>
                        <Container>
                            <Id>4
                            </Id>
                        </Container>
                    </ContainerIds>
                </Mission>
            </Missions>
        </OrderLine>
    </OrderLines>
</Picked>

Desired result:

    Orderid;Id
    1345;2
    13456;2
    134567;2

XSLT so far:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="text" encoding="UTF-8"/>
    <xsl:template match='/'>

    <xsl:text>Orderid;Id</xsl:text>
    <xsl:text>&#10;</xsl:text>

    <xsl:for-each select="/Picked/OrderLines/OrderLine">
      <xsl:value-of select="OrderId"/>
      <xsl:text>;</xsl:text>
      <xsl:value-of select="Missions/Mission/ContainerIds/Container/Id"/>                           
      <xsl:if test="position()!=last()">
        <xsl:text>&#10;</xsl:text>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

My Result so far:

    Orderid;Id
    1345;1
    13456;1
    134567;4
zx485
  • 28,498
  • 28
  • 50
  • 59
stianzz
  • 49
  • 1
  • 6

2 Answers2

2

XSLT 3.0 solution for comparison:

<xsl:stylesheet version="3.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    expand-text="yes">

    <xsl:output method="text" encoding="UTF-8"/>

    <xsl:template match='/'>
        <xsl:text>Orderid;Id&#10;</xsl:text>
        <xsl:for-each select="/Picked/OrderLines/OrderLine">
           <xsl:text>{OrderId};{count(distinct-values(//Id))}&#10;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
Michael Kay
  • 156,231
  • 11
  • 92
  • 164
1

There are several possibilities in XSLT-1.0 to get distinct values. They are enumerated in this good SO answer.

I used the approach by Nick Grealy, avoiding the use of generate-id(), and counted the resulting nodes.

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="text" encoding="UTF-8"/>

    <xsl:template match='/'>
        <xsl:text>Orderid;Id</xsl:text>
        <xsl:text>&#10;</xsl:text>
        <xsl:for-each select="/Picked/OrderLines/OrderLine">
            <xsl:value-of select="OrderId"/>
            <xsl:text>;</xsl:text>
            <xsl:value-of select="count(//Id[not(.=preceding::*)])"/> <!-- count above all Id's in document -->
            <xsl:if test="position()!=last()">
                <xsl:text>&#10;</xsl:text>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Output:

Orderid;Id
1345;2
13456;2
134567;2
zx485
  • 28,498
  • 28
  • 50
  • 59
  • @MichaelKay: Thanks. I removed the line completely - unnecessary rest of the previous version. – zx485 Sep 14 '17 at 17:26
  • "*avoiding the use of generate-id()*" Why is that a good thing? Read here why it's not: http://www.jenitennison.com/xslt/grouping/muenchian.html – michael.hor257k Sep 14 '17 at 17:37
  • Thanks alot for your answer. This worked perfectly for my example here. However on my real XML file i had to use following count to get it to work: Im not totally sure what was different but thanks again. You pointed me in the right direction. – stianzz Sep 14 '17 at 18:07