检查 xslt 中的条件 [英] check condition in xslt

查看:25
本文介绍了检查 xslt 中的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是输入 XML(小大),抱歉输入 XML 和输出 xml 较大

Below is the input XML (Little Big) sorry for the bigger input XML and as well as output xml

<tutorial>
<lessons>
   <lesson>
     chapter unit 1 page
</lesson>
    <lesson>
        chapter unit 10~ page
    </lesson>
    <lesson>
        chapter unit page
    </lesson>
    <lesson>
        note lesson
    </lesson>
 <lessons1>
    <lesson>
        chapter unit 1 page
    </lesson>
    <lesson>
        description page
    </lesson>
    <lesson>
        chapter unit page
    </lesson>
</lessons1>
</lessons>
</tutorial>

下面是我的输出 Xml

Below is my Output Xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<Geography>


 <historical>
  <social>
     <toc1>
        <toc>
           <chapter>chapter</chapter>
           <unit>unit 1</unit>
           <pages>page</pages>
        </toc>
         <toc>
           <chapter>chapter</chapter>
           <unit>unit 10</unit>
           <pages>page</pages>
        </toc>
         <toc>
           <chapter>chapter</chapter>
           <unit>unit 10</unit>
           <pages>page</pages>
        </toc>
        <toc>
          <sample>
           <original>Note Lesson</orginal>
          </sample>
        </toc>
     </toc1>
     <toc2>
        <toc>
           <chapter>chapter</chapter>
           <unit>unit 1</unit>
           <pages>page</pages>
        </toc>
        <toc>
          <sample>
           <original>description page</orginal>
          </sample>
        </toc>
         <toc>
           <chapter>chapter</chapter>
           <unit>unit 10</unit>
           <pages>page</pages>
        </toc>
     </toc2>
  </social>

很抱歉,这是一个安静的大输出 XML 文件.

It's quiet big output XML file sorry for that.

在单位中,如果我在输出中有单位 1,它将显示为单位 1,但例如,如果我有单位 10~它将显示为单位 10,如果默认情况下没有值,则必须删除 ~ 必须显示单位10.

In unit if i have unit 1 in output it will displayed as unit 1 but for example if i have unit10~ it will displayed as unit 10 the ~ has to removed if there is no value in by default it has to displayed unit 10.

简单说明

我的输出 XML 必须分为三类

My Output XML has to differentiate in three categories

1) 章节

2) 单位

3) 页

输入将采用三种不同类型的格式

The input will be in three different types of formats

1) XML 有章节,单位(带有 tilda 符号的数字)&页

1) The XML have Chapter,unit(number with tilda symbol) & pages

2) XML 有章节,单位(没有波浪号的数字)&页

2) The XML have Chapter,unit(number without tilda symbol) & pages

3) XML 只有页面 ex..(note & description) 所以这里如果例如我有 10~(unit) 输出将显示 10,如果输入 xml 没有值(对于 unit) 在输出 xml 中,它将显示 10 作为默认数字 - 昨天 karthic

3) The XML have only pages ex..(note & description) so here if for example i have 10~(unit) the output will show 10, if the input xml doesn't have the value (for unit) in output xml it will show 10 as default number – karthic yesterday

请在 XSLT 的帮助下帮助和指导我.

Please help me and guide me with the help of XSLT.

问候卡西克

推荐答案

这种转变:

<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 select="lesson"/>
               </toc1>
               <xsl:apply-templates select="lessons1"/>
            </social>
          </historical>
        </Geography>
     </xsl:template>

     <xsl:template match="lesson[starts-with(normalize-space(), 'chapter')]">
      <xsl:variable name="vNorm" select=
                     "translate(normalize-space(), '~', '')"/>
      <xsl:variable name="vAtNumber" select=
                     "substring-after($vNorm, 'chapter unit')"/>
      <xsl:variable name="vNum" select=
       "if(matches($vAtNumber, '^\s*\d+'))
          then replace($vAtNumber, '(^\s*(\d+)).*$', '$2')
          else '10'
       "/>
      <xsl:analyze-string select="."
       regex="(chapter\s+)(unit\s*)(((\d*~?)\s+)?page)">
        <xsl:matching-substring>
          <toc>
             <chapter>chapter</chapter>
             <unit>unit <xsl:value-of select="$vNum"/></unit>
             <pages>page</pages>
          </toc>
        </xsl:matching-substring>
      </xsl:analyze-string>
     </xsl:template>

     <xsl:template match="lesson">
       <sample>
         <original><xsl:value-of select="normalize-space()"/></original>
       </sample>
     </xsl:template>

     <xsl:template match="lessons1">
      <toc2>
       <xsl:apply-templates/>
      </toc2>
     </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<tutorial>
    <lessons>
       <lesson>
         chapter unit 1 page
    </lesson>
        <lesson>
            chapter unit 10~ page
        </lesson>
        <lesson>
            chapter unit page
        </lesson>
        <lesson>
            note lesson
        </lesson>
     <lessons1>
        <lesson>
            chapter unit 1 page
        </lesson>
        <lesson>
            description page
        </lesson>
        <lesson>
            chapter unit page
        </lesson>
    </lessons1>
    </lessons>
</tutorial>

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

<Geography>
   <historical>
      <social>
         <toc1>
            <toc>
               <chapter>chapter</chapter>
               <unit>unit 1</unit>
               <pages>page</pages>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>unit 10</unit>
               <pages>page</pages>
            </toc>
            <toc>
               <chapter>chapter</chapter>
               <unit>unit 10</unit>
               <pages>page</pages>
            </toc>
            <sample>
               <original>note lesson</original>
            </sample>
         </toc1>
         <toc2>
            <toc>
               <chapter>chapter</chapter>
               <unit>unit 1</unit>
               <pages>page</pages>
            </toc>
            <sample>
               <original>description page</original>
            </sample>
            <toc>
               <chapter>chapter</chapter>
               <unit>unit 10</unit>
               <pages>page</pages>
            </toc>
         </toc2>
      </social>
   </historical>
</Geography>

这篇关于检查 xslt 中的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆