如何使用文档标签检查查找表标签

时间:2012-05-14 08:01:37

标签: xslt

晕,

我有这个xml文档:

<document>
<Line>
    <Line-Item>
        <ID>5</ID>
        <Quantity>100</Quantity>                
    </Line-Item>
</Line>
<Line>
    <Line-Item>
        <ID>6</ID>
        <Quantity>9</Quantity>              
    </Line-Item>
</Line>
<Line>
    <Line-Item>
        <ID>60</ID>
        <Quantity>3020</Quantity>               
    </Line-Item>
</Line>
</document>

使用表格查找文件:

<lookup>
    <Code>
        <LookupID>5</LookupID>
        <LookupQuantity>25</LookupQuantity>
    </Code>
    <Code>
        <LookupID>6</LookupID>
        <LookupQuantity>3</LookupQuantity>
    </Code>
    <Code>
        <LookupID>70</LookupID>
        <LookupQuantity>3</LookupQuantity>
    </Code>
</lookup>

我应该使用文档Line / Line-Item / ID检查查找表字段查找/ Code / LookupId。如果lookup / Code / LookupId = document / Line / Line-Item / ID,则document / Line / Line-Item / Quantity = document / Line / Line-Item / Quantity div lookup / Code / LookupQuantity,否则为document / Line / Line-项目/数量=文件/线/线-项/数量

需要的结果:

<document>
<Line>
    <Line-Item>
        <ID>5</ID>
        <Quantity>4</Quantity>              
    </Line-Item>
</Line>
<Line>
    <Line-Item>
        <ID>6</ID>
        <Quantity>3</Quantity>              
    </Line-Item>
</Line>
<Line>
    <Line-Item>
        <ID>60</ID>
        <Quantity>3020</Quantity>               
    </Line-Item>
</Line>
</document>

我的xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="date">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

    <xsl:key name="skRez" match="LookupQuantity" use="../LookupID"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Line/Line-Item/Quantity">
        <xsl:variable name="inputS" select="..//ID"/>
        <xsl:variable name="inputQ" select="..//Quantity"/>
            <OrderedQuantity>
                <xsl:for-each select="document('lookup.xml')">
                    <xsl:for-each select="key('skRez',$inputS)">
                        <xsl:variable name="Quantity" select="."/>
                        <xsl:choose>
                            <xsl:when test="$Quantity"><xsl:value-of select="ceiling($inputQ div $Quantity)"/></xsl:when>
                            <xsl:otherwise><xsl:value-of select="$inputQ"/></xsl:otherwise>
                        </xsl:choose>                       
                    </xsl:for-each>
                </xsl:for-each>
            </OrderedQuantity>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

为此,您可以定义一个变量来保存查找数据

<xsl:variable name="lookup" select="document('Lookup.xml')/lookup"/>

然后你可以像这样查找特定Line-Item的数量(在这种情况下,XSLT当前位于Line-Item中的Quantity元素上)

<xsl:variable name="quantity" 
  select="$lookup//Code[LookupID = current()/../ID]/LookupQuantity"/>

如果此变量没有返回任何内容,则表示该元素不在查找

这是完整的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:variable name="lookup" select="document('Lookup.xml')/lookup"/>

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

   <xsl:template match="Line-Item/Quantity">
      <xsl:variable name="quantity" 
         select="$lookup//Code[LookupID = current()/../ID]/LookupQuantity"/>
      <Quantity>
         <xsl:choose>
            <xsl:when test="number($quantity) = number($quantity)">
               <xsl:value-of select="number(.) div number($quantity)"/>
            </xsl:when>
            <xsl:otherwise>
               <xsl:value-of select="."/>
            </xsl:otherwise>
         </xsl:choose>
      </Quantity>
   </xsl:template>
</xsl:stylesheet>

当应用于您的示例XML时,输出以下内容

<document>
   <Line>
      <Line-Item>
         <ID>5</ID>
         <Quantity>4</Quantity>
      </Line-Item>
   </Line>
   <Line>
      <Line-Item>
         <ID>6</ID>
         <Quantity>3</Quantity>
      </Line-Item>
   </Line>
   <Line>
      <Line-Item>
         <ID>60</ID>
         <Quantity>3020</Quantity>
      </Line-Item>
   </Line>
</document>