XML到XML转换:将元素转换为属性

时间:2015-08-18 22:25:30

标签: xml xslt

我希望有人能够协助我将一个xml文件转换为另一个xml文件。我是新的XSLT;因此,在其他问题/答案中混淆,以达到我几乎能够完成转换的程度......只需要一些最后的帮助。

我有一个XML输入文档:

<copy>
  <paragraph>
    <name>Letter Text 1</name>
    <lastModDate>DD:MM:YY</lastModDate>
  </paragraph>
  <paragraph>
    <name>Letter Text 2</name>
    <lastModDate>DD:MM:YY</lastModDate>
  </paragraph>
  <paragraph>
    <name>Letter Text 3</name>
    <lastModDate>DD:MM:YY</lastModDate>
  </paragraph>
</copy>

我要做的是将每个段落标记的标签压缩为段落元素的属性。另一个xml文档中的所需输出为:

<Paragraph name="Letter Text 1" lastModDate="DD:MM:YY" />
<Paragraph name="Letter Text 2" lastModDate="DD:MM:YY" />
<Paragraph name="Letter Text 3" lastModDate="DD:MM:YY" />

我有我的XSL文件:

<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
  <Paragraph>
    <xsl:for-each select="copy/paragraph">
      <xsl:attribute name="name">
        <xsl:value-of select="name"/>
      </xsl:attribute>
      <xsl:attribute name="lastModDate">
        <xsl:value-of select="lastModDate"/>
      </xsl:attribute>
    </xsl:for-each>
  </Paragraph>
</xsl:template>

这给了我一个输出:

<Paragraph name="Letter Text 3" lastModDate="DD:MM:YY" />

只是最后一个子段节点;我尝试在for-each标签内移动标签,但抛出与空文档有关的错误。

有人可以协助我推荐最终作品吗?

3 个答案:

答案 0 :(得分:0)

<Paragraph>代码放在 xsl:for-each指令中。你现在拥有它的方式是创建一个Paragraph元素,并且你会继续覆盖它的属性。

-
请注意,您的输出不是格式良好的XML,因为它没有单个根元素。

答案 1 :(得分:0)

这个XSLT 2.0样式表......

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

<xsl:output indent="yes" encoding="utf-8" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="/">
  <xsl:apply-templates select="copy/paragraph" />
</xsl:template>

<xsl:template match="paragraph">
  <Paragraph name="{name}" lastModDate="{lastModDate}" />
</xsl:template>

</xsl:stylesheet>

...会将您的样本输入文档转换为此XDM结果树...

<Paragraph name="Letter Text 1" lastModDate="DD:MM:YY"/>
<Paragraph name="Letter Text 2" lastModDate="DD:MM:YY"/>
<Paragraph name="Letter Text 3" lastModDate="DD:MM:YY"/>

此结果树不是格式良好的XML文档。但是,它将是一个格式良好的外部通用解析实体,可能适合您的目的。如果没有,并且您需要输出为正确的XML文档,那么请使用像这样的根元素包装器...

<xsl:output indent="yes" encoding="utf-8" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="/">
  <t>
    <xsl:apply-templates select="copy/paragraph" />
  </t>
</xsl:template>

<xsl:template match="paragraph">
  <Paragraph name="{name}" lastModDate="{lastModDate}" />
</xsl:template>

</xsl:stylesheet>

...会给你结果文件......

<t>
  <Paragraph name="Letter Text 1" lastModDate="DD:MM:YY"/>
  <Paragraph name="Letter Text 2" lastModDate="DD:MM:YY"/>
  <Paragraph name="Letter Text 3" lastModDate="DD:MM:YY"/>
</t>

答案 2 :(得分:0)

这将完成这项工作。 它的优点是,您不需要知道属性的名称。 希望它有所帮助!

[2015-08-19 16:48:30]             Application 'helloworld' with all environments build finished.
[2015-08-19 16:48:30]             Deploying application 'helloworld' with all environments to MobileFirst Server... 
[2015-08-19 16:48:34]             Failed to deploy application 'helloworld' to MobileFirst Server: : HTTP 500 - Internal Server Error 
相关问题