0

这是一个 XML 示例:

<OUT>
    <Header>
        <LineType>H</LineType>
        <DocumentType>OUT</DocumentType>
        <ClientCode>177</ClientCode>
        <OrderNumber>SP03038181</OrderNumber>
        <PayerINN>2636052865</PayerINN>
        <TotalItems>3</TotalItems>
    </Header>
    <Line>
        <LineType>I</LineType>
        <Unit>PCE</Unit>
        <OrderNumber>SP03038181</OrderNumber>
        <Party>A289217</Party>
        <field>20171031</field>
    </Line>
    <Line>
        <LineType>I</LineType>
        <OrderNumber>SP03038181</OrderNumber>
        <Party>0911082</Party>
        <field>20150731</field>
    </Line>
    <Header>
        <LineType>H</LineType>
        <DocumentType>OUT</DocumentType>
        <ClientCode>177</ClientCode>
        <OrderNumber>SP03038322</OrderNumber>
    </Header>
    <Line>
        <LineType>I</LineType>
        <OrderNumber>SP03038322</OrderNumber>
        <Party>Z10208</Party>
        <field>20141231</field>
    </Line>
</OUT>

这是 xslt 代码:

 <xsl:for-each select="*/Header">                 
            <part>
                <header></header>                       
                <content>
                    <OUT>
                        <xsl:copy-of select="."/>
                        <xsl:copy-of select="../Line[OrderNumber = ./OrderNumber]"/>
                    </OUT>
                </content>                  
            </part>
</xsl:for-each>

问题是我无法Line从当前节点复制具有相同订单号的所有Header节点。

<xsl:copy-of select="../Line[OrderNumber = ./OrderNumber]"/>

不起作用,它会复制所有Line节点。

4

3 回答 3

1

尝试将 Line 的副本更改为:

 <xsl:copy-of select="../Line[OrderNumber = current()/OrderNumber]"/>

这将选择 OrderNumber 与current()Header相同的行条目

于 2013-04-29T09:25:44.970 回答
1

那么谓词Line[OrderNumber = ./OrderNumber]与 相同Line[OrderNumber = OrderNumber]。我想一开始你想要Line[current()/OrderNumber = OrderNumber],但我强烈建议使用一个键或每个组来查找匹配的项目。

于 2013-04-29T09:26:31.103 回答
1

试试这个:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="/">
    <xsl:for-each select="*/Header">
      <xsl:variable name="orderNumber" select="OrderNumber"/>
      <part>
        <header></header>                       
        <content>
          <OUT>
            <xsl:copy-of select="."/>
            <xsl:copy-of select="//Line[OrderNumber = $orderNumber]"/>
          </OUT>
        </content>                  
      </part>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

获得输出:

<part>
   <header></header>
   <content>
      <OUT>
         <Header>
            <LineType>H</LineType>
            <DocumentType>OUT</DocumentType>
            <ClientCode>177</ClientCode>
            <OrderNumber>SP03038181</OrderNumber>
            <PayerINN>2636052865</PayerINN>
            <TotalItems>3</TotalItems>
         </Header>
         <Line>
            <LineType>I</LineType>
            <Unit>PCE</Unit>
            <OrderNumber>SP03038181</OrderNumber>
            <Party>A289217</Party>
            <field>20171031</field>
         </Line>
         <Line>
            <LineType>I</LineType>
            <OrderNumber>SP03038181</OrderNumber>
            <Party>0911082</Party>
            <field>20150731</field>
         </Line>
      </OUT>
   </content>
</part>
<part>
   <header></header>
   <content>
      <OUT>
         <Header>
            <LineType>H</LineType>
            <DocumentType>OUT</DocumentType>
            <ClientCode>177</ClientCode>
            <OrderNumber>SP03038322</OrderNumber>
         </Header>
         <Line>
            <LineType>I</LineType>
            <OrderNumber>SP03038322</OrderNumber>
            <Party>Z10208</Party>
            <field>20141231</field>
         </Line>
      </OUT>
   </content>
</part>
于 2013-04-29T09:31:58.447 回答