带有命名空间的示例XML文档:
<TXLife xmlns="some web address" Version="2.22.00"> <TXLifeRequest> <TransRefGUID>2345678</TransRefGUID> <!--*so many children in hierarchy * .... .......--> <Vector> <VectorType>Base Premium</VectorType> <cd>1<cd> <V>4659.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>656.00</V> <V>656.00</V> <V>656.00</V> <!-- .................--> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>9735.50</V> <V>9735.50</V> </Vector> <Vector> <VectorType>SLI Rider Premium</VectorType> </Vector> <Vector> <VectorType>DWP Rider Premium</VectorType> </Vector> </TXLifeRequest> </TXLife>我想要一个XML输出,如果Vector Element有V个孩子,它们应该被分组在一个名为Group的父单元中,否则按原样打印。 Vector有另外两个元素VectorType和cd 。 这些元素不应受到影响。
我是XSLT的新手,这是我的代码不工作:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="Vector/V"> <xsl:element name="Group"> <xsl:element name="{name()}"> <xsl:value-of select="."/> </xsl:element> </xsl:element> <xsl:apply-templates select="* | node()"/> </xsl:template> </xsl:stylesheet>请帮忙
预期产出:
<TXLife xmlns="some web address" Version="2.22.00"> <TXLifeRequest> <TransRefGUID>2345678</TransRefGUID> <!--*so many children in hierarchy * ....LEAVE THEM AS THEY ARE .......--> <Vector> <VectorType tc="1">Base Premium</VectorType> <!--All V elements go in a single group--> <Group> <V>4659.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>656.00</V> <V>656.00</V> <V>656.00</V> <!-- .................--> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>9735.50</V> <V>9735.50</V> </Group> </Vector> <Vector> <VectorType>SLI Rider Premium</VectorType> </Vector> <Vector> <VectorType>DWP Rider Premium</VectorType> </Vector> </TXLifeRequest>具有如此多的孩子V的向量元素重新出现。
My sample XML document with namespace:
<TXLife xmlns="some web address" Version="2.22.00"> <TXLifeRequest> <TransRefGUID>2345678</TransRefGUID> <!--*so many children in hierarchy * .... .......--> <Vector> <VectorType>Base Premium</VectorType> <cd>1<cd> <V>4659.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>656.00</V> <V>656.00</V> <V>656.00</V> <!-- .................--> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>9735.50</V> <V>9735.50</V> </Vector> <Vector> <VectorType>SLI Rider Premium</VectorType> </Vector> <Vector> <VectorType>DWP Rider Premium</VectorType> </Vector> </TXLifeRequest> </TXLife>I want an XML output where ,if Vector Element has got V children, they should be grouped in a single parent named as Group,else print as they are. Vector has got other two elements VectorType and cd. These elements should not be affected.
I am a newbie in XSLT and here is my code which is not Working:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="Vector/V"> <xsl:element name="Group"> <xsl:element name="{name()}"> <xsl:value-of select="."/> </xsl:element> </xsl:element> <xsl:apply-templates select="* | node()"/> </xsl:template> </xsl:stylesheet>Please help
Expected Output:
<TXLife xmlns="some web address" Version="2.22.00"> <TXLifeRequest> <TransRefGUID>2345678</TransRefGUID> <!--*so many children in hierarchy * ....LEAVE THEM AS THEY ARE .......--> <Vector> <VectorType tc="1">Base Premium</VectorType> <!--All V elements go in a single group--> <Group> <V>4659.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>159.50</V> <V>656.00</V> <V>656.00</V> <V>656.00</V> <!-- .................--> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>4808.00</V> <V>9735.50</V> <V>9735.50</V> </Group> </Vector> <Vector> <VectorType>SLI Rider Premium</VectorType> </Vector> <Vector> <VectorType>DWP Rider Premium</VectorType> </Vector> </TXLifeRequest>The Vector Element with so many children V ,re-occurs.
最满意答案
这个样式表是否适合您:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="some web address" exclude-result-prefixes="ns" version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="ns:Vector/ns:V[1]"> <Group xmlns="some web address"> <xsl:copy-of select="../ns:V"/> </Group> </xsl:template> <xsl:template match="ns:Vector/ns:V" priority="0"/> </xsl:stylesheet>如果元素的顺序无关紧要,您可以使用以下内容替换上面的2nd和3rd模板:
<xsl:template match="ns:Vector"> <xsl:apply-templates select="@* | node()[not(self::ns:V)]"/> <Group xmlns="some web address"> <xsl:apply-templates select="ns:V"/> </Group> </xsl:template>Would this stylesheet work for you:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="some web address" exclude-result-prefixes="ns" version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="ns:Vector/ns:V[1]"> <Group xmlns="some web address"> <xsl:copy-of select="../ns:V"/> </Group> </xsl:template> <xsl:template match="ns:Vector/ns:V" priority="0"/> </xsl:stylesheet>And if the order of elements does not matter, you can replace the 2nd and 3rd template above with the following one:
<xsl:template match="ns:Vector"> <xsl:apply-templates select="@* | node()[not(self::ns:V)]"/> <Group xmlns="some web address"> <xsl:apply-templates select="ns:V"/> </Group> </xsl:template>更多推荐
Vector,<xsl,电脑培训,计算机培训,IT培训"/> <meta name="description&qu
发布评论