0

我正在使用 XSl 文件,我在其中从 xml 节点读取值并创建另一个 xml 文件。以下是输入xml的一部分,我想从中一一读取值节点。

输入 XML:

<Param>
  <RoomsInvs>
<RoomInv>
    <ProvisionalId Id="13-0000000007">13-0000000007</ProvisionalId>
</RoomInv>
<RoomInv>
    <ProvisionalId Id="13-0000000008">13-0000000008</ProvisionalId>
</RoomInv>
  </RoomsInvs>
  <Rooms>
<Room NumberOfRooms="1" RoomId="TLC13-000005" Description="AC DEULEX" Twin="no" SupplierCode="RM" SupplierId="0" PropertyId="" ExtraBed="0" RateBasis="-4" Type="" Index="0">
    <Rate Id="0" Currency="INR" Gross="4990.00" DisplayCurrency="INR" DisplayGross="4990.00" Net="4990.00" Tax="0" STax="" ExtraGuestCharge="0" AdultCount="2" AdultRate="1490.00" ChildCount="0" ChildRate=".00" Description="" Status="Available" AllocationDetails="-" SDisc="" SComm="" SRoomTotal="">
        ieiorkmbjfgngjofmgfoikmfhkjg
    </Rate>
</Room>
<Room NumberOfRooms="2" RoomId="TLC13-000005" Description="AC DEULEX" Twin="no" SupplierCode="RM" SupplierId="0" PropertyId="" ExtraBed="0" RateBasis="-4" Type="" Index="0">
    <Rate Id="0" Currency="INR" Gross="4990.00" DisplayCurrency="INR" DisplayGross="4990.00" Net="4990.00" Tax="0" STax="" ExtraGuestCharge="0" AdultCount="2" AdultRate="1490.00" ChildCount="0" ChildRate=".00" Description="" Status="Available" AllocationDetails="-" SDisc="" SComm="" SRoomTotal="">
        dbfbjsdbfjsdfbkjsdfbklnlnlsdf
    </Rate>
</Room>
  </Rooms>
<Param>

Xsl 应用:

<Rooms>
 <xsl:apply-templates select="//Rooms/Room" >
<xsl:with-param name ="IdNo" select ="//RoomsInvs/RoomInv"></xsl:with-param>
 </xsl:apply-templates>
</Rooms>

<xsl:template match="Room" >
<xsl:param name="IdNo"/>
<Room>
    <tempID>
    <xsl:value-of select="$IdNo[position()]/ProvisionalId"/>
    </tempID>
    <xsl:copy-of select="Rate"/>
</Room>

输出获取:

  <Rooms>
<Room>
    <tempID>13-0000000007</tempID>
    <Rate Id="0" Currency="INR" Gross="4990.00" DisplayCurrency="INR" DisplayGross="4990.00" Net="4990.00" Tax="0" STax="" ExtraGuestCharge="0" AdultCount="2" AdultRate="1490.00" ChildCount="0" ChildRate=".00" Description="" Status="Available" AllocationDetails="-" SDisc="" SComm="" SRoomTotal="">
        ieiorkmbjfgngjofmgfoikmfhkjg
    </Rate>
</Room>
<Room>
    <tempID>13-0000000007</tempID>
    <Rate Id="0" Currency="INR" Gross="4990.00" DisplayCurrency="INR" DisplayGross="4990.00" Net="4990.00" Tax="0" STax="" ExtraGuestCharge="0" AdultCount="2" AdultRate="1490.00" ChildCount="0" ChildRate=".00" Description="" Status="Available" AllocationDetails="-" SDisc="" SComm="" SRoomTotal="">
        dbfbjsdbfjsdfbkjsdfbklnlnlsdf
    </Rate>
</Room>
</Rooms> 

我想要的输出:

  <Rooms>
<Room>
    <tempID>13-0000000007</tempID>
    <Rate Id="0" Currency="INR" Gross="4990.00" DisplayCurrency="INR" DisplayGross="4990.00" Net="4990.00" Tax="0" STax="" ExtraGuestCharge="0" AdultCount="2" AdultRate="1490.00" ChildCount="0" ChildRate=".00" Description="" Status="Available" AllocationDetails="-" SDisc="" SComm="" SRoomTotal="">
        ieiorkmbjfgngjofmgfoikmfhkjg
    </Rate>
</Room>
<Room>
    <tempID>13-0000000008</tempID>
    <Rate Id="0" Currency="INR" Gross="4990.00" DisplayCurrency="INR" DisplayGross="4990.00" Net="4990.00" Tax="0" STax="" ExtraGuestCharge="0" AdultCount="2" AdultRate="1490.00" ChildCount="0" ChildRate=".00" Description="" Status="Available" AllocationDetails="-" SDisc="" SComm="" SRoomTotal="">
        dbfbjsdbfjsdfbkjsdfbklnlnlsdf
    </Rate>
</Room>
</Rooms> 
4

1 回答 1

0

问题是您position()在谓词内部(在[]符号之间)使用它具有不同的含义。您应该分配position()给一个变量,然后使用该变量。我相信这会产生预期的结果:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="/">
    <Rooms>
      <xsl:apply-templates select="//Rooms/Room">
        <xsl:with-param name="IdNo" select="//RoomsInvs/RoomInv" />
      </xsl:apply-templates>
    </Rooms>
  </xsl:template>

  <xsl:template match="Room">
    <xsl:param name="IdNo" select="/.." />

    <xsl:variable name="pos" select="position()" />

    <Room>
      <tempID>
        <xsl:value-of select="$IdNo[$pos]/ProvisionalId" />
      </tempID>
      <xsl:copy-of select="Rate" />
    </Room>
  </xsl:template>
</xsl:stylesheet>
于 2013-10-26T10:03:38.280 回答