Xml XSLT多重转换
我有这样的XML:Xml XSLT多重转换,xml,xslt,Xml,Xslt,我有这样的XML: <Entity> <Type>1</Type> <Number>111</Number> <GroupList> <Group> <Id>1001</Id> <FieldsList> <Field>
<Entity>
<Type>1</Type>
<Number>111</Number>
<GroupList>
<Group>
<Id>1001</Id>
<FieldsList>
<Field>
<Name>foo</Name>
<Value>123</Value>
<Type>String</Type>
</Polje>
</FieldList>
</Group>
<Group>
<Id>1002</Id>
<FieldsList>
<Field>
<Name>bar</Name>
<Value>456</Value>
<Type>String</Type>
</Field>
</Field>
</Group>
</GroupList>
</Entity>
<Entity>
<Type>1</Type>
<Number>111</Number>
<GroupList>
<Group>1001: foo123String;</Group>
<Group>1002: bar456String;</Group>
</GroupList>
</Entity>
1.
111
1001
福
123
一串
1002
酒吧
456
一串
通过XSLT转换,我希望实现以下目标:
<Entity>
<Type>1</Type>
<Number>111</Number>
<GroupList>1001:foo,123,String;1002:bar,456,String</GroupList>
</Entity>
1.
111
1001:foo,123,String;1002:bar,456,String
当我使用这个转换时
<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="Field">
<xsl:copy>
<xsl:value-of select=
"concat(Name,',',Value,',',Type)
"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Group">
<xsl:copy>
<xsl:value-of select=
"concat(Id, ': ',FieldsList, ';')
"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我得到的结果如下:
<Entity>
<Type>1</Type>
<Number>111</Number>
<GroupList>
<Group>
<Id>1001</Id>
<FieldsList>
<Field>
<Name>foo</Name>
<Value>123</Value>
<Type>String</Type>
</Polje>
</FieldList>
</Group>
<Group>
<Id>1002</Id>
<FieldsList>
<Field>
<Name>bar</Name>
<Value>456</Value>
<Type>String</Type>
</Field>
</Field>
</Group>
</GroupList>
</Entity>
<Entity>
<Type>1</Type>
<Number>111</Number>
<GroupList>
<Group>1001: foo123String;</Group>
<Group>1002: bar456String;</Group>
</GroupList>
</Entity>
1.
111
1001:foo123String;
1002:BAR456串;
是否可以在不同的节点级别上使用两个不同的模板进行连接?我认为您需要删除一些
xsl:copy
,并且需要使用xsl:apply templates
:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<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="Field">
<xsl:value-of select=
"concat(Name,',',Value,',',Type)
"/>
</xsl:template>
<xsl:template match="Group">
<xsl:if test="position() > 1">;</xsl:if>
<xsl:value-of select="concat(Id, ':')"/>
<xsl:apply-templates select="FieldList/Field"/>
</xsl:template>
</xsl:stylesheet>
;