我需要改变这个结构
<A>
<B>value1</B>
</A>
<A>
<B>value2</B>
</A>转到
<A>
<B>value1<B>
<B>value2<B>
</A>使用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> 但我需要:
<?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>发布于 2012-05-24 20:33:47
由于您似乎需要折叠单个节点下的所有子节点,因此您不需要"A“上的foreach,您可以直接移动到"B”子节点
<?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:for-each select="//A/B">
<B>
<xsl:value-of select="./text()"/>
</B>
</xsl:for-each>
</A>
</xsl:template>
</xsl:stylesheet>根据@Sean的评论编辑,请注意,在现实生活中永远不应该使用//。将//替换为实际根元素中的路径。
发布于 2012-05-24 22:10:54
这个样式表...
<?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>..。进入这个..。
<A><B>value1</B><B>value2</B></A>发布于 2012-05-25 10:30:24
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文档获得)上应用此转换时的:
<t>
<A>
<B>value1</B>
</A>
<A>
<B>value2</B>
</A>
</t>生成所需的正确结果::
<t>
<A>
<B>value1</B>
<B>value2</B>
</A>
</t>https://stackoverflow.com/questions/10737378
复制相似问题