2010-10-05 51 views
1

編輯:實例現在包含有我的主文檔故障轉換XML文檔URL

大家在所有標籤!我只是對XSLT有個簡單的問題。我有一個很大的xml文件,其中嵌套了許多<DIMENSION_Id>節點。在每個<DIMENSION_Id>節點中有兩個SYN標記:<SYN>String</SYN><SYN>Integer</SYN>我想要做的是將每個DIMENSION_Id的最遠的子節點連接到它的所有祖先路徑以創建一個URL。

<DIMENSIONS VERSION="1.0.0"> 
    <DIMENSION NAME="Category" SRC_TYPE="INTERNAL"> 
     <DIMENSION_NODE ID="1000"/> 
     <DIMENSION_Id> 
      <SYN>Text</SYN> 
      <SYN>Number</SYN> 
      <DIMENSION_Id> 
       <SYN>More Text</SYN> 
       <SYN>Another Number</SYN> 
      </DIMENSION_Id> 
     </DIMENSION_Id> 
    </DIMENSION> 
</DIMENSIONS> 

我寫了這個XSLT先獲得從父節點的所有信息,然後最後創建一個完整的URL子節點。不幸的是,它只給了我最遠的子節點的信息......我不知道如何附加任何其他文本。 (應改爲類似:最遠的父/接近父/母/ item_selected)

不幸的是它是所有給我當前節點的值....下面是我寫的XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" /> 
    <xsl:template match="/DIMENSION_NODE"> 
     <xsl:for-each select="ancestor-or-self::*"> 
      <xsl:value-of select="@SYN" /> 
      <xsl:text>/</xsl:text> 
      <xsl:value-of select="." /> 
      <xsl:value-of select="@SYN" /> 
      <xsl:text>/</xsl:text> 
      <xsl:value-of select="." /> 
     </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

在此先感謝您的幫助!

+0

你的問題配方不好。您的輸入樣本與樣式表不符,也不符合您的期望結果。請糾正這一點。 – 2010-10-05 16:19:23

+0

請原諒我,但我不明白你需要告訴我什麼信息。我想要的是讓這就是在父SYN一路孩子,所以它看起來像一個URL值,即: GRANDPARENT_SYN_VALuE/PARENT_SYN_VALUE/CURRENT_NODE_SYN_VALUE 爲每個XML節點。接下來我需要做的是將第二個SYN值中的整數與URL匹配......但我主要關心的是實際的URL字符串。如果你需要我進一步澄清,請讓我知道.. – Daniel 2010-10-05 17:02:44

+0

通過制定不佳的問題,我的意思是:'GRANDPARENT_SYN_VALuE/PARENT_SYN_VALUE' ...但你有**兩個**'SYN'元素;你的樣式表匹配輸入樣本中不存在的'DIMENSION_NODE'根元素;你試圖用''輸出'SYN'屬性和'DIMENSION_NODE'元素的字符串值...... – 2010-10-05 17:53:48

回答

1

編輯:輸入樣本更接近問題。

有了這個輸入:

<DIMENSIONS VERSION="1.0.0"> 
    <DIMENSION NAME="Category" SRC_TYPE="INTERNAL"> 
     <DIMENSION_NODE ID="1000"/> 
     <DIMENSION_Id> 
      <SYN>Text</SYN> 
      <SYN>1</SYN> 
      <DIMENSION_Id> 
       <SYN>More Text</SYN> 
       <SYN>2</SYN> 
      </DIMENSION_Id> 
     </DIMENSION_Id> 
    </DIMENSION> 
</DIMENSIONS> 

兩個選項。

1)應用模板和模式,以祖先:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text"/> 
    <xsl:template match="text()"/> 
    <xsl:template match="SYN[number()!=.]"> 
     <xsl:apply-templates select="ancestor::DIMENSION_Id" mode="output"/> 
     <xsl:value-of select="concat(' ',../SYN[number()=.],'&#xA;')"/> 
    </xsl:template> 
    <xsl:template match="DIMENSION_Id" mode="output"> 
     <xsl:value-of select="concat('/',SYN[number()!=.])"/> 
    </xsl:template> 
</xsl:stylesheet> 

2)使用參數:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text"/> 
    <xsl:template match="text()"/> 
    <xsl:template match="SYN[number()!=.]"> 
     <xsl:param name="pPath"/> 
     <xsl:value-of select="concat($pPath,' ',../SYN[number()=.],'&#xA;')"/> 
    </xsl:template> 
    <xsl:template match="DIMENSION_Id"> 
     <xsl:param name="pPath"/> 
     <xsl:apply-templates> 
      <xsl:with-param name="pPath" 
           select="concat($pPath,'/',SYN[number()!=.])"/> 
     </xsl:apply-templates> 
    </xsl:template> 
</xsl:stylesheet> 

兩個輸出:

/Text 1 
/Text/More Text 2 
+0

嗯..對於第一個我使用SYN切換文件,並使用DIMENSION_Id命名的文件...但它不起作用。我無法真正理解你的第二個例子,我很抱歉...我對XSLT很陌生。 – Daniel 2010-10-05 17:00:39

+0

@Daniel:檢查我的編輯與輸入更接近你的 – 2010-10-05 18:26:56

1

我猜你想要這個

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 

<xsl:template match="DIMENSION_Id[not(DIMENSION_Id)]"> 
    <xsl:apply-templates select="(.|ancestor::DIMENSION_Id)/SYN" mode="gen"/> 
</xsl:template> 

<xsl:template match="SYN" mode="gen"> 
    <xsl:value-of select="concat('/',.)"/> 
</xsl:template> 
<xsl:template match="text()"/> 
</xsl:stylesheet> 

當這個變換所提供的XML文檔應用(校正爲良好的形成):

<DIMENSIONS VERSION="1.0.0"> 
    <DIMENSION NAME="Category" SRC_TYPE="INTERNAL"> 
     <DIMENSION_NODE ID="1000"/> 
     <DIMENSION_Id> 
      <SYN>Text</SYN> 
      <SYN>Number</SYN> 
      <DIMENSION_Id> 
       <SYN>More Text</SYN> 
       <SYN>Another Number</SYN> 
      </DIMENSION_Id> 
     </DIMENSION_Id> 
</DIMENSION> 
</DIMENSIONS> 

有用,正確的結果產生:

/Text/Number/More Text/Another Number 
+0

hmm..I'm沒有得到這個時候輸出任何東西......但我認爲很多是因爲我自己的不正確張貼的文件。我現在編輯它是正確的。對困惑感到抱歉。 – Daniel 2010-10-05 19:25:57

+0

@Daniel:使用新的XML文檔,我可以得到完全相同的結果。你有沒有更正它,因爲你正在呈現的那個是*不是*格式良好,會導致解析器錯誤。 – 2010-10-05 19:36:53

0

對不起,每個人都感到困惑。我得到了一些幫助,這裏是解決方案:

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 

    <xsl:template match="/"> 
     <URLs> 
      <xsl:apply-templates select="//DIMENSION_NODE"/> 
     </URLs> 
    </xsl:template> 

    <xsl:template match="DIMENSION_NODE"> 
     <xsl:call-template name="getPath"> 
      <xsl:with-param name="currentnode" select="."/> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="getPath"> 
     <xsl:param name="currentnode"/> 
     <xsl:param name="currenttext" select="''"/> 
     <xsl:param name="firstrun" select="1"/> 
     <xsl:choose> 
      <xsl:when test="$currentnode[parent::DIMENSION]"> 
       <URL> 
        <xsl:value-of select="$currenttext"/> 
       </URL> 
      </xsl:when> 
      <xsl:otherwise> 
       <xsl:choose> 
        <xsl:when test="$firstrun = 1"> 
         <xsl:variable name="gettext"> 
          <xsl:text>/</xsl:text> 
          <xsl:value-of select="concat($currentnode/DVAL/SYN[1],'&#x9;',$currentnode/DVAL/DVAL_ID/@ID)"/> 
         </xsl:variable> 
         <xsl:call-template name="getPath"> 
          <xsl:with-param name="currentnode" select="$currentnode/.."/> 
          <xsl:with-param name="currenttext" select="concat($gettext,$currenttext)"/> 
          <xsl:with-param name="firstrun" select="0"/> 
         </xsl:call-template> 
        </xsl:when> 
        <xsl:otherwise> 
         <xsl:variable name="gettext"> 
          <xsl:text>/</xsl:text> 
          <xsl:value-of select="$currentnode/DVAL/SYN[1]"/> 
         </xsl:variable> 
         <xsl:call-template name="getPath"> 
          <xsl:with-param name="currentnode" select="$currentnode/.."/> 
          <xsl:with-param name="currenttext" select="concat($gettext,$currenttext)"/> 
          <xsl:with-param name="firstrun" select="0"/> 
         </xsl:call-template> 
        </xsl:otherwise> 
       </xsl:choose> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

此外,我在我的XML文件中的錯誤道歉。

+0

我很少downvote niether問題或答案,但這是錯誤的。除了冗長和錯誤的設計(不是XSLT風格,即'$ firstrun'或調用命名模板傳遞上下文節點作爲參數),它不會幫助任何人,因爲它不符合您的輸入樣本('DVAL'和'DVAL_ID '元素)。 – 2010-10-06 18:10:41