2014-09-21 60 views
0
    <?xml version="1.0" encoding="UTF-8"?> 
      <p:transformOutput xmlns:p="http://cfpe/export/objects"> 
       <p:objectSet> 
        <p:objects> 
         <p:object> 
          <p:objectAttributes> 
           <p:objectType>a</p:objectType> 
           <p:attribute name="x">F600</p:attribute> 
           <p:attribute name="y">A100</p:attribute> 
           <p:attribute name="z">D400</p:attribute> 
          </p:objectAttributes> 
         </p:object> 
         <p:object> 
          <p:objectAttributes> 
           <p:objectType>b</p:objectType> 
           <p:attribute name="x">F600</p:attribute> 
           <p:attribute name="y">C300</p:attribute> 
           <p:attribute name="z">B200</p:attribute> 
          </p:objectAttributes> 
         </p:object> 
         <p:object> 
          <p:objectAttributes> 
           <p:objectType>b</p:objectType> 
           <p:attribute name="x">a</p:attribute> 
           <p:attribute name="y">A100</p:attribute> 
           <p:attribute name="z">B200</p:attribute> 
          </p:objectAttributes> 
         </p:object> 
        </p:objects> 
       </p:objectSet> 

      <ns1:Templates> 
       <ns1:Template> 
        <ns1:System_Class>a</ns1:System_Class> 
        <ns1:System_Table>A100</ns1:System_Table> 
        <ns1:System_Attribute>aA</ns1:System_Attribute> 
       </ns1:Template> 
       <ns1:Template> 
        <ns1:System_Class>b</ns1:System_Class> 
        <ns1:System_Table>B200</ns1:System_Table> 
        <ns1:System_Attribute>bB</ns1:System_Attribute> 
       </ns1:Template> 
      </ns1:Templates> 

XSL我無法從XML輸入

  <?xml version="1.0" encoding="UTF-8"?> 
      <xsl:stylesheet version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:p="http://cfpe/export/objects" xmlns:ns1="http://cfpe/export/objects" 
      exclude-result-prefixes="p"> 
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
      <xsl:strip-space elements="*"/> 

      <xsl:template match="/p:transformOutput"> 
       <objects> 
        <xsl:for-each select="p:transformOutput/p:objectSet/p:objects/p:object"> 
         <object type="{p:objectAttributes/p:objectType}"> 
          <xsl:variable name="attributes" select="p:objectAttributes/*" /> 
          <xsl:variable name="matching-template" select="p:transformOutput/ns1:Templates/ns1:Template[ns1:System_Class=$attributes and ns1:System_Table=$attributes]" /> 
          <template> 
           <xsl:value-of select="tns:System_Attribute"/> 
          </template> 
         </object> 
        </xsl:for-each>  
       </objects> 
      </xsl:template> 

      </xsl:stylesheet> 

匹配模板檢索XML attrubutes似乎並沒有找到該值路徑。我是想,如果存在objectAttributes比較System_Class並獲得System_Attribute從如果System_Attribute的價值並不在objectAttributes存在

+0

[XSL標記化的XML輸入文件codition屬性(可能重複http://stackoverflow.com/questions/25921099/xsl-tokenize-the-codition-attribute-with-xml-input-file) – 2014-09-21 09:11:51

+0

它仍然不會放任何東西 – user3983344 2014-09-21 15:39:07

+0

順便說一下,您輸入的XML格式不正確。它缺少一個關閉'p:transformOutput'標記,並且也缺少'ns1'前綴的名稱空間聲明。如果您修改了您的問題以修復XML,這將有所幫助。謝謝。 – 2014-09-21 18:08:11

回答

0

您可以通過根元素p:transformOutput匹配像這樣開始:

<xsl:template match="/p:transformOutput"> 

但你做了xsl:for-each其中還包括根元素

<xsl:for-each select="p:transformOutput/p:objectSet/p:objects/p:object"> 

您已經定位在p:transformOutput所以這將是尋找一個孩子eleme nt,這將不存在。你需要改變它到這個;

<xsl:for-each select="p:objectSet/p:objects/p:object"> 

您以後在matching-template可變

<xsl:variable name="matching-template" 
    select="p:transformOutput/ns1:Templates/ns1:Template[ns1:System_Class=$attributes and ns1:System_Table=$attributes]" /> 

但在這一點上,你是在xsl:for-each結構再次提到它,所以定位在p:object。您需要將xpath表達式設置爲絕對錶達式,以便您再次從根目錄搜索。

<xsl:variable name="matching-template" 
    select="/p:transformOutput/ns1:Templates/ns1:Template[ns1:System_Class=$attributes and ns1:System_Table=$attributes]"/> 

最後,你嘗試獲得System_Attribute這樣的:

<xsl:value-of select="tns:System_Attribute"/> 

但你仍然在這一點上定位在p:object。您需要包括在表達式中matching-template變量:

<xsl:value-of select="$matching-template/ns1:System_Attribute"/> 

試試這個XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:p="http://cfpe/export/objects" xmlns:ns1="http://cfpe/export/objects" exclude-result-prefixes="p"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="/p:transformOutput"> 
     <objects> 
      <xsl:for-each select="p:objectSet/p:objects/p:object"> 
       <object type="{p:objectAttributes/p:objectType}"> 
        <xsl:variable name="attributes" select="p:objectAttributes/*"/> 
        <xsl:variable name="matching-template" select="/p:transformOutput/ns1:Templates/ns1:Template[ns1:System_Class=$attributes and ns1:System_Table=$attributes]"/> 
        <template> 
         <xsl:value-of select="$matching-template/ns1:System_Attribute"/> 
        </template> 
       </object> 
      </xsl:for-each> 
     </objects> 
    </xsl:template> 
</xsl:stylesheet> 
+0

請注意,我在這裏測試了我的XSLT:http://xsltransform.net/nc4NzQ4 – 2014-09-23 09:16:57