XSLT 转换时需要生成电子表格

Need to generate spreadsheet while XSLT conversion

我想创建包含所有输入元素和属性以及输出元素和属性的电子表格。在这里,我使用 Oxygen XML Editor 18.0.

将输入 xml 转换为文档 xml

我正在输入 xml 比如:

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

我有 XSL:

<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>

我想生成如下电子表格:

是否可以在氧气编辑器或任何其他方式中这样做,或者我们有任何其他方法来处理这个问题?请推荐

所以您要分析 XSLT 代码并输出带有元素映射的 table?是仅基于 XSLT 代码还是还必须处理输入 XML?我想如果使用文字结果元素,那么使用这样一个简单的样式表来处理和读出 match 模式和直接子元素名称是相当容易的,但当然一般来说,如果有一个更复杂的样式表结构与计算元素,称为模板创建映射不会那么容易。

对于直接映射,像你的例子,很容易用XSLT处理XSLT来分析它,比如下面的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>

当 运行 使用 Saxon 9.9 HE 针对您的 XSLT 代码示例时,输出

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

使用 XSLT 2,您可以使用 https://xsltfiddle.liberty-development.net/bFWR5DS 的代码,即

<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>