检查XML是否有两种不同类型的元素节点,如何使用XSLT将其引入输出XML

时间:2021-04-15 14:33:59

I already have an input XML

我已经有了输入XML

<tutorial>
<lessons>
<lesson>
     chapter1 unit 1 page1
</lesson>
<lesson>
     unit 1 
</lesson>
</lessons>
</tutorial>

The output should be

输出应该是

<Geography>
<historical>
    <social>
       <toc1>
     <toc>
      <chapter>
    chapter1
      <chapter>
      <unit>
    unit 1
      </unit>
      <pages>
    page1
      </pages>
      </toc>
       </toc1>
    <social>
</historical>

actually i am getting confused here

实际上我在这里感到困惑

 <lesson>
chapter1 unit 1 page1
</lesson>
<lesson>
 unit 1 
</lesson>

here i need two outpus

在这里我需要两次出局

for the first lesson i need it as above output

对于第一课,我需要它作为上面的输出

for the second lesson i need it as output like below

对于第二课,我需要它作为输出如下

 <historical>
    <social>
       <toc1>
  <toc>
      <unit>
    unit 1
      </unit>   
  <toc>
       </toc1>
    <social>
</historical>

but sometimes i will get both type in xml i am totally confused how to do this.

但有时我会得到两种类型的xml我完全混淆如何做到这一点。

can any one guide me here it can be in both XSLT1.0 and XSLT2.0

任何人都可以在这里指导它,它可以在XSLT1.0和XSLT2.0中

Regards Karthic

关心Karthic

1 个解决方案

#1


1  

This XSLT 2.0 transformation:

这个XSLT 2.0转换:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

  <xsl:variable name="vNames" select="'chapter', 'unit', 'pages'"/>

 <xsl:template match="lessons">
    <Geography>
      <historical>
        <social>
           <toc1>
             <xsl:apply-templates/>
           </toc1>
        </social>
      </historical>
    </Geography>
 </xsl:template>

 <xsl:template match="lesson[matches(., '(chapter\s*\d+)?\s+(unit\s*\d+)\s+(page\s*\d+)?')]">
  <xsl:analyze-string select="."
   regex="(chapter\s*\d+)?\s+(unit\s*\d+)\s+(page\s*\d+)?">
    <xsl:matching-substring>
      <toc>
         <xsl:for-each select="1 to 3">
          <xsl:if test="regex-group(current())">
           <xsl:element name="{$vNames[current()]}">
                <xsl:sequence select="regex-group(current())"/>
           </xsl:element>
          </xsl:if>
         </xsl:for-each>
      </toc>
    </xsl:matching-substring>
  </xsl:analyze-string>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

当应用于提供的XML文档时:

<tutorial>
    <lessons>
    <lesson>
         chapter1 unit 1 page1
    </lesson>
    <lesson>
         unit 1
    </lesson>
    </lessons>
</tutorial>

produces the wanted, correct result:

产生想要的,正确的结果:

<Geography>
  <historical>
    <social>
      <toc1>
        <toc>
          <chapter>chapter1</chapter>
          <unit>unit 1</unit>
          <pages>page1</pages>
        </toc>
        <toc>
          <unit>unit 1</unit>
        </toc>
      </toc1>
    </social>
  </historical>
</Geography>

Explanation:

说明:

Proper use of XSLT 2.0 Regular expression capabilities such as:

正确使用XSLT 2.0正则表达式功能,例如:

  1. The <xsl:analyze-string> and <xsl:matching-substring> instrunctions.

    instrunctions。 :matching-substring> :analyze-string>

  2. The regex-group() function.

    regex-group()函数。

#1


1  

This XSLT 2.0 transformation:

这个XSLT 2.0转换:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

  <xsl:variable name="vNames" select="'chapter', 'unit', 'pages'"/>

 <xsl:template match="lessons">
    <Geography>
      <historical>
        <social>
           <toc1>
             <xsl:apply-templates/>
           </toc1>
        </social>
      </historical>
    </Geography>
 </xsl:template>

 <xsl:template match="lesson[matches(., '(chapter\s*\d+)?\s+(unit\s*\d+)\s+(page\s*\d+)?')]">
  <xsl:analyze-string select="."
   regex="(chapter\s*\d+)?\s+(unit\s*\d+)\s+(page\s*\d+)?">
    <xsl:matching-substring>
      <toc>
         <xsl:for-each select="1 to 3">
          <xsl:if test="regex-group(current())">
           <xsl:element name="{$vNames[current()]}">
                <xsl:sequence select="regex-group(current())"/>
           </xsl:element>
          </xsl:if>
         </xsl:for-each>
      </toc>
    </xsl:matching-substring>
  </xsl:analyze-string>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

当应用于提供的XML文档时:

<tutorial>
    <lessons>
    <lesson>
         chapter1 unit 1 page1
    </lesson>
    <lesson>
         unit 1
    </lesson>
    </lessons>
</tutorial>

produces the wanted, correct result:

产生想要的,正确的结果:

<Geography>
  <historical>
    <social>
      <toc1>
        <toc>
          <chapter>chapter1</chapter>
          <unit>unit 1</unit>
          <pages>page1</pages>
        </toc>
        <toc>
          <unit>unit 1</unit>
        </toc>
      </toc1>
    </social>
  </historical>
</Geography>

Explanation:

说明:

Proper use of XSLT 2.0 Regular expression capabilities such as:

正确使用XSLT 2.0正则表达式功能,例如:

  1. The <xsl:analyze-string> and <xsl:matching-substring> instrunctions.

    instrunctions。 :matching-substring> :analyze-string>

  2. The regex-group() function.

    regex-group()函数。