0

I am new to XML and XSLT and I have an input document, where I want to extract the elements with the prefix p , T and C and count number of serial with root SKU and list. I tried lots of ways but am unable to get it to work and need some help/suggestions.

  • In my XSLT I copy all the elements with the safix ppp to the output file.
  • In my XSLT I copy all the elements with the safix c00 to the output file.
  • In my XSLT I copy all the elements with the saefix t0 to the output file.

Any help is very much appreciated.
Thank you and looking forward for responses.

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <row>
        <product.name>iPhoneX</product.name>
        <category>phone</category>
        <serial>P0001</serial>
    </row>
    <row>
        <product.name>iPadMini</product.name>
        <category>tablet</category>
        <serial>T0001</serial>
    </row>
    <row>
        <product.name>iPadMini</product.name>
        <category>tablet</category>
        <serial>T0002</serial>
    </row>
    <row>
        <product.name>iPhoneX</product.name>
        <category>phone</category>
        <serial>P0002</serial>
    </row>
    <row>
        <product.name>iMacPro</product.name>
        <category>computer</category>
        <serial>C0001</serial>
    </row>
    <row>
        <product.name>iMacPro</product.name>
        <category>computer</category>
        <serial>C0002</serial>
    </row>
    <row>
        <product.name>iPhoneX</product.name>
        <category>phone</category>
        <serial>P0003</serial>
    </row>
    <row>
        <product.name>iPhoneX</product.name>
        <category>phone</category>
        <serial>P0004</serial>
    </row>
    <row>
        <product.name>iPhoneX</product.name>
        <category>phone</category>
        <serial>P0005</serial>
    </row>

The expected output is in the below format:

<?xml version="1.0" encoding="UTF-8"?>
<Inventory>
    <SKU>
        <category>phone</category>
        <product.name>iPhoneX</product.name>
        <product.count>5</product.count>
        <list>P0001</list>
        <list>P0002</list>
        <list>P0003</list>
        <list>P0004</list>
        <list>P0005</list>
    </SKU>
    <SKU>
        <category>tablet</category>
        <product.name>iPadMini</product.name>
        <product.count>2</product.count>
        <list>T0001</list>
        <list>T0002</list>
    </SKU>
    <SKU>
        <category>computer</category>
        <product.name>iMacPro</product.name>
        <product.count>2</product.count>
        <list>C0001</list>
        <list>C0002</list>
    </SKU>
</Inventory>

I tried lots of ways, but am unable to get it to work and need some help/suggestions.

Any help is very much appreciated.
Thank you and looking forward for responses.

What can I do?

  • 1
    I improved the formatting of your XML code, but I couldn't improve the XSLT code, because it is invalid. So, before you can expect a valuable answer, you will have to fix your XSLT code. – zx485 Oct 31 '18 at 19:02
  • This seems like a relatively simple *grouping* problem (do a search). Will the input have any other prefixes besides P, T and C - and if yes, should they be included in the output? – michael.hor257k Oct 31 '18 at 19:19

1 Answers1

1

As indicated in a comment, it seems a simple grouping problem you could tackle in XSLT 2 or 3 with for-each-group group-by:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:param name="search-list" as="xs:string*" select="'P', 'T', 'C'"/>

  <xsl:output indent="yes"/>

  <xsl:template match="root">
      <Inventory>
          <xsl:for-each-group select="row[substring(serial, 1, 1) = $search-list]" group-by="substring(serial, 1, 1)">
              <SKU>
                  <xsl:copy-of select="category, product.name"/>
                  <product.count>
                      <xsl:value-of select="count(current-group())"/>
                  </product.count>
                  <xsl:apply-templates select="current-group()/serial"/>
              </SKU>
          </xsl:for-each-group>
      </Inventory>
  </xsl:template>

  <xsl:template match="serial">
      <list>
          <xsl:apply-templates/>
      </list>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bFDb2Dc

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110