转换XML内容阵列 [英] Transforming XML content to array

查看:140
本文介绍了转换XML内容阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为相反的情况下​​<一href=\"http://stackoverflow.com/questions/12084968/change-a-string-in-xml-into-an-array-in-xml-easily\">this帖子,它怎么可能使用XSLT转换XML文档的内容数组。

As the reverse case of this post, how it's possible to transform content of an XML document to array using XSLT.

有关这一点:

<Records>
    <item>value1</item>
    <item>value2</item>
    <item>value3</item>
    <item>value4</item>
    <item>value5</item>
</Records> 

期望的结果是这样的:

The desired result is something like this:

[value1, value2, value3, value4, value5]

有什么想法?

推荐答案

希望这有助于..

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl">
  <xsl:output method="text"/>
  <xsl:template match="/">
    <xsl:variable name="countItems" select="count(Records/item)"/>
    [<xsl:for-each select="Records/item">
      <xsl:value-of select="."/>
      <xsl:choose>
        <xsl:when test="position()=$countItems"/>
        <xsl:otherwise>,</xsl:otherwise>
      </xsl:choose>
    </xsl:for-each>]
  </xsl:template>
</xsl:stylesheet>

XSLT 1.0可用于生成从源xml文件所得的XML,文本或HTML文件。
您可以从通过XSLT创建的文件获得最终的数据

XSLT 1.0 can be used to generate a resultant xml,text or HTML file from the source xml file. You can get the resultant data from the file created via XSLT

这篇关于转换XML内容阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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