如何在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>处理指令,我想添加字符串。

例如,

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

相关内容

最新更新