无法到达 for-each 循环中的特定 xpath 位置

Unable to reach a specific xpath location in for-each loop

在 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 处理 tokenize 调用的结果中,上下文项是一个字符串,如果您需要从文档访问节点声明一个变量,例如<xsl:variable name="main-doc" select="/"/>xsl:for-each 之前,然后,对于任何以 $main-doc 开头的节点选择,例如$main-doc//Form.

或者,可以使用 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>