【问题标题】:Confused converting XML form one format to an other将 XML 从一种格式转换为另一种格式感到困惑
【发布时间】:2018-03-11 01:16:14
【问题描述】:

我需要将消息数据从一种格式转换为另一种格式。原始文件有 3472 个条目 (sms /sms)。 id、threadId、person 将不再需要。但我必须创建@time,@name。

我的格式:

<?xml version='1.0' encoding='UTF-8'?>
<smsall>
  <sms>
    <id>200</id>
    <threadId>65</threadId>
    <address>+123456789</address>
    <person>1</person>
    <date>1387977340608</date>
    <body> This is a text </body>
    <type>1</type>
    <read>1</read>
  </sms>
</smsall>

运行 XSLT 1.0 后我需要的格式:

<?xml version="1.0" encoding="UTF-8"?>
<allsms count="1">
  <sms address="+123456789" time="" date="1387977340608" type="1" body="This is a text" read="1" service_center="" name="" />
</allsms>

我花了很多天,但我失败得很惨。请问谁能帮帮我?

【问题讨论】:

  • 您能否也添加您的 XSLT,并描述您的问题是什么?
  • 相信我,如果我不感到尴尬,我会发布我的“最佳”尝试。我尝试将 android 备份程序的输出转换为我可以使用的另一个程序。问题是我无论如何都不是专家。我是学生,我的水平显然不够高。我正在尝试基于一般信息和示例(就像我在这里找到的一些)来制作 XSL 。无论我在 online-toolz.com 上运行什么,要么不传输文本,要么不创建具有正确顺序的属性。与我的问题最相似的 michael.hor257k 的示例运行良好。所以这不是工具问题。

标签: xml xslt xslt-1.0


【解决方案1】:

问题不是很复杂,但是你要记住两个细节:

首先:XSLT 1.0xsl:attribute 的语法不允许select 属性。相反,您必须使用此标签的 content,例如:

 <xsl:attribute name="address">
   <xsl:value-of select="address"/>
 </xsl:attribute>

第二个:拥有一个 empty 内容的属性,就足够了 将xsl:attribute 仅与 name 放在一起,但没有任何内容,例如:

<xsl:attribute name="time"/>

所以脚本可以如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />

  <xsl:template match="smsall">
    <allsms>
      <xsl:attribute name="count">
        <xsl:value-of select="count(sms)"/>
      </xsl:attribute>
      <xsl:apply-templates/>
    </allsms>
  </xsl:template>

  <xsl:template match="sms">
    <sms>
      <xsl:attribute name="address">
        <xsl:value-of select="address"/>
      </xsl:attribute>
      <xsl:attribute name="time"/>
      <xsl:attribute name="date">
        <xsl:value-of select="date"/>
      </xsl:attribute>
      <xsl:attribute name="type">
        <xsl:value-of select="type"/>
      </xsl:attribute>
      <xsl:attribute name="body">
        <xsl:value-of select="normalize-space(body)"/>
      </xsl:attribute>
      <xsl:attribute name="read">
        <xsl:value-of select="read"/>
      </xsl:attribute>
      <xsl:attribute name="service_center"/>
      <xsl:attribute name="name"/>
    </sms>
  </xsl:template>
</xsl:stylesheet>

有关工作示例(在 XSLT 1.0 中)请参阅 http://xsltransform.net/nb9MWsZ

【讨论】:

  • 谢谢!!!你不明白我为此浪费了多少小时!第一个错误是例如“时间”和“日期”将它们添加为文本 time="{@time}" date="{@date}"。我在数据中发现的第二个 BUG 是由于某些愚蠢的原因,原始程序将其翻译为 ⁄并且代码没有给我任何结果。再次非常感谢您!
  • 对于所有设计并下载 android 的人 - 超级备份和恢复:所有而不是超级备份,这将修复您的数据,以便超级备份可以恢复它们。请记住,您必须替换“⁄”到 ” ' ”。谢谢 Valdi_Bo!
猜你喜欢
  • 1970-01-01
  • 2018-01-20
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多