2015-01-10 73 views
-1

我堅持寫作XSL問題使用XSLT 1填充列表,列表大小是可變的使用XSLT 1

我的原始XML是這樣的:

<conf:BasicConfig id="1"> 
    <conf:attributes> 
     <conf:LIST_value> 
      <conf:value>5</conf:value> 
      <conf:value>10</conf:value> 
     </conf:LIST_value> 
    </conf:attributes> 
</conf:BasicConfig> 
<conf:BasicConfig id="2"> 
    <conf:attributes> 
     <conf:LIST_value> 
      <conf:value>6</conf:value> 
      <conf:value>7</conf:value> 
     </conf:LIST_value> 
    </conf:attributes> 
</conf:BasicConfig> 

我有一個列表像10,如圖9所示,12(該列表的大小可能不同) 值的我想在列表中替換這些值,其中BasicConfig ID =「1」

這樣我有最終的XML等:

<conf:BasicConfig id="1"> 
    <conf:attributes> 
     <conf:LIST_value> 
      <conf:value>10</conf:value> 
      <conf:value>9</conf:value> 
      <conf:value>12</conf:value> 
     </conf:LIST_value> 
    </conf:attributes> 
</conf:BasicConfig> 
<conf:BasicConfig id="2"> 
    <conf:attributes> 
     <conf:LIST_value> 
      <conf:value>6</conf:value> 
      <conf:value>7</conf:value> 
     </conf:LIST_value> 
    </conf:attributes> 
</conf:BasicConfig> 

我有靈活性來保持這些值的列表10,9,12任何方式在xsl文件。 對於例如,我可以把它作爲:

<xsl:param name="list_values" select="'9,10,12'" /> 

或者,我可以把它作爲:

<xsl:param name="list_values1" select="'9'" /> 
<xsl:param name="list_values2" select="'10'" /> 
<xsl:param name="list_values3" select="'12'" /> 

或者以另一種方式,但此列表的大小可能會有所不同。有時,我的xsl文件將包含1值或2或任何n值。

但我不知道如何將這種靈活性帶入xsl文件。 任何人都可以幫助我。

+0

如果您提供了當前的XSLT文件,這將會很有幫助。 – JLRishe

+0

也請告訴你正在使用的主機系統。如果是Java或.NET,那麼您可以輕鬆地將節點列表作爲XSLT參數傳遞,這比通過逗號分隔的字符串更加通用。 – Tomalak

+0

爲什麼這被標記爲撒克遜和Xalan? –

回答

2

由於您尚未提供任何關於您當前的XSLT或conf的命名空間的信息,因此我可以做的最好的回答是基於您提供的信息。

您可以使用遞歸模板分裂的字符串值,一旦你有,你只需要的是conf:LIST_value爲您的條件相匹配的模板:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
           xmlns:conf="c"> 
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 
    <xsl:param name="listValues" select="'9,10,12'" /> 

    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="conf:BasicConfig[@id = 1]/conf:attributes/conf:LIST_value"> 
    <xsl:copy> 
     <xsl:call-template name="Split"> 
     <xsl:with-param name="elementName" select="'conf:value'" /> 
     <xsl:with-param name="value" select="$listValues" /> 
     </xsl:call-template> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template name="Split"> 
    <xsl:param name="elementName" /> 
    <xsl:param name="separator" select="','" /> 
    <xsl:param name="value" /> 

    <xsl:if test="string($value)"> 
     <xsl:element name="{$elementName}"> 
     <xsl:value-of 
       select="substring-before(concat($value, $separator), $separator)"/> 
     </xsl:element> 
     <xsl:call-template name="Split"> 
     <xsl:with-param name="elementName" select="$elementName" /> 
     <xsl:with-param name="separator" select="$separator" /> 
     <xsl:with-param name="value" 
         select="substring-after($value, $separator)" /> 
     </xsl:call-template> 
    </xsl:if> 

    </xsl:template> 
</xsl:stylesheet> 

當這是基於以下運行輸入:

<n xmlns:conf="c"> 
    <conf:BasicConfig id="1"> 
    <conf:attributes> 
     <conf:LIST_value> 
     <conf:value>5</conf:value> 
     <conf:value>10</conf:value> 
     </conf:LIST_value> 
    </conf:attributes> 
    </conf:BasicConfig> 
    <conf:BasicConfig id="2"> 
    <conf:attributes> 
     <conf:LIST_value> 
     <conf:value>6</conf:value> 
     <conf:value>7</conf:value> 
     </conf:LIST_value> 
    </conf:attributes> 
    </conf:BasicConfig> 
</n> 

結果是:

<n xmlns:conf="c"> 
    <conf:BasicConfig id="1"> 
    <conf:attributes> 
     <conf:LIST_value> 
     <conf:value>9</conf:value> 
     <conf:value>10</conf:value> 
     <conf:value>12</conf:value> 
     </conf:LIST_value> 
    </conf:attributes> 
    </conf:BasicConfig> 
    <conf:BasicConfig id="2"> 
    <conf:attributes> 
     <conf:LIST_value> 
     <conf:value>6</conf:value> 
     <conf:value>7</conf:value> 
     </conf:LIST_value> 
    </conf:attributes> 
    </conf:BasicConfig> 
</n> 
+0

非常感謝。你太棒了!我是xsl的新手,這對我有很大的幫助。 –

0

我有靈活性,以保留這些值列表中的值10,9,12任何方式在 xsl文件。

那麼爲什麼不讓他們完全按照你需要的方式,在你需要他們的地方?

<xsl:template match="conf:BasicConfig[@id='1']"> 
    <xsl:copy> 
     <conf:attributes> 
      <conf:LIST_value> 
       <conf:value>10</conf:value> 
       <conf:value>9</conf:value> 
       <conf:value>12</conf:value> 
      </conf:LIST_value> 
     </conf:attributes> 
    </xsl:copy> 
</xsl:template> 

有時候,我的XSL文件將包含值1或2或N值。

您打算如何處理這些更改?