2013-10-26 30 views
0

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

输入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> 

回答

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> 
+0

您给出的解决方案是正确的,但我错了。我没有正确地提到我通过XSL所做的事情现在我已经编辑了它。 – Audy

+0

好吧,但从你的例子中,我不知道你想要完成什么。你能解释一下吗?或者我的示例解决了您的问题? – JLRishe

+0

在这个问题中,我打电话给一个模板进行一些其他工作,这个模板给了我两个节点,在这两个节点中,我想要得到两个“ProvisionalId”值,其中一个值是模板的第一个节点,另一个值是第二个节点的模板,但我正在为模板中的节点获得第一个值。所以我的问题是,我怎样才能避免重复或我怎样才能得到从模板返回的每个节点的单独值? – Audy

相关问题