Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt Xpath来检测子级是否交错_Xslt - Fatal编程技术网

Xslt Xpath来检测子级是否交错

Xslt Xpath来检测子级是否交错,xslt,Xslt,我需要一个XSLT1.0测试表达式,它将指示当前节点t的元素是否完全交错,如下所示 <t> <cat /> <dog /> <horse /> <cat /> <dog /> <horse /> </t> 或者有其他的顺序,比如 <t> <cat /> <cat /> <dog /&

我需要一个XSLT1.0测试表达式,它将指示当前节点t的元素是否完全交错,如下所示

<t>
    <cat />
    <dog />
    <horse />
    <cat />
    <dog />
    <horse />
</t>

或者有其他的顺序,比如

<t>
    <cat />
    <cat />
    <dog />
    <dog />
    <horse />
    <horse />
</t>


如果是第一个元组,则可以有任意数量的此类元组。如果是第二个,则每种类型的子项可以有任意数量(包括零),并且可以是任意顺序

一只猫、一条狗、一匹马的特殊情况可以测试正确或错误,以使算法更容易

我确实事先知道这三个元素的名称


编辑。应迪米特里的要求,让我试着用另一种可能更简单的方式来表达

上下文节点有任意数量的子节点,但每个子节点只有三个名称中的一个。在处理这些子项之前,我需要测试它们是否以重复模式出现,例如a B C a B C a B C C C C B,或C a B C a B,或任何其他重复三元组的组合,三元组中的每一个都出现一次(a B C a B C C测试为真,a B B B B B B B测试为假)

如果交织是完美的(或者只有三项),则返回true


编辑:添加第一个条件以确保最终元组完整,添加三个中间条件以确保重复元组包含三个项中的每一项(即不包含重复项)

如果元组的顺序是固定的,则对于存在1个或多个元组的所有情况,此模板将返回true,否则返回false:

<xsl:template match="t">
  <xsl:sequence
    select="
    count(*) gt 2 and
    count(*) = count(*[
      self::cat   and position() mod 3 = 1 or
      self::dog   and position() mod 3 = 2 or
      self::horse and position() mod 3 = 0])"/>
</xsl:template>

如果元组的顺序可能不同,则对于有1个或多个元组的顺序与元组的第一个实例相同的所有情况,此模板将返回true,否则返回false

<xsl:template match="t">
  <xsl:variable name="cat.pos"   select="(count(cat[1]/preceding-sibling::*) + 1)   mod 3"/>
  <xsl:variable name="dog.pos"   select="(count(dog[1]/preceding-sibling::*) + 1)   mod 3"/>
  <xsl:variable name="horse.pos" select="(count(horse[1]/preceding-sibling::*) + 1) mod 3"/>
  <xsl:sequence
    select="
    count(*) gt 2 and
    count(*) = count(*[
      self::cat   and position() mod 3 = $cat.pos or
      self::dog   and position() mod 3 = $dog.pos or
      self::horse and position() mod 3 = $horse.pos])"/>
</xsl:template>


是否“可以有任何数字”与“我知道三个元素的名称”不矛盾?只有三种元素,我知道它们的名称。但是(在非交错的情况下),每种类型都可以有任意数量的实例。元素是否总是以给定的顺序交错,或者顺序是否可以变化,它们只是必须定期分布?JPM,对不起,但到目前为止,这个问题无法理解。请你编辑一下,准确地定义一下问题,好吗?你说的“交错”是什么意思?“其他订单”呢?你提供的例子对我来说似乎是随意的。请问,总是只有三个元素A B C吗?它可以是A B A B或A B C D A B C D或X Y Z X Y Z?如果元组包含重复项,则此测试不正确。
<xsl:template match="t">
  <xsl:sequence
    select="
    count(*) gt 2 and
    count(*) = count(*[
      self::cat   and position() mod 3 = 1 or
      self::dog   and position() mod 3 = 2 or
      self::horse and position() mod 3 = 0])"/>
</xsl:template>
<xsl:template match="t">
  <xsl:variable name="cat.pos"   select="(count(cat[1]/preceding-sibling::*) + 1)   mod 3"/>
  <xsl:variable name="dog.pos"   select="(count(dog[1]/preceding-sibling::*) + 1)   mod 3"/>
  <xsl:variable name="horse.pos" select="(count(horse[1]/preceding-sibling::*) + 1) mod 3"/>
  <xsl:sequence
    select="
    count(*) gt 2 and
    count(*) = count(*[
      self::cat   and position() mod 3 = $cat.pos or
      self::dog   and position() mod 3 = $dog.pos or
      self::horse and position() mod 3 = $horse.pos])"/>
</xsl:template>