Xml 在XSLT中连接节点值
我需要基于以下内容连接XML值: 以下是我的XML:Xml 在XSLT中连接节点值,xml,xslt,Xml,Xslt,我需要基于以下内容连接XML值: 以下是我的XML: <?xml version="1.0" encoding="utf-8"?> <Root> <Parent> <Name>Father 1</Name> <Child> <Name>F1 - Child 1</Name> <Age>2</Age> </Child>
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Parent>
<Name>Father 1</Name>
<Child>
<Name>F1 - Child 1</Name>
<Age>2</Age>
</Child>
<Child>
<Name>F1- Child 2</Name>
<Age>4</Age>
</Child>
</Parent>
<Parent>
<Name>Father 2</Name>
<Child>
<Name>F2 - Child 1</Name>
<Age>2</Age>
</Child>
<Child>
<Name>F2 - Child 2</Name>
<Age>4</Age>
</Child>
</Parent>
</Root>
父亲1
F1-儿童1
2.
F1-儿童2
4.
父亲2
F2-儿童1
2.
F2-儿童2
4.
这是我的XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match ="Root">
<xsl:apply-templates select ="Parent/Name" />
<xsl:apply-templates select ="Parent/Child" />
</xsl:template>
<xsl:template match ="Parent/Name">
<FatherName>
<xsl:value-of select ="."/>
</FatherName>
</xsl:template>
<xsl:template match ="Parent/Child">
<ChildNames>
<xsl:for-each select="Name">
<xsl:value-of select="." />
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</ChildNames>
</xsl:template>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
,
这是什么结果
<?xml version="1.0" encoding="utf-8"?>
<FatherName>Father 1</FatherName>
<FatherName>Father 2</FatherName>
<ChildNames>F1 - Child 1</ChildNames>
<ChildNames>F1- Child 2</ChildNames>
<ChildNames>F2 - Child 1</ChildNames>
<ChildNames>F2 - Child 2</ChildNames>
父亲1
父亲2
F1-儿童1
F1-儿童2
F2-儿童1
F2-儿童2
但我需要的结果是这样的:
<FatherName>Father 1</FatherName>
<ChildNames>F1 - Child 1, F1- Child 2</ChildNames>
<FatherName>Father 2</FatherName>
<ChildNames>F2 - Child 1, F2 - Child 2</ChildNames>
<xsl:template match="Parent/Name">
<FatherName>
<xsl:value-of select ="."/>
</FatherName>
<ChildNames>
<xsl:apply-templates select ="../Child" />
</ChildNames>
</xsl:template>
父亲1
F1-子1,F1-子2
父亲2
F2-子1,F2-子2
有人能帮我得到所需的结果吗?基本上,您需要做的是将行
移出当前模板,并移到与父/名
匹配的模板中。大概是这样的:
<FatherName>Father 1</FatherName>
<ChildNames>F1 - Child 1, F1- Child 2</ChildNames>
<FatherName>Father 2</FatherName>
<ChildNames>F2 - Child 1, F2 - Child 2</ChildNames>
<xsl:template match="Parent/Name">
<FatherName>
<xsl:value-of select ="."/>
</FatherName>
<ChildNames>
<xsl:apply-templates select ="../Child" />
</ChildNames>
</xsl:template>
想出这个
<xsl:template match="/">
<good>
<xsl:for-each select="Root/Parent">
<FatherName>
<xsl:value-of select="Name"/>
</FatherName>
<xsl:variable name="childCount" select="count(.//Child)"/>
<ChildNames>
<xsl:variable name="data">
<xsl:for-each select=".//Child">
<xsl:value-of select="Name"/>
<xsl:if test="not(position()=$childCount)">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$data"/>
</ChildNames>
</xsl:for-each>
</good>
</xsl:template>
,