English 中文(简体)
XSLT 自动编号多级但无结构文件
原标题:XSLT autonumbering multi level but unstructured document
  • 时间:2012-01-11 16:08:01
  •  标签:
  • xml
  • xslt

我有一些XML文件(实际上的XVD),其中载有我想要自动编号的多层次条款;问题是,这些文件是无结构的、与案文属性所示的等级相同的清单。 是的,我知道,但我可以现实地改变它们;太多而且太大。

显示我所希望的结构和标签的简化案例是:

<root>
  <p label="1" class="clause_L1">A</p>
  <p label="1.1" class="clause_L2">B</p>
  <p label="1.2" class="clause_L2">C</p>
  <p label="1.3" class="clause_L2">D</p>
  <p label="2" class="clause_L1">E</p>
  <p label="3" class="clause_L1">F</p>
  <p label="4" class="clause_L1">G</p>
  <p label="4.1" class="clause_L2">H</p>
  <p label="4.1.1" class="clause_L3">I</p>
  <p label="4.1.2" class="clause_L3">J</p>
  <p label="4.2" class="clause_L2">K</p>
  <p label="4.3" class="clause_L2">L</p>
</root>

在经过大量 ha后,我举出以下风格:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml" indent="yes" />

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

<xsl:template match="p[substring(@class, 1, 6) =  clause ]">
   <xsl:variable name="class" select="@class"/>
   <xsl:variable name="level" select="substring(@class, 9, 1)"/>
   <xsl:variable name="parent" select="preceding-sibling::p[@class = concat( clause_L , $level - 1)][1]"/>
   <xsl:variable name="parentPos" select="count($parent/preceding-sibling::p[substring(@class, 1, 6) =  clause ]) + 1"/>

   <clause>
    <xsl:attribute name="parent">
      <xsl:value-of select="$parent/@label"/>
    </xsl:attribute>
    <xsl:attribute name="parentPos">
      <xsl:value-of select="$parentPos"/>
    </xsl:attribute>
    <xsl:attribute name="origLabel">
       <xsl:value-of select="@label"/>
    </xsl:attribute>
    <xsl:attribute name="label">
       <xsl:number count="p[string($parent) =    or position() &gt; $parentPos][@class = $class]" />
    </xsl:attribute>
    <xsl:value-of select="."/>
   </clause>
</xsl:template>

......这给我提供了目前最低水平的正确数字:

<clause parent="" parentPos="1" origLabel="1" label="1">A</clause>
<clause parent="1" parentPos="1" origLabel="1.1" label="1">B</clause>
<clause parent="1" parentPos="1" origLabel="1.2" label="2">C</clause>
<clause parent="1" parentPos="1" origLabel="1.3" label="3">D</clause>
<clause parent="" parentPos="1" origLabel="2" label="2">E</clause>
<clause parent="" parentPos="1" origLabel="3" label="3">F</clause>
<clause parent="" parentPos="1" origLabel="4" label="4">G</clause>
<clause parent="4" parentPos="7" origLabel="4.1" label="1">H</clause>
<clause parent="4.1" parentPos="8" origLabel="4.1.1" label="1">I</clause>
<clause parent="4.1" parentPos="8" origLabel="4.1.2" label="2">J</clause>
<clause parent="4" parentPos="7" origLabel="4.2" label="2">K</clause>
<clause parent="4" parentPos="7" origLabel="4.3" label="3">L</clause>

首先,在这一模板中可以产生较高的贴标签数量,因为标识似乎为xsl:编号只是目前情况下的工作——因此,我不知道如何使目前的下一个水平出现反响。

第二,有更好的办法完全停止这种做法? 我铭记着源数据格式。

问题回答

解决该问题,以合理的内 ne和一般方式,进行再入侵,从而应对任意程度。

对于那些试图从公寓中产生结构化文件的人来说,这也可以作有益的修改。

纽约总部 业绩可能令人厌恶,但这不是一个高量估计,因此我不关心;o)

www.un.org/Depts/DGACM/index_spanish.htm 实际表现是迅速的,因此,我以更优化的方式更新了答案;造成业绩问题的实际事情是微不足道的,在法典中作了评论,我假定,这是与 la评价如何奏效。 如果不进行预先评价,全休的补休时间为一分钟半,以掌握我的数据(约400项条款),共耗时5秒。 I m 采用XSLT发动机在4.NET中。 有5个字眼亮了,但这只是整个运行,包括产生、下载和浏览,使人民抵抗力量的最后文件成为可能。

根据上述资料来源XML,XSL是:

<xsl:template match="p[substring(@class, 1, 6) =  clause ]">
   <clause>
    <xsl:attribute name="origLabel">
         <xsl:value-of select="@label"/>
      </xsl:attribute>
    <xsl:attribute name="label">
       <xsl:call-template name="makeLabel">
           <xsl:with-param name="node" select="."/>
        </xsl:call-template>
     </xsl:attribute>
     <xsl:value-of select="."/>
   </clause>
</xsl:template>


<xsl:template name="makeLabel">
   <xsl:param name="node"/>

   <xsl:variable name="class" select="$node/@class"/>
   <xsl:variable name="level" select="substring($class, 9, 1)"/>
   <xsl:variable name="parent" select="$node/preceding-sibling::p[@class = concat( clause_L , $level - 1)][1]"/>
   <xsl:variable name="parentPos" select="count($parent/preceding-sibling::p[substring(@class, 1, 6) =  clause ]) + 1"/>
   <xsl:variable name="myPos" select="count($node/preceding-sibling::p[substring(@class, 1, 6) =  clause ]) + 1"/>
   <xsl:variable name="precPos" select="number($myPos - $parentPos)"/>
   <xsl:variable name="topLevel" select="not($parent)"/>

   <xsl:if test="string($parent) !=   ">
       <xsl:call-template name="makeLabel">
           <xsl:with-param name="node" select="$parent"/>
        </xsl:call-template>
   </xsl:if>

   <xsl:choose>
    <xsl:when test="$topLevel">
      <xsl:value-of select="count($node/preceding-sibling::p[@class = $class]) + 1" />
    </xsl:when>
    <xsl:otherwise>
      <!--
        It is ABSOLUTELY REQUIRED to pre-evaluate $precPos before the count(); without this the request takes over a minute to run; 
        with it, it takes ~5 seconds.
      -->
      <xsl:comment><xsl:value-of select="$precPos"/></xsl:comment>
      <!-- -->
      <xsl:value-of select="count($node/preceding-sibling::p[position() &lt; $precPos][@class = $class]) + 1" />
    </xsl:otherwise>
   </xsl:choose>    
   <xsl:text>.</xsl:text>
</xsl:template>

产生预期结果:

<clause origLabel="1" label="1.">A</clause>
<clause origLabel="1.1" label="1.1.">B</clause>
<clause origLabel="1.2" label="1.2.">C</clause>
<clause origLabel="1.3" label="1.3.">D</clause>
<clause origLabel="2" label="2.">E</clause>
<clause origLabel="3" label="3.">F</clause>
<clause origLabel="4" label="4.">G</clause>
<clause origLabel="4.1" label="4.1.">H</clause>
<clause origLabel="4.1.1" label="4.1.1.">I</clause>
<clause origLabel="4.1.2" label="4.1.2.">J</clause>
<clause origLabel="4.2" label="4.2.">K</clause>
<clause origLabel="4.3" label="4.3.">L</clause>




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签