2012-08-23 24 views
2

我想知道是否有人知道是否有一種簡單的方法將我的xml文件中的所有字符串更改爲數組?將xml中的字符串更改爲xml中的數組很容易

例子:

//This is the string I need to change: 
<string name="sample1">value1, value2, value3, value4, value5</string> 
//This is the way I need it to be: 
<array name="sample1"> 
    <item>value1</item> 
    <item>value2</item> 
    <item>value3</item> 
    <item>value4</item> 
    <item>value5</item> 
</array> 

我的問題是不是我不知道該怎麼做手工。但我正在尋找一種更容易模擬這個過程的方法,因爲我有120個字符串,每個字符串的值都是25-90。

一個很好的例子就是使用GIMP插件將單個點擊的多個圖像擴展轉換爲模擬您的每個圖像的過程。

誰知道我在問什麼,知道一種方法來做到這一點的字符串數組?

+0

,因爲你是在處理XML – Peter

+0

什麼是XSLT正是你可以使用XSLT? – zack2o

+0

http://en.m.wikipedia.org/wiki/XSLT。我可以在一個小時內給你一個xslt的轉換,如果你想嘗試一下 – Peter

回答

3

這XSLT:

<?xml version="1.0" encoding="UTF-8"?> 

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

<xsl:template match="/"> 
    <array> 
     <xsl:attribute name="name"> 
      <xsl:value-of select="string/@name"/> 
     </xsl:attribute> 
     <xsl:apply-templates/> 
    </array> 
</xsl:template> 

<xsl:template match="string/text()" name="tokenize"> 
    <xsl:param name="text" select="."/> 
    <xsl:param name="sep" select="','"/> 
    <xsl:choose> 
     <xsl:when test="not(contains($text, $sep))"> 
      <item> 
       <xsl:value-of select="normalize-space($text)"/> 
      </item> 
     </xsl:when> 
     <xsl:otherwise> 
      <item> 
       <xsl:value-of select="normalize-space(substring-before($text, $sep))"/> 
      </item> 
      <xsl:call-template name="tokenize"> 
       <xsl:with-param name="text" select="substring-after($text, $sep)"/> 
      </xsl:call-template> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 

應用到這個XML:

<?xml version="1.0" encoding="UTF-8"?> 
<string name="sample1">value1, value2, value3, value4, value5</string> 

給出了這樣的輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<array name="sample1"> 
<item>value1</item> 
<item>value2</item> 
<item>value3</item> 
<item>value4</item> 
<item>value5</item> 
</array> 

使用遞歸模板,通過您的字符串值去的XSLT和用逗號分割它們。

最好的問候, 彼得

+0

很好的答案。但是,在標準化字符串值時要小心,也許數據中允許有空格。 – FiveO

+0

好吧,所以我運行xsl轉換使用你提供的,我得到一個錯誤:res \ layout \ NewStylesheet.xsl:**沒有嵌入的文件樣式表指令:file:/ C:/ Users/MyName/Documents/My Dropbox/Personal/workspace/MySampleApp/res/values/strings.xml ** – zack2o

+0

hi zack2o,在源碼xml之前放置這一行:<?xml-stylesheet type =「text/xsl」href =「cdcatalog.xsl」 ?>並在href中給出你的文件名。問候,彼得 – Peter

1

這可以用XSLT 2.0進行簡單,如

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="/*"> 
    <array name="{@name}"> 
    <xsl:for-each select="tokenize(., ',\s*')"> 
     <item><xsl:value-of select="."/></item> 
    </xsl:for-each> 
    </array> 
</xsl:template> 
</xsl:stylesheet> 

當這種變換所提供的XML文檔應用:

<string name="sample1">value1, value2, value3, value4, value5</string> 

想要的,正確的結果產生

<array name="sample1"> 
    <item>value1</item> 
    <item>value2</item> 
    <item>value3</item> 
    <item>value4</item> 
    <item>value5</item> 
</array> 
+0

似乎是兩個相同的錯誤,我是否缺少一些東西: **沒有嵌入式文件的樣式表指令:file:/ C:/ Users/MyName/Documents/My Dropbox/Personal/workspace/MySampleApp/res /values/strings.xml** – zack2o

+0

看來你試圖以某種奇怪的方式運行轉換。轉換需要存在於自己的文件中 - 而不是嵌入在要轉換的XML文件中。 –

相關問題