如何从XML属性转换为值和值子值...? [英] How to convert attributes from XML to values and values to subvalues...?

查看:162
本文介绍了如何从XML属性转换为值和值子值...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我想做些什么:

转换这个XML:

 <书的作者=名称一年=2000>书名< /书>

要这个XML:

<$p$p><$c$c><book><author>Name</author><year>2000</year><value>Book标题&LT; /值&GT;&LT; /书&GT;

我想用XSLT或东西我可以从庆典...

运行做

感谢。


解决方案

这个转变

 &LT;的xsl:样式版本=1.0
 的xmlns:XSL =htt​​p://www.w3.org/1999/XSL/Transform&GT;
 &LT; XSL:输出中省略的XML声明=YES/&GT; &LT;的xsl:模板匹配=节点()| @ *&GT;
  &LT; XSL:复制&GT;
   &LT; XSL:申请模板选择=节点()| @ */&GT;
  &LT; / XSL:复制&GT;
 &LT; / XSL:模板&GT; &LT;的xsl:模板匹配=@ *&GT;
  &LT; XSL:元素名称={名()}&GT;
    &LT;的xsl:value-of的选择= /&gt;中。
  &LT; / XSL:组件&gt;
 &LT; / XSL:模板&GT; &LT;的xsl:模板匹配=TEXT()&GT;
  &LT; VALUE&GT;
   &LT;的xsl:value-of的选择= /&gt;中。
  &LT; /值&GT;
 &LT; / XSL:模板&GT;
&LT; / XSL:样式&GT;

时,所提供的XML文档应用

 &LT;书的作者=名称一年=2000&GT;书名&LT; /书&GT;

产生想要的,正确的结果:

<$p$p><$c$c><book><author>Name</author><year>2000</year><value>Book标题&LT; /值&GT;&LT; /书&GT;

说明


  1. 身份规则/模板 拷贝的每一个节点原样。


  2. 我们覆盖相匹配的任何属性模板身份规则。它创建了一个元素,其名称是匹配的属性的名称,其纯文本子节点是匹配的属性值。


  3. 最后,我们覆盖与任何文本节点匹配的模板身份规则。它只是输出这个节点包裹在一个父元素。


待办事项:身份规则的使用和压倒一切的最根本和强大的XSLT设计模式


  

我想用XSLT或做
  东西我可以从庆典...

运行

大多数XSLT处理器带有命令行实用程序调用的命令行XSLT转换。读您的XSLT处理器的相关文档。

here is what I would like to do:

Convert this XML:

<book author="Name" year="2000">Book title</book>

To this XML:

<book><author>Name</author><year>2000</year><value>Book title</value></book>

I would like to do it with xslt or something I can run from bash...

Thanks.

解决方案

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="@*">
  <xsl:element name="{name()}">
    <xsl:value-of select="."/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="text()">
  <value>
   <xsl:value-of select="."/>
  </value>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<book author="Name" year="2000">Book title</book>

produces the wanted, correct result:

<book><author>Name</author><year>2000</year><value>Book title</value></book>

Explanation:

  1. The identity rule/template copies every node "as-is".

  2. We override the identity rule with a template matching any attribute. It creates an element whose name is the name of the matched attribute and whose only text-node child is the value of the matched attribute.

  3. Finally, we override the identity rule with a template that matches any text node. It simply outputs this node wrapped in a value parent element.

Do note: The use and overriding of the identity rule is the most fundamental and powerful XSLT design pattern.

I would like to do it with xslt or something I can run from bash...

Most XSLT processors come with a command-line utility that invokes an XSLT transformation from the command line. Read your XSLT processor's documentation.

这篇关于如何从XML属性转换为值和值子值...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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