2017-01-19 53 views
-1

我有下面的XML:多重條件在XSLT不工作

<?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號線的「位置功能的能力」是HQ 。

在這種特殊情況下,我不應該得到line2中的任何內容,因爲地址沒有任何內容(對於滿足條件的上述地址)。不過,我的XSLT正在從下一個地址獲取第2行。這讓我相信我編碼的條件不起作用。儘管我在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> 

什麼可這裏的問題以及如何解決這個問題?

+0

我的回答有幫助嗎? –

+0

是Daniel,謝謝。你的評估是正確的,我能解決我的問題。 –

+0

請考慮[接受我的回答](http://stackoverflow.com/help/accepted-answer)。另請參閱[當某人回答我的問題時該怎麼辦?](http://stackoverflow.com/help/someone-answers)。 –

回答

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|