使用 XSLT 获得所需的输出

Using XSLT for desired output

我关注XML

<Name>
  <Person>
    <Car>
      <brand>Merc</brand>
    </Car>
    <Car>
      <brand>Merc</brand>
    </Car>
  </Person>
  <Person>
    <Car>
      <brand>Audi</brand>
    </Car>
  </Person>
</Name>

我想要的输出是

<People>
  <Resident>
   <Number>1</Number>
   <Vehicle>
     <CarNumber>1</CarNumber>
     <CarNumber>2</CarNumber>
  </Vehicle>
 </Resident>
 <Resident>
   <Number>2</Number>
   <Vehicle>
     <CarNumber>3</CarNumber>
   </Vehicle>
 </Resident>
</People>

这里请注意第二个Resident中的CarNumber是3

为此我有以下 XSLT

<People>    
  <xsl:for-each select="Name/Person">
    <Resident>
      <Number><xsl:value-of select="position()"/></Number>
    </Resident>
    <Vehicle>
      <xsl:for-each select="Car/brand">
        <CarNumber><xsl:value-of select="position()"/><CarNumber>
      </xsl:for-each>
    </Vehicle>
 </xsl:for-each>

但我得到了

<People>
<Resident>
    <Number>1</Number>
    <Vehicle>
        <CarNumber>1</CarNumber>
        <CarNumber>2</CarNumber>
    </Vehicle>
</Resident>
<Resident>
    <Number>2</Number>
    <Vehicle>
        <CarNumber>1</CarNumber>
    </Vehicle>
</Resident>

我是否可以使用 XSLT 转换它?

在这里,我得到的是 1,而不是 3 作为 CarNumber。

试试这个方法:

<xsl:template match="/">
    <People>    
        <xsl:for-each select="Name/Person">
            <Resident>
                <Number><xsl:number/></Number>
            </Resident>
            <Vehicle>
                <xsl:for-each select="Car">
                    <CarNumber><xsl:number level="any"/></CarNumber>
                </xsl:for-each>
            </Vehicle>
        </xsl:for-each>
    </People>   
</xsl:template>