【问题标题】:Remove The Child Tag Name not a value删除子标签名称不是值
【发布时间】:2013-02-27 05:44:40
【问题描述】:

我有这种类型的 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>

我想删除 &lt;Node&gt; 只是想要上面的值

【问题讨论】:

  • 在 PHP、Javascript 或 XSLT 中?
  • 在 PHP 中有 DOMDocument;在 Javascript 中 - 你需要 IE8 支持吗?你试过什么?我会做一些手动迭代;在 XSLT 中 - 不知道。
  • 我没有尝试过任何事情,但我认为它可能与替换功能......
  • @JackPhp 再次,什么语言?

标签: php javascript xml xslt


【解决方案1】:

这种转变:

<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 27,02,2013
  • @Dimitre Novatchev - 我在你的 xslt 中有一个澄清,我试过了,它工作正常,但目的是什么 ?它有什么作用?我现在正在学习 xslt,希望您为我澄清这一点
  • @Abi,您需要了解身份规则。这是最基本的 XSLT 设计模式。 dpawson.co.uk/xsl/sect2/identity.html
【解决方案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));

【讨论】:

  • 秒!其中有一个小错误..没有正确阅读问题将修复 xsl...
  • 嘿,它很好,但我想要其他类型,如 xml tree wise output 127,02,2013跨度>
  • 是的,我已经提到了这一点。我建议你使用我的 php 代码和 dimitries (great) xsl .. 这应该可以工作 .. (我已经测试过了)
  • 在浏览器中打开源代码视图;)浏览器会“吃掉”xml标签
  • 检查我的更新.. 为此使用file_put_contents()。还要确保您(网络服务器)具有写入权限
【解决方案3】:

您可以编写一个执行以下操作的脚本

  1. 删除所有空格和换行符(所以,所有内容都在一行中)。
  2. 使用字符串替换,将'&lt;/node&gt;&lt;node&gt;'替换为','
  3. 使用字符串替换,将'&lt;/node&gt;'替换为'','&lt;node&gt;'替换为''

完成!!

您可以通过使用适当的语法在 PHP 或 Javascript 中使用它。

【讨论】:

  • 您正在尝试展平文件并使用正则表达式。当您可以使用更有效的方法(即 DOMDocument、SXE)时,这会完全减慢处理速度。这根本不对。我是否还应该提到所需的编码语言甚至没有被提及。
  • 我宁愿坚持 DOM 操作也不愿依赖字符串操作。
  • 好吧,关于编码语言,问题已被标记为 PHP,Javascript,因此假设。
  • @Archer 问题已被标记为 PHP、Javascript,因此这不是一个真正的问题(直到提问者决定)
  • @JanDvorak, Ok Jan :) ... 除了标签 .. 另外,他的用户名是 Jack Php,... 我还在等待提问者决定. :)
猜你喜欢
  • 2020-05-16
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
  • 1970-01-01
  • 2014-07-12
  • 1970-01-01
  • 2020-07-23
  • 1970-01-01
相关资源
最近更新 更多