Trying to convert xml to xslt with one default and skipping elements

61 views Asked by At

I looked through the examples here on how to do this, but it doesn't seem to be working for me. I am using Stylus Studio 2011 to write and test my results and my requirement is to transform and xml file into a fixed width.

Here is the source:

> <?xml version="1.0" encoding="UTF-8"?> <PublishNASAMILE
> xmlns="http://www.ibm.com/maximo"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> creationDateTime="2015-06-09T13:48:33-05:00" transLanguage="EN"
> baseLanguage="EN" messageID="1433875714494891022" maximoVersion="7 5
> 20130829-1209 V7510--1" event="0">   <NASAMILESet>
>     <ASSET>
>       <ASSETNUM>G10-1313K</ASSETNUM>
>       <ASSETTYPE maxvalue="PRODUCTION">GSA</ASSETTYPE>
>       <NASAGSAVHCLCLASS>G10</NASAGSAVHCLCLASS>
>       <NASALICENSE>G101313K</NASALICENSE>
>       <NASAODOMETER>73660</NASAODOMETER>
>       <SITEID>MP</SITEID>
>     </ASSET>
>     <ASSET>
>       <ASSETNUM>G10-2465M</ASSETNUM>
>       <ASSETTYPE maxvalue="PRODUCTION">GSA</ASSETTYPE>
>       <NASAGSAVHCLCLASS>G10</NASAGSAVHCLCLASS>
>       <NASALICENSE>G102465M</NASALICENSE>
>       <NASAODOMETER>108590</NASAODOMETER>
>       <SITEID>MP</SITEID>
>     </ASSET>
>     <ASSET>
>       <ASSETNUM>G10-2469M</ASSETNUM>
>       <ASSETTYPE maxvalue="PRODUCTION">GSA</ASSETTYPE>
>       <NASAGSAVHCLCLASS>G10</NASAGSAVHCLCLASS>
>       <NASALICENSE>G102469M</NASALICENSE>
>       <NASAODOMETER>78999</NASAODOMETER>
>       <SITEID>MP</SITEID>
>     </ASSET>    </NASAMILESet> </PublishNASAMILE>

Desired output

32M                         G10 G101313K  73660

32M                         G10 G102465M  108590

32M                         G10 g102469M  78999

The first value is a default value and the tag does not exist in the XML The spacing are: 32M 1-25 G10 26-29 Tag 30-36 Mile 37-42

I have done this from the examples found here on the website

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" encoding="ASCII" method="text"/>
<xsl:strip-space elements=" "/>


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

  <xsl:template match="ASSET">
    <xsl:apply-templates />
    <xsl:text>&#10;</xsl:text>
  </xsl:template>

 <xsl:variable name = "TYPE" select = "'32M'" />

 <xsl:template match="TYPE">
    <xsl:value-of
      select="substring(concat(., '         '), 1, 25)"/>
  </xsl:template>




 <xsl:template match="NASAGSAVHCLCLASS">
    <xsl:value-of
      select="substring(concat(., '                '), 1, 4)"/>
  </xsl:template>

  <xsl:template match="NASALICENSE">
    <xsl:value-of
      select="substring(concat(., '                '), 1, 7)"/>
  </xsl:template>

   <xsl:template match="NASAODOMETER">
    <xsl:value-of
      select="substring(concat(., '                '), 1, 6)"/>
  </xsl:template>

  <xsl:template match="SITEID"/>

</xsl:stylesheet>

and this is my output from this file

{

  G10-1313K
  GSA
  G10
  G101313K
  73660
  MP


  G10-2465M
  GSA
  G10
  G102465M
  108590
  MP


  G10-2469M
  GSA
  G10
  G102469M
  78999
  MP

}

What doesn't seem to be working is each record is not on a single line, I want to skip the first two nodes of the xml and start with the defauted value of 32M and also want to skip the last element "MP".

In my attempt I tried the match without specifying anything to skip nodes I didn't want and I tried to create a variable and match to it, but nothing seems to change in the output.

I'm not well versed in xslt, so help would be greatly appreciated.

1

There are 1 answers

1
michael.hor257k On BEST ANSWER

First thing: if you want fixed-width text output, then don't use the identity transform template - that just makes no sense.

Second thing: the nodes of your input XML are in a namespace; as a result, your templates do not select anything, because they address nodes in no-namespace. The result that you see is produced entirely by the built-in template rules. You would get the same result with a stylesheet with no templates at all.

Now, I couldn't understand your description of the required output: the numbers that you give do not match what you show as the expected result. Try this as your starting point:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mx="http://www.ibm.com/maximo">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:variable name="spaces" select="'                         '"/>

<xsl:template match="/mx:PublishNASAMILE">
    <xsl:for-each select="mx:NASAMILESet/mx:ASSET">
        <xsl:text>32M                         </xsl:text>
        <xsl:value-of select="substring(concat(mx:NASAGSAVHCLCLASS, $spaces), 1, 4)"/>
        <xsl:value-of select="substring(concat(mx:NASALICENSE, $spaces), 1, 10)"/>
        <xsl:value-of select="substring(concat(mx:NASAODOMETER, $spaces), 1, 6)"/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Note the namespace declaration and the use of the associated prefix to select nodes from the input document.

Applied to your input example, the result will be:

32M                         G10 G101313K  73660 
32M                         G10 G102465M  108590
32M                         G10 G102469M  78999