我正在尝试根据下面的位置从 xml 中获取数据是示例 xml
<node>
<Item>
<ItemName><![CDATA[Name]]></ItemName>
<ItemValue><![CDATA[Value]]></ItemValue>
</Item>
<Item>
<ItemName><![CDATA[Name]]></ItemName>
<ItemValue><![CDATA[Value]]></ItemValue>
</Item>
<Item>
<ItemName><![CDATA[Name]]></ItemName>
<ItemValue><![CDATA[Value3]]></ItemValue>
</Item>
<Item>
<ItemName><![CDATA[Name]]></ItemName>
<ItemValue><![CDATA[Value4]]></ItemValue>
</Item>
<Item>
<ItemName><![CDATA[Name]]></ItemName>
<ItemValue><![CDATA[Value]]></ItemValue>
</Item>
</node>
现在我想根据位置获取并连接 value3 和 value4
这是我在 xslt 中的示例逻辑;
<xsl:for-each select="node/Item">
<xsl:variable name="cellValue">
<xsl:if test="(ItemValue != '')">
<xsl:when test ="position() = 4"/> <!--will skip as it is getting concatenated with pos() 3-->
<xsl:when test ="position() = 3">
<xsl:value-of select="concat(ItemValue,node/Item[position()+1]/ItemValue)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="ItemValue"/>
</xsl:otherwise>
</xsl:if>
</xsl:variable>
<xsl:if test="$cellValue != ''">
<fo:table-row>
<fo:table-cell>
<fo:block>
<xsl:value-of select="$cellValue"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:if>
</xsl:for-each>
我正在使用的命名空间
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/TR/xpath-functions"
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xalan="http://xml.apache.org/xalan"
xmlns:fox="http://xml.apache.org/fop/extensions"
xmlns:pre="xalan://org.apache.xalan.Version"
xmlns:svg="http://www.w3.org/2000/svg">
但是我看到 value3 和 value4 没有在我的输出 pdf 中填充
更新
抱歉给您带来不便:
更新了 xml 层次结构格式
[不幸的是你没有解释你的代码应该做什么..]
**这里我试图从匹配 xpath 的元素中获取值:
node/Item/ItemValue
ItemValue (s) 不是静态的;唯一可以保证的是索引 3 和 4;这是 Item[3] 和 Item[4] 我们总是需要连接 ItemValue (s)。而其余的 ItemValues 可以保持原样。 因此我使用 position() 函数来评估每个
的位置node/Item
那么可能的解决方案是什么?
[你好像误解了 position() 函数的作用。]
**如果可能,请您简要介绍一下职位的用例吗?
你似乎误解了 position() 函数的作用。
不幸的是,你没有解释你的代码应该做什么,并且很难从不满足它们的代码中逆向工程你的需求。
您包含代码片段
node/Item[position()+1]
。这不会选择任何东西。首先,上下文是一个 Item
元素,而您的 Item
元素没有名为 node
的子元素。也许你的意思是parent::node
。其次,数字谓词 P 是 [position()=P]
的缩写,因此您的谓词意味着 [position()=position()+1]
,这永远不会是真的。也许你的意思是following-sibling::Item
.
您可以像这样使用
following-sibling::*[1]
轴:
<xsl:variable name="cellValue">
<xsl:if test="(ItemValue != '')">
<xsl:choose>
<xsl:when test ="position() = 4"/> <!--will skip as it is getting concatenated with pos() 3-->
<xsl:when test ="position() = 3">
<xsl:value-of select="concat(ItemValue, following-sibling::Item[1]/ItemValue)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="ItemValue"/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:variable>
谓词
[1]
表示只有第一个