0

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

Scott Don
  • 127
  • 11

1 Answers1

0

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>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thanks @Martin. Yes. It's purely based on the XSLT template match and then output element. It is possible on XSLT 2.0? – Scott Don Oct 01 '19 at 10:02
  • This Error I'm getting `Attribute @item-separator is not allowed on element ` while validating your code on XSLT 3.0 – Scott Don Oct 01 '19 at 10:09
  • @ScottDon, yes, of course, it just needs some more verbosity in the code, you don't have `item-separator` so you will have to output the line breaks explicitly with e.g. ` ` in the templates, the use of the string concatenation operator `||` has to be replaced with the use of the `concat` function and the use of the simple map operator `!` with an XPath 2 `for .. return` expression or an XSLT `for-each`. – Martin Honnen Oct 01 '19 at 10:09
  • @ScottDon, I have added an attempt to transcribe the initial XSLT 3 suggestion to XSLT 2. – Martin Honnen Oct 01 '19 at 10:23
  • I have posted the similar question (https://stackoverflow.com/questions/58215373/need-to-fetch-the-attribute-values-from-the-xsl) . Please see – Scott Don Oct 03 '19 at 08:44