我有以下模板:
<xsl:template match="footnote">
<xsl:variable name = "string">
<xsl:value-of select="."/>
</xsl:variable>
<xsl:variable name = "bool">
<xsl:if test="$string = preceding-sibling::node()/$string">
<xsl:text>false</xsl:test>
</xsl:if>
</xsl:variable>
<xsl:if test="$bool != 'false'">
<!-- DO STUFF -->
</xsl:if>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
我正在尝试检查当前节点的 $string 变量,并将其与所有以前的脚注节点进行比较,以查看它们是否具有相同的 $string 变量。如果它与任何前面的兄弟姐妹不匹配,那么它应该做一些事情,否则它什么都不做。
使用我拥有的代码,即使尚未创建脚注节点,测试“$string =previous-sibling::node()/$string”也始终评估为真。
谁能帮我吗?我很难创建表达式来与所有以前的兄弟姐妹中的变量进行比较。
编辑:示例 XML:
<xml>
<footnote>Footnote 1</footnote>
<footnote>Footnote 2</footnote>
<footnote>Footnote 1</footnote>
<footnote>Footnote 1</footnote>
<footnore>Footnote 3</footenote>
</xml>
Run Code Online (Sandbox Code Playgroud)
我正在尝试将其转换为:
<xml>
<footnote>Footnote 1</footnote>
<footnote>Footnote 2</footnote>
<footnote>Footnore 3</footnore>
</xml
Run Code Online (Sandbox Code Playgroud)
好。发布您的输出 XML 使其更加清晰!你去解决方案:
示例 XML:
<?xml version="1.0" encoding="utf-8"?>
<root>
<footnote>Matching</footnote>
<footnote>Example1</footnote>
<footnote>Matching</footnote>
<footnote>Example2</footnote>
<footnote>Example3</footnote>
</root>
Run Code Online (Sandbox Code Playgroud)
和 XSL:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="footnote[.=preceding-sibling::footnote/.]"/>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
结果:
<?xml version="1.0" encoding="utf-8"?>
<xml>
<footnote>Matching</footnote>
<footnote>Example1</footnote>
<footnote>Example2</footnote>
<footnote>Example3</footnote>
</xml>
Run Code Online (Sandbox Code Playgroud)