我对 XSL 完全陌生,正在尝试学习一些东西。假设我有一个这样的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Result>
<Node>
<node-id>1</node-id>
<node-path>2,3</node-path>
</Node>
<Node>
<node-id>2</node-id>
<node-path>2,3,4</node-path>
</Node>
<Node>
<node-id>3</node-id>
<node-path>123,34</node-path>
</Node>
<Node>
<node-id>4</node-id>
<node-path>2124,14,14</node-path>
</Node>
<Node>
<node-id>5</node-id>
<node-path>1,0</node-path>
</Node>
</Result>
我想获取节点路径字段中只有两个值的所有节点,例如:
<?xml version="1.0" encoding="UTF-8"?>
<Result>
<Node>
<node-id>1</node-id>
<node-path>2,3</node-path>
</Node>
<Node>
<node-id>3</node-id>
<node-path>123,34</node-path>
</Node>
<Node>
<node-id>5</node-id>
<node-path>1,0</node-path>
</Node>
</Result>
我将如何在 XSL 中执行此操作?由于我需要复制节点,我发现我必须使用身份转换作为模板。我还看到我们应该使用递归来计算令牌。我想出了这个:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:template name="root-nodes">
<xsl:for-each select="/Result/Node">
<xsl:variable name="path" select="node-path" />
<xsl:call-template name="tokenizer" mode="matcher">
<xsl:with-param name="list" select="$path" />
<xsl:with-param name="delimiter" select="','" />
</xsl:call-template>
</xsl:for-each>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Could not figure out how to write this recursion -->
<xsl:template name="tokenizer" mode="matcher">
<xsl:param name="list"/>
<xsl:param name="delimiter" />
<xsl:value-of select="substring-before($list,$delimiter)" />
<xsl:call-template name="tokenizer">
<xsl:with-param name="list" select="substring-after($list,$delimiter)" />
<xsl:with-param name="delimiter" select="','" />
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
但是我遇到了递归部分的问题。如何计算令牌并确保仅在计数为 2 时才进行身份转换?如何修复递归模板?我现有的“tokenizer”模板有什么问题(它甚至没有给我令牌)?任何其他资源/链接都会非常好。