XSLT中的多个条件无法正常工作

时间:2017-01-19 15:52:50

标签: xml xslt

我有以下XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<bp_list xmlns="http://example.com/2012/03/01/canonical/BusinessPartner">
<total_count>221</total_count>
<bp>
    <bp_id>10</bp_id>
    <bp_name>aad</bp_name>
    <last_changed_date>2016-12-20T02:38:14</last_changed_date>
    <location>
        <location_id>200020</location_id>
        <location_name>gfh</location_name>
        <is_location_owner>true</is_location_owner>
        <location_registration_status>
            <registration_status_id>7202</registration_status_id>
            <registration_status_name>Unregistered</registration_status_name>
            <registration_status_effective_date>2015-07-24T00:00:00</registration_status_effective_date>
        </location_registration_status>
        <location_status>
            <name>In Progress</name>
        </location_status>
        <marked_for_delete>false</marked_for_delete>
        <bp_location>
            <bp>
                <bp_id>10</bp_id>
            </bp>
            <bp_location_capability>
                <is_primary_capability>true</is_primary_capability>
                <location_function_capability_category_id>50</location_function_capability_category_id>
                <location_function_capability_name>hq</location_function_capability_name>
            </bp_location_capability>
            <bp_location_capability>
                <is_primary_capability>true</is_primary_capability>
                <location_function_capability_category_id>53</location_function_capability_category_id>
                <location_function_capability_name>Remit To</location_function_capability_name>
            </bp_location_capability>
        </bp_location>
        <address>
            <id>103754</id>
            <line1>someadress</line1>
            <city>Columbus</city>
            <postal_code>47201</postal_code>
            <country_code>US</country_code>
            <country_name>UNITED STATES</country_name>
            <subdivision_code>US-IN     </subdivision_code>
            <subdivision_name>Indiana</subdivision_name>
            <validation_status>0</validation_status>
        </address>
        <location_type>
            <location_type_id>5003</location_type_id>
            <location_type_name>Business Office</location_type_name>
        </location_type>
        <location_type>
            <location_type_id>5002</location_type_id>
            <location_type_name>Warehouse</location_type_name>
        </location_type>
        <location_capability>
            <location_capability_id>13</location_capability_id>
            <location_capability_name>Stores Cartons</location_capability_name>
            <location_capability_category>1</location_capability_category>
        </location_capability>
        <location_capability>
            <location_capability_id>50</location_capability_id>
            <location_capability_name>hq</location_capability_name>
            <location_capability_category>2</location_capability_category>
        </location_capability>
        <location_capability>
            <location_capability_id>53</location_capability_id>
            <location_capability_name>Remit To</location_capability_name>
            <location_capability_category>2</location_capability_category>
        </location_capability>
    </location>
    <location>
        <location_id>16677059</location_id>
        <location_name>ggg(something Co. Ltd)</location_name>
        <is_location_owner>false</is_location_owner>
        <location_registration_status>
            <registration_status_id>7202</registration_status_id>
            <registration_status_name>Unregistered</registration_status_name>
        </location_registration_status>
        <location_status>
            <name>In Progress</name>
        </location_status>
        <marked_for_delete>false</marked_for_delete>
        <bp_location>
            <bp>
                <bp_id>29303</bp_id>
            </bp>
        </bp_location>
        <address>
            <id>122088</id>
            <line1>No.139 abc.</line1>
            <line2>down the woods</line2>
            <city>ff</city>
            <postal_code>4567</postal_code>
            <country_code>CN</country_code>
            <country_name>CHINA</country_name>
            <subdivision_code>r5     </subdivision_code>
            <subdivision_name>Fujian</subdivision_name>
            <validation_status>1702</validation_status>
        </address>
        <location_type>
            <location_type_id>5001</location_type_id>
            <location_type_name>Factory</location_type_name>
        </location_type>
        <location_capability>
            <location_capability_id>10</location_capability_id>
            <location_capability_name>Produces Product</location_capability_name>
            <location_capability_category>1</location_capability_category>
        </location_capability>
    </location>
    </bp>

我必须取地址的第1行和第2行,其中&#34;是主要功能&#34;是真实的&#34;位置功能&#34;是的。

在这种特殊情况下,我不应该在第2行中获取任何内容,因为地址没有任何内容(对于满足条件的上述地址)。但是我的XSLT正在从下一个地址获取line2。这让我相信我编码的条件不起作用。尽管我在Line1中得到了预期的输出,但这只是因为它是第一个地址。下面是我的XSLT:

<xsl:stylesheet version="1.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:ns="http://target.com/2012/03/01/canonical/BusinessPartner">
<xsl:strip-space elements="*" />
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">bp_name|bp_id|line1|line2
    <xsl:for-each select="ns:bp_list/ns:bp">
        <xsl:variable name="bp_name" select="normalize-space(ns:bp_name)"/>
        <xsl:value-of select="$bp_name"/>|<xsl:value-of select="normalize-space(ns:bp_id)"/>|<xsl:if test="((ns:location/ns:bp_location/ns:bp_location_capability/ns:is_primary_capability = 'true') and (ns:location/ns:bp_location/ns:bp_location_capability/ns:location_function_capability_name = 'hq'))"> <xsl:value-of select="normalize-space(ns:location/ns:address/ns:line1)"/></xsl:if>|<xsl:if test="((ns:location/ns:bp_location/ns:bp_location_capability/ns:is_primary_capability = 'true') and (ns:location/ns:bp_location/ns:bp_location_capability/ns:location_function_capability_name = 'hq'))"> <xsl:value-of select="normalize-space(ns:location/ns:address/ns:line2)"/><xsl:text>&#xA;</xsl:text>
    </xsl:for-each></xsl:template>
 </xsl:stylesheet>

这里有什么问题,我该如何解决?

1 个答案:

答案 0 :(得分:0)

我认为问题在于您正在检查某个地点是否符合您的条件,但您没有专门针对该地点。

您可以将条件移至location上的谓词,并仅输出该位置的地址。

示例...

XML输入

<bp_list xmlns="http://example.com/2012/03/01/canonical/BusinessPartner">
    <total_count>221</total_count>
    <bp>
        <bp_id>10</bp_id>
        <bp_name>aad</bp_name>
        <last_changed_date>2016-12-20T02:38:14</last_changed_date>
        <location>
            <location_id>200020</location_id>
            <location_name>gfh</location_name>
            <is_location_owner>true</is_location_owner>
            <location_registration_status>
                <registration_status_id>7202</registration_status_id>
                <registration_status_name>Unregistered</registration_status_name>
                <registration_status_effective_date>2015-07-24T00:00:00</registration_status_effective_date>
            </location_registration_status>
            <location_status>
                <name>In Progress</name>
            </location_status>
            <marked_for_delete>false</marked_for_delete>
            <bp_location>
                <bp>
                    <bp_id>10</bp_id>
                </bp>
                <bp_location_capability>
                    <is_primary_capability>true</is_primary_capability>
                    <location_function_capability_category_id>50</location_function_capability_category_id>
                    <location_function_capability_name>hq</location_function_capability_name>
                </bp_location_capability>
                <bp_location_capability>
                    <is_primary_capability>true</is_primary_capability>
                    <location_function_capability_category_id>53</location_function_capability_category_id>
                    <location_function_capability_name>Remit To</location_function_capability_name>
                </bp_location_capability>
            </bp_location>
            <address>
            <id>103754</id>
            <line1>someadress</line1>
            <city>Columbus</city>
            <postal_code>47201</postal_code>
            <country_code>US</country_code>
            <country_name>UNITED STATES</country_name>
            <subdivision_code>US-IN     </subdivision_code>
            <subdivision_name>Indiana</subdivision_name>
            <validation_status>0</validation_status>
        </address>
            <location_type>
                <location_type_id>5003</location_type_id>
                <location_type_name>Business Office</location_type_name>
            </location_type>
            <location_type>
                <location_type_id>5002</location_type_id>
                <location_type_name>Warehouse</location_type_name>
            </location_type>
            <location_capability>
                <location_capability_id>13</location_capability_id>
                <location_capability_name>Stores Cartons</location_capability_name>
                <location_capability_category>1</location_capability_category>
            </location_capability>
            <location_capability>
                <location_capability_id>50</location_capability_id>
                <location_capability_name>hq</location_capability_name>
                <location_capability_category>2</location_capability_category>
            </location_capability>
            <location_capability>
                <location_capability_id>53</location_capability_id>
                <location_capability_name>Remit To</location_capability_name>
                <location_capability_category>2</location_capability_category>
            </location_capability>
        </location>
        <location>
            <location_id>16677059</location_id>
            <location_name>ggg(something Co. Ltd)</location_name>
            <is_location_owner>false</is_location_owner>
            <location_registration_status>
                <registration_status_id>7202</registration_status_id>
                <registration_status_name>Unregistered</registration_status_name>
            </location_registration_status>
            <location_status>
                <name>In Progress</name>
            </location_status>
            <marked_for_delete>false</marked_for_delete>
            <bp_location>
                <bp>
                    <bp_id>29303</bp_id>
                </bp>
            </bp_location>
            <address>
            <id>122088</id>
            <line1>No.139 abc.</line1>
            <line2>down the woods</line2>
            <city>ff</city>
            <postal_code>4567</postal_code>
            <country_code>CN</country_code>
            <country_name>CHINA</country_name>
            <subdivision_code>r5     </subdivision_code>
            <subdivision_name>Fujian</subdivision_name>
            <validation_status>1702</validation_status>
        </address>
            <location_type>
                <location_type_id>5001</location_type_id>
                <location_type_name>Factory</location_type_name>
            </location_type>
            <location_capability>
                <location_capability_id>10</location_capability_id>
                <location_capability_name>Produces Product</location_capability_name>
                <location_capability_category>1</location_capability_category>
            </location_capability>
        </location>
    </bp>
</bp_list>

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ns="http://example.com/2012/03/01/canonical/BusinessPartner">
  <xsl:strip-space elements="*"/>
  <xsl:output method="text" version="1.0" encoding="UTF-8"/>

  <xsl:template match="/">
    <xsl:text>bp_name|bp_id|line1|line2&#xA;</xsl:text> 
    <xsl:for-each select="ns:bp_list/ns:bp/ns:location[
      ns:bp_location/ns:bp_location_capability[
      ns:is_primary_capability = 'true' and ns:location_function_capability_name = 'hq']]">
      <xsl:value-of select="normalize-space(../ns:bp_name)"/>
      <xsl:text>|</xsl:text>
      <xsl:value-of select="normalize-space(../ns:bp_id)"/>
      <xsl:text>|</xsl:text>
      <xsl:value-of select="normalize-space(ns:address/ns:line1)"/>
      <xsl:text>|</xsl:text>
      <xsl:value-of select="normalize-space(ns:address/ns:line2)"/>
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

<强>输出

bp_name|bp_id|line1|line2
aad|10|someadress|