Highlighted
Knowledge Partner
Knowledge Partner
103 views

XSLT preprocessor help

Jump to solution

Hi,

 

I have a strage xml file I need to read and thought I would use the generic file driver for this.

 

The file looks a bit like this (heavily modified because its too big otherwhise):

<?xml version="1.0" encoding="iso-8859-1"?><primulaAnswer xmlns="http://hr.evry.se" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://hr.evry.se primulaMessage.xsd">
<aPerson fornamn="Anders" version="149">
<anstalldHandlaggare anvandare="anluk" ></anstalldHandlaggare>
<primulaAdress postAnstalt="CITY" postNr="12345"></primulaAdress>
<aAnstallning aktInstKod="1" aNr="1" identity="123">
<anstFriaFalt1 kod="3" </anstFriaFalt1>
<aAnstallningsPeriod fromDat="2017-10-01" grundOmf="100" grupperingsniva="4" huvudanstallning="false" instKod="340" tomDat="2018-09-30">
<arbetsomrade kod="45" text1="45 IT drift, underhåll och support" text2="45 IT drift, underhåll och supportl"></arbetsomrade>
<befattningskod kod="80565" text1="Systemtekniker" text2="Systemtekniker"></befattningskod>
<kompetenskategorier kod="S" text1="Stödkompetens"></kompetenskategorier>
<lart kod="0002" loneform="tv" text="Lön, Individuell"></lart>
</aAnstallningsPeriod>
<aAnstallningsPeriod fromDat="2018-10-01" grundOmf="100" grupperingsniva="4" huvudanstallning="false" instKod="340">
<arbetsomrade kod="45" text1="45 IT drift, underhåll och support" text2="45 IT drift, underhåll och supportl"></arbetsomrade>
<befattningskod kod="80565" text1="Systemtekniker" text2="Systemtekniker"></befattningskod>
<kompetenskategorier kod="S" text1="Stödkompetens"></kompetenskategorier>
<lart kod="0002" loneform="tv" text="Lön, Individuell"></lart>
</aAnstallningsPeriod>
</aAnstallning>
</aPerson>
</primulaAnswer>

 

This is the preProcessor xslt I have so far:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="varUserStart" select="'cn='"/>
<xsl:variable name="varDelimiter" select="'##'"/>
<xsl:variable name="varClassTypeAttr" select="'class_type'"/>
<xsl:variable name="varUserTag" select="'user'"/>
<xsl:template match="*[local-name()='primulaAnswer']">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="*[local-name()='aPerson']">
<record>
<!-- Add the attribute information as new nodes -->
<!-- Cycle around the @ tags, add a <name()> tag in to the XML -->
<xsl:for-each select="@*">
<xsl:variable name="varAttrName" select="name()" />
<!-- The <xsl:element> code actually writes the node -->
<xsl:element name="{$varAttrName}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>

<!-- Now we add the 'class_type' node so we can use it later -->
<xsl:element name="{$varClassTypeAttr}">
<xsl:value-of select="$varUserTag"/>
</xsl:element>
</record>
</xsl:template>
<xsl:template match="*[local-name()='aPerson']/*[local-name()='primulaAdress']">
<record>
<!-- Add the attribute information as new nodes -->
<!-- Cycle around the @ tags, add a <name()> tag in to the XML -->
<xsl:for-each select="@*">
<xsl:variable name="varAttrName" select="name()" />
<!-- The <xsl:element> code actually writes the node -->
<xsl:element name="{$varAttrName}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:element name="{$varClassTypeAttr}">
<xsl:value-of select="$varUserTag"/>
</xsl:element>
</record>
</xsl:template>

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

 

And right now I get the aPerson tag names as attributes.

So I get:

<nds dtdversion="3.0">
<source>
<product build="2018-12-11 22:12:13" instance="Primula" version="1.2.201812112212">Generic File Driver</product>
<contact>VanCauwenberge.info</contact>
</source>
<input>
<add class-name="User" event-id="gfd-pub-1">
<add-attr attr-name="version">
<value>149</value>
</add-attr>
<add-attr attr-name="fornamn">
<value>Anders</value>
</add-attr>
<add-attr attr-name="tilltalsnamn">
<value>Anders</value>
<add-attr attr-name="recordNumber">
<value>1</value>
</add-attr>
<add-attr attr-name="isLastRecord">
<value>true</value>
</add-attr>
</add>
</input>
</nds>

 

But how do I get the inner parts like <befattningskod kod="80565" text1="Systemtekniker" text2="Systemtekniker"></befattningskod>

from aAnstallningsPeriot that does not contain tomDat ?

I feel like im on thin ice here so any pointers on how to go forward is nice.

Another problem is that the same names are used in several tags, like kod. My thought was to create an attrobute with the tag as prefix.

 

/jocke

Labels (1)
0 Likes
1 Solution

Accepted Solutions
Knowledge Partner
Knowledge Partner

Re: XSLT preprocessor help

Jump to solution

Got it to work finally.

0 Likes
2 Replies
Knowledge Partner
Knowledge Partner

Re: XSLT preprocessor help

Jump to solution

Got it to work finally.

0 Likes
Knowledge Partner
Knowledge Partner

Re: XSLT preprocessor help

Jump to solution

Good job, Joakim!

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.