我有这种类型的xml文件: -
<product>
<node>
<region_id>
<node>1</node>
</region_id>
<region_time>
<node>27</node>
<node>02</node>
<node>2013</node>
</region_time>
</node>
</ptroduct>
我想改变这种类型的东西: -
<product>
<region_id>1</region_id>
<region_time>27,02,2013</region_time>
</product>
我想删除<Node>
只想要有如上所述的值
答案 0 :(得分:4)
此转化:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="node[position()>1]/text()">
<xsl:text>,</xsl:text>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
应用于提供的XML文档时:
<product>
<node>
<region_id>
<node>1</node>
</region_id>
<region_time>
<node>27</node>
<node>02</node>
<node>2013</node>
</region_time></node>
</product>
会产生想要的正确结果:
<product>
<region_id>1</region_id>
<region_time>27,02,2013</region_time>
</product>
答案 1 :(得分:2)
以下示例将完成此任务。
想象一下,你有以下文件:
的test.xml
<?xml version="1.0"?>
<product>
<node>
<region_id>
<node>1</node>
</region_id>
<region_time>
<node>27</node>
<node>02</node>
<node>2013</node>
</region_time>
</node>
</product>
test.xsl 已更新
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<!-- from dimitre\'s xsl.thanks -->
<xsl:template match="node[position()>1]/text()">
<xsl:text>,</xsl:text>
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
xslt.php
$sourcedoc = new DOMDocument();
$sourcedoc->load('test.xml');
$stylesheet = new DOMDocument();
$stylesheet->load('test.xsl');
// create a new XSLT processor and load the stylesheet
$xsltprocessor = new XSLTProcessor();
$xsltprocessor->importStylesheet($stylesheet);
// save the new xml file
file_put_contents('test-translated.xml', $xsltprocessor->transformToXML($sourcedoc));
答案 2 :(得分:0)
您可以编写执行以下操作的脚本
'</node><node>'
替换为',''</node>'
替换为'',将'<node>'
替换为''完成!!
您可以使用适当的语法在PHP或Javascript中使用它。