更改XML字符串转换成XML格式的数组容易 [英] Change a string in xml into an array in xml easily

查看:130
本文介绍了更改XML字符串转换成XML格式的数组容易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人知道,如果有一个简单的方法来改变我所有的串在我的xml文件到数组?

I was wondering if anyone knew if there was a simple way to change all of my strings in my xml file to arrays?

例如:

//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值在每一个更容易模拟这个过程。

My problem is not that I do not know how to manually do it. But I am looking for a way to simulate this process more easily because I have 120 strings with 25-90 values in each.

一个很好的例子就是使用附加在GIMP模拟这个过程对你为每个图像转换多个图像的一些推广,只需一次点击。

A good example would be converting multiple images extentions with a single click using an add-on to GIMP that simulates that process for you for each image.

会有人谁明白我要问,知道的方式为字符串数组做到这一点?

Would anyone who understands what I am asking, know a way to do this for string to array?

推荐答案

这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:

applied to this XML:

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

给出了这样的输出:

gives this output:

<?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使用递归模板通过您的字符串值不断在逗号分割它们。

The XSLT is using a recursive template going through your string values and splitting them at the comma.

最好的问候,
彼得

Best regards, Peter

这篇关于更改XML字符串转换成XML格式的数组容易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆