0

I wish to transform an XML message to a CSV file, however I have some requirements of which I'm not sure I'm going to solve using XSLT.

First the original XML file:

<ProductInformation>
  <Products>
    <Product ID="itemid-00001" UserTypeID="objectType" ParentID="objectType2">
      <KeyValue KeyID="keyItemNumber">123456</KeyValue>
      <Name>123456</Name>
      <AttributeLink AttributeID="sizeNumeric" Inherited="2">
                
        <MetaData>
          <Value AttributeID="sequence">2</Value>
          <Value AttributeID="CPS" ID="1">1</Value>
        </MetaData>

      </AttributeLink>
      <AttributeLink AttributeID="primaryColor" Inherited="2">
                
        <MetaData>
          <Value AttributeID="sequence">3</Value>
          <Value AttributeID="CPS" ID="1">1</Value>
        </MetaData>

      </AttributeLink>
      <AttributeLink AttributeID="secondaryColour_" Inherited="2">
                
        <MetaData>
          <Value AttributeID="sequence">10</Value>
        </MetaData>

      </AttributeLink>

      <Values>
        <Value AttributeID="sizeNumeric">0.01</Value>
        <Value AttributeID="secondaryColour_" ID="Red">Red</Value>
      </Values>
    </Product>
  </Products>
</ProductInformation>

From this XML file I wish to retrieve the values for all attributelinks which have a CPS score of 1. I want to create an extra column in my CSV for each attributelink that has a CPS property of 1. Then I want to put the matching values in the correct column. This XML will also contain multiple items which can all have their own unique attributeLinks and corresponding CPS-scores.

In this example the resulting CSV file should look like this;

Item;sizeNumeric;primaryColor
123456;0.01;;

Is something like this possible to do using XSLT?

Thanks in advance!

2
  • So each Product in the XML maps to a line in the CSV? As for the header lines, if "multiple items which can all have their own unique attributeLinks and corresponding CPS-scores" do you simply output a new header line of column names for each Product? Commented Jan 11, 2021 at 7:38
  • @MartinHonnen indeed! Each product will have their own attributeLinks and CPS scores. An item/rproduct can have the same links as a previous one, but these can also differ. I think prefereably I would like to keep adding header columns for each new attributeLink with a CPS score of 1. Commented Jan 11, 2021 at 8:51

1 Answer 1

1

In XSLT 3 you can use e.g.

<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:key name="score" match="Product/AttributeLink/@AttributeID" use="../MetaData/Value[@AttributeID = 'CPS']"/>
    
  <xsl:key name="value" match="Product/Values/Value" use="@AttributeID"/>

  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:value-of 
      select="//Product ! 
              (let $columns := key('score', '1')
              return (
                ('Item', $columns) => string-join(';'),
                (Name, $columns ! string(key('value', .))) => string-join(';')
              ))" separator="&#10;"/>
  </xsl:template>
  
</xsl:stylesheet>

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.