English 中文(简体)
如何根据要素价值选择
原标题:How can query be formed to select based on element value
  • 时间:2011-05-09 22:54:43
  •  标签:
  • xslt
  • select

我们有一份Xml文件,其中附有Invoices/Invoice/[asst. elements],使用我们所需要的xslt呼吁模板,将InvoiceAmounts与InvoiceNumbers相匹配。

投入xml文档:

    <Invoices>   
     <Invoice>  
      <InvoiceNumber>351510</InvoiceNumber> 
      <InvoiceAmount>137.50</InvoiceAmount> 
     </Invoice>   
     <Invoice>  
      <InvoiceNumber>351510</InvoiceNumber> 
      <InvoiceAmount>362.50</InvoiceAmount> 
     </Invoice>   
     <Invoice>  
      <InvoiceNumber>351511</InvoiceNumber> 
      <InvoiceAmount>239.50</InvoiceAmount> 
     </Invoice>  
    </Invoices>

我发现有一份特定声明,即归还整个文件的发票总金额,但需要过滤该数额,以发票为根据,因为发票文件中可能有一个以上发票,有相同的发票。

总计:

<xsl:value-of select="sum(/*[local-name()= Invoices ]/*[local-name()= Invoice ]/*[local-name()= InvoiceAmount ])" />

根据我的研究,下面的询问应当把结果过滤到一个发票号上,但不是。 因此,我们在这一模板中增加了一个参数,即第1页,并安排了以下提问。 是否有另一种方式来安排这种选择,只为那些印花=1美元的人 gr一 total? 在尝试了这方面的许多差异之后,仍然无法取得预期结果。 是否有办法在选择中增加1美元变量以过滤结果?

<xsl:value-of select="sum(/*[local-name()= Invoices ]/*[local-name()= Invoice ][local-name()= InvoiceAmount  = $p1]/*[local-name()= InvoiceAmount ])" />

预期结果:

<Invoices>
 <Invoice>
  <InvoiceNumber>351510</InvoiceNumber>
  <InvoiceAmount>500.00</InvoiceAmount>
 </Invoice>
 <Invoice>
  <InvoiceNumber>351510</InvoiceNumber>
  <InvoiceAmount>500.00</InvoiceAmount>
 </Invoice>
 <Invoice>
  <InvoiceNumber>351511</InvoiceNumber>
  <InvoiceAmount>239.50</InvoiceAmount>
 </Invoice>
</Invoices>

Thank you for your consideration. Tom

问题回答

这一风格:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="InvoiceAmount">
        <InvoiceAmount>
            <xsl:value-of 
               select="sum(../../Invoice[InvoiceNumber=
                   current()/parent::*/InvoiceNumber]/InvoiceAmount)"/>
       </InvoiceAmount>
    </xsl:template>
</xsl:stylesheet>

生产:

<Invoices>   
     <Invoice>  
         <InvoiceNumber>351510</InvoiceNumber> 
         <InvoiceAmount>500</InvoiceAmount> 
     </Invoice>   
     <Invoice>  
         <InvoiceNumber>351510</InvoiceNumber> 
         <InvoiceAmount>500</InvoiceAmount> 
     </Invoice>   
     <Invoice>  
         <InvoiceNumber>351511</InvoiceNumber> 
         <InvoiceAmount>239.5</InvoiceAmount> 
     </Invoice>  
</Invoices>

<>说明: 形式化资金留作一项工作。 见

This transformation can be factors of magnitude more efficient than if keys are not used -- especially with big XML documents:

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

 <xsl:key name="kInvAmmtByNumber" match="InvoiceAmount"
  use="../InvoiceNumber"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="InvoiceAmount/text()">
  <xsl:value-of select=
  "sum(key( kInvAmmtByNumber , ../../InvoiceNumber))"/>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<Invoices>
    <Invoice>
        <InvoiceNumber>351510</InvoiceNumber>
        <InvoiceAmount>137.50</InvoiceAmount>
    </Invoice>
    <Invoice>
        <InvoiceNumber>351510</InvoiceNumber>
        <InvoiceAmount>362.50</InvoiceAmount>
    </Invoice>
    <Invoice>
        <InvoiceNumber>351511</InvoiceNumber>
        <InvoiceAmount>239.50</InvoiceAmount>
    </Invoice>
</Invoices>

the wanted, correct result is produced:

<Invoices>
   <Invoice>
      <InvoiceNumber>351510</InvoiceNumber>
      <InvoiceAmount>500</InvoiceAmount>
   </Invoice>
   <Invoice>
      <InvoiceNumber>351510</InvoiceNumber>
      <InvoiceAmount>500</InvoiceAmount>
   </Invoice>
   <Invoice>
      <InvoiceNumber>351511</InvoiceNumber>
      <InvoiceAmount>239.5</InvoiceAmount>
   </Invoice>
</Invoices>




相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...

热门标签