Нужно сгенерировать электронную таблицу при конвертации XSLT - PullRequest
0 голосов
/ 01 октября 2019

Я хочу создать электронную таблицу со всеми входными элементами и атрибутами, а также с выходными элементами и атрибутами. Здесь я преобразовываю входной xml в docbook xml с помощью Oxygen XML Editor 18.0.

У меня вводится 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>

Я хочу создать электронную таблицу, как показано ниже:

enter image description here

Возможно ли это сделать в редакторе кислорода или любым другим способом? или у нас есть какие-то другие методы для обработки этого? Пожалуйста, предложите

1 Ответ

0 голосов
/ 01 октября 2019

Итак, вы хотите проанализировать код XSLT и вывести таблицу с сопоставлением элементов? Это исключительно на основе кода 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>
...