XSLT 将 XML 转换为文本 [英] XSLT to convert XML to text

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

问题描述

我有一个需求,我需要从 XML 文档生成一个文本文件,生成的文本文件应该是基于某些规则的某种特定格式.我的 XML 看起来如下所示:

<标签1><标签2><标签3><PartNo>12 </PartNo></Tag3><DBOMInf1>第111话<DBOMInf2>sring</DBOMInf2><标签2><标签3><PartNo>12 </PartNo></Tag3><DBOMInf1>第555话<DBOMInf2>abcd </DBOMInf2></标签1><标签4><标签5><描述>1200升</描述><否>12</否><名称>引擎</名称><Id>700</Id></Tag5><动作><actionId>700</actionId></动作></信息>

  • 预期的文本输出格式:

ACTIONID|NO|DESCRIPTION|NAME|DBOMInf1|DBOMInf2700|12|发动机|1200 升|第111话700|12|发动机|1200 升|第555话

我是 XSLT 编程的新手,任何人都可以分享一些关于如何实现这一点的信息或示例,我熟悉 XSLT 的基础,如模板匹配、选择值.

任何链接或示例都会非常有帮助.谢谢

解决方案

看起来您希望每个 Tag2 元素都有一行,在这种情况下,通过执行以下操作可以轻松匹配这些元素(假设您当前位于 info 元素

但您似乎还想从 Tag5 元素中查找信息.在这种情况下,您可以根据 No 元素使用键来查找此类值.键的定义如下:

要查找给定 Tag2 元素的标签,您可以执行以下操作:

(注意,normalize-space 并删除元素中多余的空格)

这是完整的 XSLT

<xsl:output method="text" indent="yes"/><xsl:key name="Tags" match="Tag5" use="No"/><xsl:template match="/info"><xsl:text>ACTIONID|NO|DESCRIPTION|NAME|DBOMInf1|DBOMInf2</xsl:text><xsl:value-of select="'&#13;'"/><xsl:apply-templates select="Tag1/Tag2"/></xsl:模板><xsl:template match="Tag2"><xsl:apply-templates select="key('Tags', normalize-space(Tag3/PartNo))"/><xsl:value-of select="concat(DBOMInf1, '|', DBOMInf2, '&#13;')"/></xsl:模板><xsl:template match="Tag5"><xsl:value-of select="concat(Id, '|', No, '|', Name, '|', Description, '|')"/></xsl:模板></xsl:stylesheet>

当应用于您的输入 XML 时,输出以下文本

ACTIONID|NO|DESCRIPTION|NAME|DBOMInf1|DBOMInf2700|12|发动机|1200 升 |111 |环700|12|发动机|1200 升 |第555话A B C D

I have a requirement where i need to generate a text file from XML document, generated text file should be in some particular format based on some rules. My XML looks something shown below:

<info>
 <Tag1>
  <Tag2>
   <Tag3>
    <PartNo>12 </PartNo>
   </Tag3>
   <DBOMInf1> 111 </DBOMInf1>
   <DBOMInf2> sring </DBOMInf2>
   </Tag2>
   <Tag2>
    <Tag3>
     <PartNo>12 </PartNo>
    </Tag3>
    <DBOMInf1> 555 </DBOMInf1>
    <DBOMInf2> abcd </DBOMInf2>
   </Tag2>
  </Tag1>
  <Tag4>
   <Tag5>
    <Description>1200 liter </Description>
    <No>12</No>
    <Name>Engine</Name>
    <Id>700</Id>
   </Tag5>
  </Tag4>
  <action>
   <actionId>700</actionId>
  </action>
</info>

  • Expected output format in text:

ACTIONID|NO|DESCRIPTION|NAME|DBOMInf1|DBOMInf2 700|12|Engine|1200 liter| 111|sring 700|12|Engine|1200 liter| 555|abcd

I am new to XSLT programming can any body share some info or example on how can i achieve this , i am familiar with basis of XSLT like templates matching, value of select.

Any link, or example will be very helpful. Thanks

解决方案

It looks like you want a row for each Tag2 element, in which case these are easily matching by doing the following (assuming you are currently positioned on the info element

<xsl:apply-templates select="Tag1/Tag2" />

But it also looks like you want to look up information from Tag5 elements. In this case you could use a key to look up such values, based on the No element. The key would be defined as follows:

<xsl:key name="Tags" match="Tag5" use="No" />

And to look up the tags for a given Tag2 element, you could do the following:

<xsl:apply-templates select="key('Tags', normalize-space(Tag3/PartNo))" />

(Note, normalize-space with remove the excess white-space from the element)

Here is the full XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text" indent="yes"/>
   <xsl:key name="Tags" match="Tag5" use="No" />

   <xsl:template match="/info">
      <xsl:text>ACTIONID|NO|DESCRIPTION|NAME|DBOMInf1|DBOMInf2</xsl:text>
      <xsl:value-of select="'&#13;'" />
      <xsl:apply-templates select="Tag1/Tag2" />
   </xsl:template>

   <xsl:template match="Tag2">
      <xsl:apply-templates select="key('Tags', normalize-space(Tag3/PartNo))" />
      <xsl:value-of select="concat(DBOMInf1, '|', DBOMInf2, '&#13;')" />
   </xsl:template>

   <xsl:template match="Tag5">
      <xsl:value-of select="concat(Id, '|', No, '|', Name, '|', Description, '|')" />
   </xsl:template>
</xsl:stylesheet>

When applied to your input XML, the following text is output

ACTIONID|NO|DESCRIPTION|NAME|DBOMInf1|DBOMInf2
700|12|Engine|1200 liter | 111 | sring 
700|12|Engine|1200 liter | 555 | abcd 

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

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