如果指定的 children 存在于该节点中,则用于创建一组元素的 parent 的 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 children,它们应该被分组在一个名为 Group[=] 的 parent 中33=],否则按原样打印。
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>
Vector Element有这么多childrenV,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>
我的示例 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 children,它们应该被分组在一个名为 Group[=] 的 parent 中33=],否则按原样打印。 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>
Vector Element有这么多childrenV,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>