如何使用XSLT-1.0转换xml结构
我需要改变这个结构如何使用XSLT-1.0转换xml结构,xml,xslt,transform,xslt-1.0,Xml,Xslt,Transform,Xslt 1.0,我需要改变这个结构 <A> <B>value1</B> </A> <A> <B>value2</B> </A> 价值1 价值2 进入 价值1 价值2 使用XSLT-1.0的最佳解决方案是什么? 谢谢大家! PS:我尝试了以下代码: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.1" xm
<A>
<B>value1</B>
</A>
<A>
<B>value2</B>
</A>
价值1
价值2
进入
价值1
价值2
使用XSLT-1.0的最佳解决方案是什么?
谢谢大家!
PS:我尝试了以下代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="group_a" match="//A" use="B"/>
<xsl:template match="/Test"> <a-node> <xsl:for-each select="//A"> <b-node>
<xsl:value-of select="//A/B"/> </b-node> </xsl:for-each> </a-node>
</xsl:template>
</xsl:stylesheet>
但它只返回第一个值:
<?xml version="1.0" encoding="utf-8"?> <a-node mlns:fo="http://www.w3.org/1999/XSL/Format"> <b-node>value1</b-node> <b-node>value1</b-node> </a-node>
value1 value1
但我需要:
<?xml version="1.0" encoding="utf-8"?> <a-node xmlns:fo="http://www.w3.org/1999/XSL/Format"> <b-node>value1</b-node> <b-node>value2</b-node> </a-node>
value1值2
由于您似乎需要将所有子节点折叠到单个节点下,因此不需要“a”上的foreach,您可以直接移动到“B”子节点
编辑根据@Sean的评论,请注意//不应用于现实生活中。将//替换为实际根元素的路径。此样式表
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
<xsl:template match="/">
<A>
<xsl:apply-templates select="*/A/B"/>
</A>
</xsl:template>
<xsl:template match="B">
<B><xsl:value-of select="."/></B>
</xsl:template>
</xsl:stylesheet>
。。。将改变
<root>
<A>
<B>value1</B>
</A>
<A>
<B>value2</B>
</A>
</root>
价值1
价值2
。。。进入这个
<A><B>value1</B><B>value2</B></A>
value1value2
此转换使用最基本的XSLT设计模式之一——覆盖标识转换。因此,更容易编写、理解、维护和扩展:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="A[1]">
<A>
<xsl:apply-templates select="node()|following-sibling::A/node()"/>
</A>
</xsl:template>
<xsl:template match="A"/>
</xsl:stylesheet>
将此转换应用于以下XML文档时(通过将提供的XML片段包装到单个顶部元素中获得,以使其成为格式良好的XML文档):
价值1
价值2
生成所需的正确结果:
<t>
<A>
<B>value1</B>
<B>value2</B>
</A>
</t>
价值1
价值2
功能正确但效率低下。最好使用模板样式并避免不必要地使用//运算符。@Sean-sure-同意从不使用//但别无选择,因为OP的xml示例没有根元素:)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="A[1]">
<A>
<xsl:apply-templates select="node()|following-sibling::A/node()"/>
</A>
</xsl:template>
<xsl:template match="A"/>
</xsl:stylesheet>
<t>
<A>
<B>value1</B>
</A>
<A>
<B>value2</B>
</A>
</t>
<t>
<A>
<B>value1</B>
<B>value2</B>
</A>
</t>