Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XSLT确定要选择哪个节点_Xml_Xslt - Fatal编程技术网

Xml XSLT确定要选择哪个节点

Xml XSLT确定要选择哪个节点,xml,xslt,Xml,Xslt,我的xml结构如下所示: <Person> <ID> 123 </ID> <Type> L </Type> </Person> <Person> <ID> 456 </ID> <Type> N </Type> </Person> <Person> <ID> 123 </ID> &

我的xml结构如下所示:

<Person>
  <ID> 123 </ID>
  <Type> L </Type>
</Person>    
<Person>
  <ID> 456 </ID>
  <Type> N </Type>
</Person>    
<Person>
  <ID> 123 </ID>
  <Type> U </Type>
</Person>

123
L
456
N
123
U
人是重复的,无限的,但我只关心这三种类型。我只想以某种方式根据类型进行选择,但以某种方式对其进行优先排序

当类型为L时,我需要始终选择ID,不管还有什么。如果L不存在,选择N,如果N不存在,则选择U。最后,如果U不存在,则根本不输出节点

我尝试过嵌套选择:(很抱歉,如果这在语法上不正确,我会手动键入以消除复杂性

<xsl:choose>
  <xsl:when test = "Person/Type='L'>
    <L-ID>
      <xsl:value-of "Person[Type = 'L']/ID" />
    </L-ID>
  </xsl:when>
  <xsl:otherwise>
      <xsl:when test = "Person/Type='N'>
        <N-ID>
          <xsl:value-of "Person[Type = 'N']/ID" />
        </N-ID>
      </xsl:when>
      <xsl:otherwise>
          <xsl:when test = "Person/Type='U'>
            <U-ID>
              <xsl:value-of "Person[Type = 'U']/ID" />
            </U-ID>
          </xsl:when>
      </xsl:otherwise>
  </xsl:otherwise>
</xsl:choose>


xsl:choose
的语法是

    <xsl:choose>
      <xsl:when test="Person/Type=' L '">
        <L-ID>
          <xsl:value-of select="Person[Type = ' L ']/ID" />
        </L-ID>
      </xsl:when>
      <xsl:when test="Person/Type=' N '">
        <N-ID>
          <xsl:value-of select="Person[Type = ' N ']/ID" />
        </N-ID>
      </xsl:when>
      <xsl:when test="Person/Type=' U '">
        <U-ID>
          <xsl:value-of select="Person[Type = ' U ']/ID" />
        </U-ID>
      </xsl:when>
    </xsl:choose>
</xsl:template>
或者,您可以利用这样一个事实:您要选择的字母是按字母顺序排列的,因此您也可以这样做

<xsl:for-each select="Person[normalize-space(Type) = 'L' or normalize-space(Type) = 'N' or normalize-space(Type) = 'U']">
    <xsl:sort select="Type" />
    <xsl:if test="position() = 1">
        <xsl:element name="{normalize-space(Type)}-ID">
            <xsl:value-of select="ID" />
        </xsl:element>
    </xsl:if>
</xsl:for-each>
      <xsl:when test="Person[normalize-space(Type)= 'L']">
        <L-ID>
          <xsl:value-of select="Person[normalize-space(Type)= 'L']/ID" />
        </L-ID>
      </xsl:when>
<xsl:for-each select="Person[normalize-space(Type) = 'L' or normalize-space(Type) = 'N' or normalize-space(Type) = 'U']">
    <xsl:sort select="Type" />
    <xsl:if test="position() = 1">
        <xsl:element name="{normalize-space(Type)}-ID">
            <xsl:value-of select="ID" />
        </xsl:element>
    </xsl:if>
</xsl:for-each>
<xsl:for-each select="Person[normalize-space(Type) = ('L', 'N', 'U')]">