Change a string in xml into an array in xml easily

2019-07-12 23:15发布

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

Example:

//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>

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.

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?

2条回答
霸刀☆藐视天下
2楼-- · 2019-07-12 23:30

This can be done with XSLT 2.0 as simply as:

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

When this transformation is applied on the provided XML document:

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

the wanted, correct result is produced:

<array name="sample1">
   <item>value1</item>
   <item>value2</item>
   <item>value3</item>
   <item>value4</item>
   <item>value5</item>
</array>
查看更多
等我变得足够好
3楼-- · 2019-07-12 23:39

This 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>

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>

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

Best regards, Peter

查看更多
登录 后发表回答