2014-09-23 91 views
-2

我試圖查看輸入objectType,value0是否存在於Types/Type中,並檢查條件以在輸出中使用Type/Type的兄弟屬性名稱值XML。 下面是輸入XML:如何將xml字符串屬性分配給xslt中的變量

<?xml version="1.0" encoding="UTF-8"?> 
<outPut> 
    <object> 
    <objectType>TestOne</objectType> 
    <Attributes> 
     <attribute name="value0">codeOne</attribute> 
     <attribute name="value1">35</attribute> 
     <attribute name="value2">35</attribute> 
    </Attributes> 
    <objectType>TestTwo</objectType> 
    <Attributes> 
     <attribute name="value0">codeTwo</attribute> 
     <attribute name="value1">25</attribute> 
     <attribute name="value2">35</attribute> 
    </Attributes> 
    <objectType>TestThree</objectType> 
    <Attributes> 
     <attribute name="value0">codeThree</attribute> 
     <attribute name="value1">25</attribute> 
     <attribute name="value2">3225</attribute> 
     <attribute name="value3">225</attribute> 
    </Attributes> 
    <objectType>TestFour</objectType> 
    <Attributes> 
     <attribute name="value0">codeFour</attribute> 
     <attribute name="value1">25</attribute> 
     <attribute name="value2">35</attribute> 
    </Attributes> 
    <objectType>TestFive</objectType> 
    <Attributes> 
     <attribute name="value0">codeFive</attribute> 
     <attribute name="value1">2</attribute> 
     <attribute name="value2">3225</attribute> 
     <attribute name="value3">225</attribute> 
    </Attributes> 
    <objectType>TestSix</objectType> 
    <Attributes> 
     <attribute name="value0">codSix</attribute> 
     <attribute name="value1">2</attribute> 
     <attribute name="value2">3225</attribute> 
     <attribute name="value3">225</attribute> 
    </Attributes> 
    </object> 
    <Types> 
    <Type> 
     <temp>TestOne</temp> 
     <ID>2847</ID> 
     <Is_Ingest>0</Is_Ingest> 
     <Category>NAV</Category> 
     <CategoryTwo>NAVAid</CategoryTwo> 
     <Class>codeOne</Class> 
     <New_Attribute>nisting</New_Attribute> 
     <New_Value>52</New_Value> 
     <Condition>value1=35</Condition> 
    </Type> 
    <Type> 
     <temp>TestTwo</temp> 
     <ID>2847</ID> 
     <Is_Ingest>0</Is_Ingest> 
     <Category>NAV</Category> 
     <CategoryTwo>NAVAid</CategoryTwo> 
     <Class>codeTwo</Class> 
     <New_Attribute>nisting</New_Attribute> 
     <New_Value>53</New_Value> 
     <Condition>value1!=33</Condition> 
    </Type> 
    <Types> 
     <Type> 
     <temp>TestThree</temp> 
     <ID>28247</ID> 
     <Is_Ingest>0</Is_Ingest> 
     <Category>NAV</Category> 
     <CategoryTwo>NAVAid</CategoryTwo> 
     <Class>codeThree</Class> 
     <New_Attribute>nisting</New_Attribute> 
     <New_Value>52</New_Value> 
     <Condition>value1=35</Condition> 
     </Type> 
     <Type> 
     <temp>TestFour</temp> 
     <ID>2847</ID> 
     <Is_Ingest>0</Is_Ingest> 
     <Category>NAV</Category> 
     <CategoryTwo>NAVAid</CategoryTwo> 
     <Class>codeFour</Class> 
     <New_Attribute>AidM</New_Attribute> 
     <New_Value>45</New_Value> 
     <Condition>value1!=33 & value1!=28</Condition> 
     </Type> 
     <Type> 
     <temp>TestFive</temp> 
     <ID>2847</ID> 
     <Is_Ingest>0</Is_Ingest> 
     <Category>NAV</Category> 
     <CategoryTwo>Aid</CategoryTwo> 
     <Class>codeFive</Class> 
     <New_Attribute>AidM</New_Attribute> 
     <New_Value>4</New_Value> 
     <Condition>!value</Condition> 
     </Type> 
     <Type> 
     <temp>TestSix</temp> 
     <ID>2847</ID> 
     <Is_Ingest>0</Is_Ingest> 
     <Category>NAV</Category> 
     <CategoryTwo>Aid</CategoryTwo> 
     <Class>codeSix</Class> 
     <New_Attribute>AidM</New_Attribute> 
     <New_Value>4</New_Value> 
     </Type> 
    </Types> 
    </outPut> 

這裏是xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
    <xsl:strip-space elements="*"/> 
    <xsl:template match="/Input"> 
     <objects> 
      <xsl:for-each select="Output"> 
       <object type="{object/objectType}"> 
        <xsl:variable name="attributes" select="Attributes/*"/> 
        <xsl:variable name="matching-template" select="/Output/Types/Type[temp=$attributes and class=$attributes]"/> 
        <template> 
         <xsl:value-of select="$matching-template/New_Attribute"/> 
         <xsl:value-of select="$matching-template/New_Value"/> 
        </template> 
       </object> 
      </xsl:for-each> 
     </objects> 
    </xsl:template> 
</xsl:stylesheet> 

預期輸出

<output> 
    <object> 
     <objectType>TestOne</objectType> 
     <Attributes> 
      <attribute name="value0">codeOne</attribute> 
      <attribute name="value1">25</attribute> 
      <attribute name="value2">35</attribute> 
      <attribute name="nisting">52</attribute> 
     </Attributes> 
     <objectType>TestTwo</objectType> 
     <Attributes> 
      <attribute name="value0">codeTwo</attribute> 
      <attribute name="value1">25</attribute> 
      <attribute name="value2">35</attribute> 
      <attribute name="NAV">NAVAID</attribute> 
      <attribute name="value2">35</attribute> 
     </Attributes> 
    </object> 
</output> 

回答

0

注:萬一有人想知道這個答案是如何相關的到上面的(編輯)問題:以下是2014年9月23日這個問題的第一個版本被接受的答案。在2014年10月4日,OP幾乎完全改變(而不是僅僅發佈後續問題),並且OP不接受答案。爲方便起見,原題是:

我想解析以下

<points> 
    <point> 
     <A>test=11 or test=12</A> 
     <B>pointer> test!=7847</pointer> 
    </point> 
</points> 

我想要得到的東西一樣一樣

<out> 
<att>test</att> 
<att2>=</att> 
<att3>or</att3> 
<att4>12</att4> 
</out> 

我已經使用的財產以後
<xsl:variable name="A" select="substring(/points/point/test,1)
它並沒有幫助,以獲得「或」「和」 ......

接受的答案是:

你的XML是無效的,我想這應該是<B><pointer> test!=7847</pointer></B>而不是<B>pointer> test!=7847</pointer>
雖然尚不清楚哪些規則要得到想要的輸出中,如果元素名稱是靜態的或應生成動態的,下面的XSLT將工作:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

    <xsl:template match="/*"> 
    <xsl:apply-templates select="//A"/> 
    </xsl:template> 

    <xsl:template match="A"> 
    <xsl:variable name="spaceToken" select="tokenize(., ' ')"/> 
    <xsl:variable name="firstEqualToken" select="tokenize($spaceToken[1], '=')"/> 
    <xsl:variable name="secondEqualToken" select="tokenize($spaceToken[3], '=')"/> 
    <xsl:variable name="equalString" select="substring($spaceToken[1],5,1)"/> 
    <out> 
    <att> 
    <xsl:value-of select="$firstEqualToken[1]"/> 
    </att> 
    <att2> 
    <xsl:value-of select="$equalString"/> 
    </att2> 
    <att3> 
    <xsl:value-of select="$spaceToken[2]"/> 
    </att3> 
    <att4> 
    <xsl:value-of select="$secondEqualToken[2]"/> 
    </att4> 
    </out> 
</xsl:template> 
</xsl:stylesheet> 

輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<out> 
<att>test</att> 
<att2>=</att2> 
<att3>or</att3> 
<att4>12</att4> 
</out> 

而且添加一些可能更有價值的東西 - 你的選擇語句select="substring(/points/point/test,1)將不會像預期的那樣工作 - substring()是一個使用字符串的函數,/points/point是xpath的開始,它不會在A文本中獲得「測試」。 「test」是名爲「A」的節點的文本值的一部分。你會看到文字,例如與xpath //points/point/A/text()。您可以使用substring()-功能如下substring(string, startpoint, length),所以要獲得「test = 11」中的「=」$test='test=11'這是substring($test, 5, 1)

參考:W3C - XPath - substring,也許感興趣的第一個答案在這裏提供了很好的解釋:XPath - Difference between node() and text()

更新:對於下如何處理!=<B> test!=7847</B>評論中提及的問題 - 它依賴。因爲它不清楚1)如果在OP中給出的XML是唯一可能的輸入進行處理,或者如果有多種不同的方程式將被拆分,並且輸出應該如何以及爲什麼看起來像建議的輸出在OP中。
它已經提到,輸出應該「看起來像」建議的輸出,但是最好給出整個預期輸出,因爲我只是想知道爲什麼<A>test=11 or test=12</A>缺少11。我會預期所需的輸出應該代表所有可能的條件,如名稱(測試),條件(=),值(s)被檢查相等(11和/或12)。也許只有最大值是有趣的?
進一步的問題是:3)如果只有基於相等的條件是可能的 - =!= - 或者如果有可能,例如, <>在輸入的條件和4)如果可能的話,有2個以上可選的測試表達式和5)如果這些條件將始終是可選的(使用or)或也可被包括(使用and)。
最後,我不得不承認,我不確定你的意思是「如何改變'='等於和'!='不等於比較值」 - 你想要的例子輸出<att2>equals</att2>而不是<att2>=</att2>

現在回到在註釋的第一個問題 - 如果你應該寫第二個模板匹配<B>處理!= - 不知道可能的輸入,這並不容易給任何意見。我認爲<point>可能有兩個以上的條目(C,D ......),並且可能<A>包含使用!=的表達式,並且<B>包含使用=的表達式。因此,我建議使用更動態的方法 - 使用匹配<point>的所有子項的模板,如果包含邏輯運算符(and/or),請檢查每個孩子的每個文本,然後首先處理每個表達式(test=12,test!=12)檢查是否包含!= - 如果是,則在!=上標記化,如果不是,則檢查是否包含=,如果然後標記爲=,並生成動態元素,因爲給定的<B>條件只會導致3個元素 - <att>test</att> <att2>!=</att> <att>7847</att>,還因爲上面的問題4)

如果需要進一步的輸入,如果您只是更新您的問題以提供更多信息將會有所幫助;不一定通過回答我提到的所有問題,但至少考慮它們比幫助您獲得理想輸出更容易。

+0

這是一個很好的幫助。我還有一個問題,我應該爲測試創建另一個模板!= 7847?此外,如何將'='等於和'!='更改爲不等於比較值。謝謝 – user3983344 2014-09-24 14:30:06

+0

@ user3983344很高興我已經能夠幫助你了。對於你評論中的問題 - 我剛剛更新了我的答案 – 2014-09-24 18:32:09

相關問題