2013-03-15 105 views
0

我有以下XMLXSLT删除节点和atrribute值转换为元素名称

<?xml version="1.0" encoding="UTF-8"?> 
<response> 
    <lst name="somename"> 
     <node1></node1> 
     <node2></node2> 
    </lst> 
    <result name="somename" count="5"> 
     <doc> 
      <str name="NodeA">ValueA</str> 
      <str name="NodeB">ValueB</str> 
      <str name="NodeC">ValueC</str> 
     </doc> 
     <doc> 
      <str name="NodeA">ValueD</str> 
      <str name="NodeB">ValueE</str> 
      <str name="NodeC">ValueF</str> 
     </doc> 
    </result> 
</response> 

,我要转换为

<?xml version="1.0" encoding="UTF-8"?> 
<response> 
    <doc> 
     <NodeA>ValueA</NodeA> 
     <NodeB>ValueB</NodeB> 
     <NodeC>ValueC</NodeC> 
    </doc> 
    <doc> 
     <NodeA>ValueD</NodeA> 
     <NodeB>ValueE</NodeB> 
     <NodeC>ValueF</NodeC> 
    </doc> 
</response> 

正如你所看到的LST节点被完全去除,并属性值现在成为节点。

首先,我用这个xslt代码去除了第一个节点。

<?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" version="1.0" encoding="UTF-8" indent="yes" /> 
    <xsl:template match="node() | @*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node() | @*"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="lst"/> 
</xsl:stylesheet> 

这给了我这个

<?xml version="1.0" encoding="UTF-8"?> 
<response> 
    <result name="somename" count="5"> 
     <doc> 
      <str name="NodeA">ValueA</str> 
      <str name="NodeB">ValueB</str> 
      <str name="NodeC">ValueC</str> 
     </doc> 
     <doc> 
      <str name="NodeA">ValueD</str> 
      <str name="NodeB">ValueE</str> 
      <str name="NodeC">ValueF</str> 
     </doc> 
    </result> 
</response> 

然后用这个XSLT从链接[link] Convert attribute value into element

<?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" version="1.0" encoding="UTF-8" indent="yes" /> 
    <xsl:template match="response/result/doc"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()" /> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="token"> 
     <xsl:element name="{@name}"> 
      <xsl:apply-templates /> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

但它并没有帮助。它给了我这个。

<?xml version="1.0" encoding="utf-8"?> 
<doc>ValueAValueBValueC</doc> 
<doc>ValueDValueEValueF</doc> 

请帮我把属性值转换成节点的第二部分。 是否有可能让一个xslt做这两件事?

+0

问题是什么/你在哪里卡住了? – 2013-03-15 14:00:22

+0

您可以展示迄今为止获得的XSLT,因此我们可以看到/指出问题到底是什么? – 2013-03-15 14:03:27

+0

发布更新。请检查。 – user1677271 2013-03-15 14:14:10

回答

1

你可以在这里实现你所有的目标,一个XSLT这是非常接近目前的一个:

<?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" version="1.0" encoding="UTF-8" indent="yes" /> 
    <xsl:strip-space elements="*" /> 

    <!-- copy everything as-is apart from exceptions below --> 
    <xsl:template match="node() | @*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node() | @*"/> 
     </xsl:copy> 
    </xsl:template> 

    <!-- delete lst --> 
    <xsl:template match="lst"/> 

    <!-- strip out the result element but still include its children --> 
    <xsl:template match="result"> 
     <xsl:apply-templates /> 
    </xsl:template> 

    <!-- convert str name="X" to X --> 
    <xsl:template match="str"> 
     <xsl:element name="{@name}"> 
     <xsl:apply-templates /> 
     </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 
+0

完全按照我想要给伊恩的方式工作。特别感谢您在每行代码之前的自我解释评论。 – user1677271 2013-03-18 11:34:16

+0

一个奇怪的问题。 当我们在源xml中有一个空节点时,xslt会将这个节点的结束标记放在下一行,而不是同一行。 因此,当导入这样的文件时,我们在数据中有新的行字符。 例如,这 \t 转化为 \t \t 但是应该理想已tranformed要么这 \t 或该 \t 任何解决方案对于这个问题? – user1677271 2013-03-18 12:31:37

+0

@ user1677271如果节点真正完全是空的,只要它是负责解析原始XML文件的XSLT处理器,''应该处理该节点。如果您将XSLT处理程序交给预解析的DOM文档,而不是让它解析XML,那么它可能不会去除仅包含空白的文本节点。你可以通过添加一个'' – 2013-03-18 13:19:42

0

你可以以此为基础

XSL如何将所有元素复制除了那些所谓的注释

<?xml version="1.0"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="1.0"> 

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

    <!-- copy xsd:annotation and children, but don't copy the attributes --> 
    <xsl:template match="xsd:annotation"> 

     <xsd:annotation> 
      <xsl:copy-of select="*"/> 
     </xsd:annotation> 

    </xsl:template> 

</xsl:stylesheet> 
0

这将做到这一点:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" /> 
    <xsl:template match="node()"> 
    <xsl:copy> 
     <xsl:apply-templates /> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="lst"/> 

    <xsl:template match="str[@name]"> 
    <xsl:element name="{@name}"> 
     <xsl:apply-templates /> 
    </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

当你的样品输入运行,这将产生:

<response> 
    <result> 
    <doc> 
     <NodeA>ValueA</NodeA> 
     <NodeB>ValueB</NodeB> 
     <NodeC>ValueC</NodeC> 
    </doc> 
    <doc> 
     <NodeA>ValueD</NodeA> 
     <NodeB>ValueE</NodeB> 
     <NodeC>ValueF</NodeC> 
    </doc> 
    </result> 
</response>