Search code examples
xmlxsltspreadsheetoxygenxml

Need to generate spreadsheet while XSLT conversion


I want to create the spreadsheet of all input elements and attributes along with output elements and attributes. Here I'm converting input xml to docbook xml by using Oxygen XML Editor 18.0.

I'm having input xml like:

<Section1>
   <Section1Heading>Section 1</Section1Heading>
  <Section2>
    <Section2Heading>Heading</Section2Heading>
    <Para>other.</Para>
  </Section2>
</Section1>

XSL I'm having:

<xsl:template match="Section1">
   <sect1>
      <xsl:apply-templates/>
   </sect1>
</xsl:template>

<xsl:template match="Section1Heading | Section2Heading">
    <title>
        <xsl:apply-templates/>
    </title>
</xsl:template>

<xsl:template match="Section2">
   <sect2>
      <xsl:apply-templates/>
   </sect2>
</xsl:template>

<xsl:template match="Para">
   <para>
      <xsl:apply-templates/>
   </para>
</xsl:template>

I want to generate the spreadsheet like below:

enter image description here

Is this possible to do like this in the Oxygen Editor or any other way or we having any some other methods to do handling this? Please suggest


Solution

  • So you are looking to analyze the XSLT code and to output a table with the element mapping? Is that solely based on the XSLT code or does the input XML also have to be processed? I guess it is rather easy with such a simple stylesheet to process and read out the match patterns and the immediate child element name if literal result elements are used but of course in general if there is a more complicated stylesheet structure with computed elements, called templates creating the mapping will not be that easy.

    For a direct mapping, like your sample, it is easy to process XSLT with XSLT to analyze it, for instance, the following XSLT 3

    <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="sep" as="xs:string">,</xsl:param>
    
        <xsl:mode on-no-match="shallow-skip"/>
    
        <xsl:output method="text" item-separator="&#10;"/>
    
        <xsl:template match="/">
            <xsl:sequence select="'Input XML' || $sep || 'Result XML'"/>
            <xsl:apply-templates/>
        </xsl:template>
    
        <xsl:template match="xsl:template[@match]">
            <xsl:sequence 
                select="tokenize(@match, '\s*\|\s*') ! 
                (. || $sep || node-name(current()/*[1]))"/>
        </xsl:template>
    
    </xsl:stylesheet>
    

    when run with Saxon 9.9 HE against your XSLT code sample, outputs

    Input XML,Result XML
    Section1,sect1
    Section1Heading,title
    Section2Heading,title
    Section2,sect2
    Para,para
    

    With XSLT 2 you can the code of https://xsltfiddle.liberty-development.net/bFWR5DS i.e.

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        exclude-result-prefixes="#all"
        version="2.0">
    
      <xsl:param name="field-sep" as="xs:string">,</xsl:param>
      <xsl:param name="line-sep" as="xs:string" select="'&#10;'"/>
    
      <xsl:output method="text"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="/">
          <xsl:value-of select="concat('Input XML', $field-sep, 'Result XML')"/>
          <xsl:value-of select="$line-sep"/>
          <xsl:apply-templates/>
      </xsl:template>
    
      <xsl:template match="xsl:template[@match]">
        <xsl:value-of 
           select="for $pattern in tokenize(@match, '\s*\|\s*')
                   return concat($pattern, $field-sep, node-name(current()/*[1]))"
           separator="{$line-sep}"/>
           <xsl:value-of select="$line-sep"/>
      </xsl:template>
    
    </xsl:stylesheet>