如果指定的子节点存在于该节点中,则用于创建一组元素的父节点的XSLT代码(XSLT code for Creating parent of a group of elements if the spe

编程入门 行业动态 更新时间:2024-10-12 05:48:20
如果指定的子节点存在于该节点中,则用于创建一组元素的父节点的XSLT代码(XSLT code for Creating parent of a group of elements if the specified children exist in that node)

带有命名空间的示例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有另外两个元素VectorTypecd 。 这些元素不应受到影响。

我是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

本文发布于:2023-07-19 04:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1173695.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   则用   元素   代码   code

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!