如何处理 XSLT 中的处理指令

How to handle Processing instructions in XSLT

我想使用 XSLT 2.0 完成一些任务。我已经用下面的例子解释了我的用法。

输入:

<code>
    <bigin>
        <?codestuct a?>
        <butic>
            <a>a</a>
            <a>b</a>
        </butic>
        <?codestuct c?>
        <butic>
            <a>a</a>
            <a>b</a>
        </butic>
    </bigin>
    <medium>
        <?codestuct a?>
        <super>
            <p>para1</p>
        </super>
        <?codestuct b?>
        <super>
            <p>para2</p>
        </super>
        <?codestuct c?>
        <super>
            <p>para3</p>
        </super>
    </medium>
</code>

如果 <super> 处理指令等于 <butic> 处理指令,我想要添加字符串。

例如,

1st <super> 元素处理指令等于 1st <butic> 处理指令。然后 found 字符串应该打印在输出中。

但是第二个 <super> 元素处理指令不等于任何 <butic> 元素。

预期输出:

<output>
    <extreme>Founded</extreme>
    <extreme>Not Founded</extreme>
    <extreme>Founded</extreme>
</output>

尝试过的代码:

<xsl:template match="medium">
    <output>
        <xsl:choose>
            <xsl:when test="preceding-sibling::bigin/processing-instruction('codestuct') = super/processing-instruction('codestuct')">
                <extreme>
                    <xsl:value-of select="'Founded'"/>
                </extreme>
            </xsl:when>
            <xsl:otherwise>
                <extreme>
                    <xsl:value-of select="'Not Founded'"/>
                </extreme>
            </xsl:otherwise>
        </xsl:choose>
    </output>
</xsl:template>

不太清楚您想比较什么以及元素是否重要但是

  <xsl:template match="code">
      <output>
          <xsl:apply-templates select="medium/processing-instruction()"/>
      </output>
  </xsl:template>
  
  <xsl:template match="medium/processing-instruction()">
      <extreme>Not found</extreme>
  </xsl:template>
  
  <xsl:template match="medium/processing-instruction()[some $pi in /code/bigin/processing-instruction() satisfies deep-equal(., $pi)]">
      <extreme>Found</extreme>
  </xsl:template>

给予

<output>
   <extreme>Found</extreme>
   <extreme>Not found</extreme>
   <extreme>Found</extreme>
</output>

https://xsltfiddle.liberty-development.net/ei5R4uG

一条处理指令有一个名称和一个值。对于 <?codestuct a?>,名称为 codestuct,值为 a。当您使用“=”运算符时,那么(就像元素一样)您只是在比较值,而不是名称。如果希望两部分相等,使用deep-equal(),或者分别比较:name($x) = name($y) and string($x) = string($y)