如何使用xsl数来计算组中的元素

时间:2013-04-11 03:36:11

标签: xslt

我将按顺序计算proceduralStep,如下所示。我不确定如何实施。

XML:

<mainProcedure>
    <proceduralStepAlts>
        <proceduralStep/>
        <proceduralStep/>
    </proceduralStepAlts>
    <proceduralStep/>
    <proceduralStepAlts>
        <proceduralStep>
            <proceduralStepAlts>
                <proceduralStep/>
                <proceduralStep/>
            </proceduralStepAlts>
        </proceduralStep>
        <proceduralStep/>
    </proceduralStepAlts>
</mainProcedure>

对架构有效:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:xlink="http://www.w3.org/1999/xlink"  xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="mainProcedure" type="mainProcedureElemType"/>
    <xs:complexType name="mainProcedureElemType">
        <xs:choice maxOccurs="unbounded">
            <xs:element ref="proceduralStep"/>
            <xs:element ref="proceduralStepAlts"/>
        </xs:choice>
    </xs:complexType>
    <xs:element name="proceduralStep" type="proceduralStepElemType"/>
    <xs:complexType name="proceduralStepElemType">
        <xs:sequence>
            <xs:element ref="title"/>
            <xs:choice maxOccurs="unbounded" minOccurs="0">
                <xs:element ref="proceduralStep"/>
                <xs:element ref="proceduralStepAlts"/>
            </xs:choice>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="proceduralStepAlts" type="proceduralStepAltsElemType"/>
    <xs:complexType name="proceduralStepAltsElemType">
        <xs:sequence>
            <xs:element maxOccurs="unbounded" ref="proceduralStep"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

所需的Oupput:         1         1         2         3         3.1         3.1         3.2         3         4

为什么需要输出:proceduralStepAlts包装多个程序步骤,该组被认为只是“一个”,因此每个程序步骤都显示相同的轮廓号。

3 个答案:

答案 0 :(得分:0)

此样式表:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>

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

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

    <xsl:template match="proceduralStep[parent::proceduralStepAlts]">
        <xsl:value-of select="
                count(parent::proceduralStepAlts/preceding-sibling::proceduralStepAlts)
              + count(parent::proceduralStepAlts/preceding-sibling::proceduralStep)
              + 1"/>                
        <xsl:text> </xsl:text>
    </xsl:template>

    <xsl:template match="proceduralStep[parent::mainProcedure]">
        <xsl:value-of select="
              count(preceding-sibling::proceduralStepAlts)
            + count(preceding-sibling::proceduralStep)
            + 1"/>                
        <xsl:text> </xsl:text>
    </xsl:template>

</xsl:stylesheet>

使用提供的输入生成所需的输出。

答案 1 :(得分:0)

此转化

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

 <xsl:template match="proceduralStepAlts/proceduralStep">
  <xsl:value-of select=
  "concat(count(../preceding-sibling::*) +1, ' ')"/>
 </xsl:template>

 <xsl:template match="/*/proceduralStep">
  <xsl:value-of select=
   "concat(count(preceding-sibling::*) +1, ' ')"/>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档时:

<mainProcedure>
    <proceduralStepAlts>
        <proceduralStep/>
        <proceduralStep/>
    </proceduralStepAlts>
    <proceduralStep/>
    <proceduralStepAlts>
        <proceduralStep/>
        <proceduralStep/>
    </proceduralStepAlts>
</mainProcedure>

产生想要的结果:

1 1 2 3 3 

第二个解决方案

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

 <xsl:template match="/*">
   <xsl:apply-templates select="*[self::proceduralStep or proceduralStep]"/>
 </xsl:template>

 <xsl:template match="proceduralStep | proceduralStepAlts">
  <xsl:variable name="vPos" select="position()"/>

  <xsl:for-each select="self::proceduralStep | proceduralStep">
   <xsl:value-of select="concat($vPos, ' ')"/>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于同一XML文档(上图)时,会产生相同的正确结果

1 1 2 3 3 

答案 2 :(得分:0)

我认为你只是计算根元素下面的元素,所以你可以使用这个表达式

<xsl:number count="/*/*" />

这是'完整的'XSLT

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

   <xsl:template match="proceduralStep">
      <xsl:number count="/*/*" />
   </xsl:template>
</xsl:stylesheet>

应用于XML时,输出以下内容:

11233