找到一系列值并使用xsl开头

时间:2014-06-03 10:59:33

标签: xslt range between

我需要询问985000和989999之间的Supplier_ID范围是否在标记AllocationNumber中设置值'FX',否则将其清空。

我想用xsl函数启动来解决它。

但它不起作用(我是XSL的新手)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:user="http://www.altova.com/MapForce/UDF/user" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="user xs fn  user">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/Form">
        <MaxPostInvoice>
            <xsl:attribute name="xsi:noNamespaceSchemaLocation">C:\Program Files (x86)\MaxPostInvoice-v1.18.xsd</xsl:attribute>
            <Header>
                <VendorID>
                    <xsl:for-each select="Fields">
                        <xsl:for-each select="Supplier_ID">
                            <xsl:for-each select="@source">
                                <xsl:attribute name="source"><xsl:value-of select="."/></xsl:attribute>
                            </xsl:for-each>
                        </xsl:for-each>
                    </xsl:for-each>
                    <xsl:for-each select="Fields">
                        <xsl:for-each select="Supplier_ID">
                            <xsl:for-each select="@confidence">
                                <xsl:attribute name="confidence"><xsl:value-of select="."/></xsl:attribute>
                            </xsl:for-each>
                        </xsl:for-each>
                    </xsl:for-each>
                    <xsl:for-each select="Fields">
                        <xsl:for-each select="Supplier_ID">
                            <xsl:for-each select="@rawContents">
                                <xsl:attribute name="rawValue"><xsl:if test="//Fields/Supplier_ID[@rawContents = '']"><xsl:value-of select="''"/></xsl:if><xsl:if test="//Fields/Supplier_ID[@rawContents != 'X']"><xsl:value-of select="."/></xsl:if></xsl:attribute>
                            </xsl:for-each>
                        </xsl:for-each>
                    </xsl:for-each>
                    <xsl:for-each select="Fields">
                        <xsl:for-each select="Supplier_ID">
                            <xsl:for-each select="@contents">
                                <xsl:attribute name="value"><xsl:if test="//Fields/Supplier_ID[@contents = 'X']"><xsl:value-of select="''"/></xsl:if><xsl:if test="//Fields/Supplier_ID[@contents != 'X']"><xsl:value-of select="."/></xsl:if></xsl:attribute>
                            </xsl:for-each>
                        </xsl:for-each>
                    </xsl:for-each>
                </VendorID>
                <AllocationNumber>
                    <xsl:attribute name="rawValue">
                        <xsl:if test="number(./Fields/Supplier_ID/@contents) &gt;= 985000 and number(./Fields/Supplier_ID/@contents) &lt;= 989999">
                            <xsl:value-of select="FX"/>
                        </xsl:if>
                    </xsl:attribute>
                    <xsl:attribute name="value">
                        <xsl:if test="number(./Fields/Supplier_ID/@contents) &gt;= 985000 and number(./Fields/Supplier_ID/@contents) &lt;= 989999">
                            <xsl:value-of select="FX"/>
                        </xsl:if>
                    </xsl:attribute>
                </AllocationNumber>
            </Header>
        </MaxPostInvoice>
    </xsl:template>
</xsl:stylesheet>

这是输入XML:

<Form name="FP0001">
    <Fields>
        <Supplier_ID contents="985000" rawContents="" source="OCR" confidence="100"/>
        <Supplier_Name contents="Leifheitstr" rawContents="" source="OCR" confidence="100"/>
        <Supplier_Street contents="abcde" rawContents="" source="OCR" confidence="100"/>
    </Fields>
</Form>

结果 <Header> <VendorID source="OCR" confidence="100" rawValue="" value="985000 " /> <AllocationNumber rawValue="" /> </Header>

2 个答案:

答案 0 :(得分:1)

您没有提供输入XML,如果您的XPath是正确的,您可以这样做:

<xsl:attribute name="rawValue">
    <xsl:if test="number(./Fields/Supplier_ID/@contents) &gt;= 985000 and number(./Fields/Supplier_ID/@contents) &lt;= 989999">
        <xsl:value-of select="FX"/>
    </xsl:if>
</xsl:attribute>
<xsl:attribute name="rawValue">
    <xsl:if test="number(./Fields/Supplier_ID/@contents) &gt;= 985000 and number(./Fields/Supplier_ID/@contents) &lt;= 989999">
        <xsl:value-of select="FX"/>
    </xsl:if>
</xsl:attribute>

如果不起作用,请分享您的输入XML和更多XSLT(完整)。

答案 1 :(得分:1)

为什么两次具有相同的属性?无论如何,你需要改变:

<xsl:value-of select="FX"/>

为:

<xsl:value-of select="'FX'"/>

顺便说一下,在我看来,你可以将这件事简化为:

<xsl:template match="/Form/Fields">
    <MaxPostInvoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\Program Files (x86)\MaxPostInvoice-v1.18.xsd">
            <Header>
                <VendorID source="{Supplier_ID/@source}" confidence="{Supplier_ID/@confidence}" >
                    <xsl:attribute name="rawValue">
                        <xsl:if test="Supplier_ID/@rawContents != 'X'">
                            <xsl:value-of select="Supplier_ID/@rawContents"/>
                        </xsl:if>
                    </xsl:attribute>
                    <xsl:attribute name="value">
                        <xsl:if test="Supplier_ID/@contents != 'X'">
                            <xsl:value-of select="Supplier_ID/@contents"/>
                        </xsl:if>
                    </xsl:attribute>
                </VendorID>
                <AllocationNumber>
                    <xsl:attribute name="rawValue">
                        <xsl:if test="number(Supplier_ID/@contents) &gt;= 985000 and number(Supplier_ID/@contents) &lt;= 989999">
                            <xsl:value-of select="'FX'"/>
                        </xsl:if>
                    </xsl:attribute>
                </AllocationNumber>
            </Header>
     </MaxPostInvoice>
</xsl:template>