如何转换xml块?
我是XSL的新手,在任何地方都找不到像下面这样的案例。我想将source.xml转换为target.xml。我使用了“组”模式,但它对我不起作用(可能我不能正确使用它) source.xml:如何转换xml块?,xml,xslt,transformation,mode,Xml,Xslt,Transformation,Mode,我是XSL的新手,在任何地方都找不到像下面这样的案例。我想将source.xml转换为target.xml。我使用了“组”模式,但它对我不起作用(可能我不能正确使用它) source.xml: <?xml version="1.0" encoding="ISO-8859-1"?> <PersonBody> <Person> <D>Name</D> <D>Surname</D&
<?xml version="1.0" encoding="ISO-8859-1"?>
<PersonBody>
<Person>
<D>Name</D>
<D>Surname</D>
<D>Id</D>
</Person>
<PersonValues>
<D>Michael</D>
<D>Jackson</D>
<D>01</D>
</PersonValues>
<PersonValues>
<D>James</D>
<D>Bond</D>
<D>007</D>
</PersonValues>
<PersonValues>
<D>Kobe</D>
<D>Bryant</D>
<D>24</D>
</PersonValues>
</PersonBody>
名称
姓
身份证件
迈克尔
杰克逊
01
詹姆斯
债券
007
科比
布莱恩特
24
target.xml:
<PersonBody>
<AllValues>
<Name>
<D>Michael</D>
<D>James</D>
<D>Kobe</D>
</Name>
<Surname>
<D>Jackson</D>
<D>Bond</D>
<D>Bryant</D>
</Surname>
<Id>
<D>01</D>
<D>007</D>
<D>24</D>
</Id>
</AllValues>
</PersonBody>
试试看:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="kColumnValue" match="PersonValues/*"
use="count(preceding-sibling::*)" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<AllValues>
<xsl:apply-templates select="Person/*" />
</AllValues>
</xsl:copy>
</xsl:template>
<xsl:template match="Person/*">
<xsl:element name="{.}">
<xsl:apply-templates select="key('kColumnValue', position() - 1)" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
在示例XML上运行时,结果是:
<PersonBody>
<AllValues>
<Name>
<D>Michael</D>
<D>James</D>
<D>Kobe</D>
</Name>
<Surname>
<D>Jackson</D>
<D>Bond</D>
<D>Bryant</D>
</Surname>
<Id>
<D>01</D>
<D>007</D>
<D>24</D>
</Id>
</AllValues>
</PersonBody>
迈克尔
詹姆斯
科比
杰克逊
债券
布莱恩特
01
007
24
请告诉我您尝试了什么?
中的第一个
是空的,但这只是一个输入错误吗?您是否试图用
替换
?@serhiy.h不,
在这里不会产生正确的结果。我使用了
,因为这会产生正确的结果。谢谢@JLRishe,但输出与我给出的不同。很抱歉。我试图为请求的输出开发xsl,但没有成功。我将编辑我的问题以获得输出。再次谢谢,对不起。这将开启一个新的问题。谢谢