xml - 无法到达 for-each 循环中的特定 xpath 位置
问题描述
在 for-each 循环中,我无法在 xpath 中说出错误:
错误:
XPTY0020: Cannot select a node here: the context item is an atomic value
XML:
<?xml version="1.0" encoding="utf-8" ?>
<CommlCoverage>
<Form>
<FormNumber id="XP149NEW">XP149</FormNumber>
<FormName>XP218 NON-FOLLOWED ENDORSEMENT</FormName>
<FormText QuestionCd="XP149_cov_desc" arrayId="101">XP149 cov desc 1</FormText>
<FormText QuestionCd="XP149_underly_limit" arrayId="101">1000001</FormText>
<FormText QuestionCd="XP149_cov_desc" arrayId="102">XP149 cov desc 2</FormText>
<FormText QuestionCd="XP149_underly_limit" arrayId="102">2000001</FormText>
<FormText QuestionCd="XP149_cov_desc" arrayId="103">XP149 cov desc 3</FormText>
<FormText QuestionCd="XP149_underly_limit" arrayId="103">3000001</FormText>
<FormText QuestionCd="XP149_array">101,102,103</FormText>
</Form>
</CommlCoverage>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="#all" version="2.0">
<xsl:output method="html" indent="yes" html-version="5"/>
<xsl:template match="CommlCoverage">
<html>
<head>
<title>Transformation</title>
</head>
<body>
<xsl:for-each select="tokenize((//Form[FormNumber='XP149']/FormText[@QuestionCd='XP149_array']), ',')">
<!-- Assigning token value in id variable -->
<xsl:variable name="id" select="string(.)"/>
<tr>
<td border="1pt solid" display-align="center">
<xsl:value-of select="."/>
<!-- When i try to use $id in xpath then error is coming as above -->
<!--<xsl:value-of select="//Form[FormText[@QuestionCd='XP149_cov_desc' and @arrayId=$id]]/FormText"/>-->
</td>
<td border="1pt solid" display-align="center">
<xsl:value-of select="."/>
<!-- When i try to use $id in xpath then error is coming as above -->
<!--<xsl:value-of select="//FormText[@QuestionCd='XP149_underly_limit' and @arrayId=$id]"/>-->
</td>
</tr>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
解决方案
或者,它可以通过使用以其他方式完成for-each-group
:
<xsl:for-each-group select="Form/FormText" group-by="@arrayId">
<tr>
<td border="1pt solid" display-align="center">
<xsl:value-of select="current-group()[@QuestionCd='XP149_cov_desc']"/>
</td>
<td border="1pt solid" display-align="center">
<xsl:value-of select="current-group()[@QuestionCd='XP149_underly_limit']"/>
</td>
</tr>
</xsl:for-each-group>
输出:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Transformation</title>
</head>
<body>
<tr>
<td border="1pt solid" display-align="center">XP149 cov desc 1</td>
<td border="1pt solid" display-align="center">1000001</td>
</tr>
<tr>
<td border="1pt solid" display-align="center">XP149 cov desc 2</td>
<td border="1pt solid" display-align="center">2000001</td>
</tr>
<tr>
<td border="1pt solid" display-align="center">XP149 cov desc 3</td>
<td border="1pt solid" display-align="center">3000001</td>
</tr>
</body>
</html>
推荐阅读
- c# - 如何将父级转换为子级以在 C# 中调用正确的函数
- c# - C# - 函数跳过“if”循环
- kentico - 如何在首次使用时重置自动生成的 Kentico 密码?
- javascript - Formik,子组件中的useField()?
- uwp - 错误 APPX1204:无法签署 UWP 项目
- docker - 如何在启用 GUI 的情况下重新启动 ROS docker 容器
- pyspark - AWS 胶水 pyspark - 将源表中的一行转换为目标中的多行
- postgresql - 如何将两个小时添加到时间戳值,然后将其重新转换为仅包含小时和分钟的字符串?
- r - R:计算百分比相对频率并将它们应用于所有列的构建函数
- html - Accordion(仅限 html 和 css)适用于 Chrome,但不适用于 Edge 和 Mozilla