首页 > 解决方案 > 如何处理 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>

举个例子,

第一<super>元素处理指令等于第一<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>

标签: xmlxslt-2.0

解决方案


目前尚不清楚您要比较什么以及这些元素是否重要,但是

  <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


推荐阅读