2013-06-26 34 views
1

我試圖使用像過濾器自帶的XML參數列在XSL領域獲得從XML數據的數據值的: 參數是這樣一個xml:動態的xsl:使用從參數

<Catalog name="Catalog1"> 
    <Fields> 
     <Field>ID</Field> 
     <Field>Name</Field> 
     <Field>Description</Field> 
    </Fields> 
</Catalog> 

所以我需要從另一XML元素這個像這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<Nodes> 
    <Node> 
     <Data> 
      <Attribute name="ID">Desktop</Attribute> 
      <Attribute name="Parent">administrator</Attribute> 
      <Attribute name="Name">Desktop</Attribute> 
      <Attribute name="Description">Desktop folder</Attribute> 
     </Data> 
     <Relationship> 
      <RelatedNodes> 
       <Node> 
        <Data> 
         <Attribute name="ID">administrator</Attribute> 
         <Attribute name="Parent"></Attribute> 
         <Attribute name="Name">administrator</Attribute> 
         <Attribute name="Description">administrator folder</Attribute> 
        </Data> 
       </Node> 
      </RelatedNodes> 
     </Relationship> 
    </Node> 
</Nodes> 

我到目前爲止是這樣一個一個XSL模板:

<?xml version="1.0"?> 
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' > 
    <xsl:output method='text' encoding="UTF-8"/> 
    <xsl:param name="Fields"> <!--type="xml"--></xsl:param> 
    <xsl:template match="/"> 
     { 
     <xsl:apply-templates select="//Data[Attribute[@name='Parent']='' ]/parent::Node" /> 
     } 
    </xsl:template> 

    <xsl:template match="Node" > 
     "children":[ 
      { 
       <xsl:for-each select="$Fields/Catalog/Fields/Field"> 
        <xsl:variable name="AttributeName" select="." /> 
        "<xsl:value-of select="$AttributeName"/>": " <xsl:value-of select="Data/Attribute[@name=$AttributeName]" /> ", 
       </xsl:for-each> 
       <xsl:apply-templates select="ancestor::Node[1]"/> 
      } 
     ] 
    </xsl:template> 
</xsl:stylesheet> 

但是不工作,xpath沒有得到值,我認爲這是因爲我必須在xpath的過濾器中設置「'」字符,這就是爲什麼我使用concat但concat只是返回給我的xpath作爲一個字符串,任何想法?,結果應該是這樣的:

{ 
    "children":[ 
     { 
      "ID": "1", 
      "Name": "Name1", 
      "Description: "Desc1", 
     } 
    ] 
} 

謝謝!

回答

1

因爲你只顯示你的問題的一部分,我只能猜測。 似乎在XML中有一些Data/Attribute條目。要訪問它們,你不應該像你那樣在你的選擇中使用concat。

嘗試類似:

<xsl:for-each select="$Fields/Catalog/Fields/Field"> 
     <xsl:variable name="name" select="." /> 
     "<xsl:value-of select="."/>": " <xsl:value-of select="//Data/Attribute[@name= $name]" /> ", 
    </xsl:for-each> 
0

如果您的動態路徑組成元素名稱的簡單,那麼你可以使用

*[name()=$path] 

如果他們是更普遍的路徑,那麼你需要一個函數來評估一個以字符串形式動態提供的XPath表達式。在XSLT 3.0之前,標準中沒有這樣的功能,但許多XSLT處理器有一個合適的擴展函數,通常稱爲xx:evaluate(),其中xx是一些供應商前綴。

+0

你能否進一步解釋你的解決方案? – Dev

+0

除非你告訴我你不瞭解哪一部分。 –