如何将具有由 a 分隔的值的属性分解为单独的元素 [英] How do I explode an attribute with values separated by a , into seperate elements
问题描述
我有一个属性,它的值可能是一个或多个文本字符串,所有字符串都用逗号分隔.我希望使用 XSL 将属性值转换为它们自己的元素;
I have a an attribute who's value may be one or more text strings all delimited by a comma. I wish to transform using XSL the attribute value(s) into their own element;
例如
<post title='Hello World" tags="Test,Hello,World />
我希望它变成什么样;
<post>
<title>Hello World</title>
<tag>Test</tag>
<tag>Hello</tag>
<tag>World</tag>
</post>
这可能吗?TIA
推荐答案
有几种方法可以做到这一点.
There are several ways to do this.
我.在 XSLT 1.0 中使用递归调用的命名模板这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*[not(name()='tags')]">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="@tags">
<xsl:call-template name="tokenize">
<xsl:with-param name="pText"
select="concat(., ',')"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="pText"/>
<xsl:if test="string-length($pText)">
<tag>
<xsl:value-of select=
"substring-before($pText, ',')"/>
</tag>
<xsl:call-template name="tokenize">
<xsl:with-param name="pText" select=
"substring-after($pText, ',')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
应用于最初提供的 XML 文档时(更正为格式良好):
<post title="Hello World"
tags="Test,Hello,World" />
产生所需的结果:
<post>
<title>Hello World</title>
<tag>Test</tag>
<tag>Hello</tag>
<tag>World</tag>
</post>
二.使用 FXSL 1.xstr-split-to-words
模板/函数
II. Using the str-split-to-words
template/function from FXSL 1.x
此处 FXSL 提供标记化功能:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
>
<xsl:import href="strSplit-to-Words.xsl"/>
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*[not(name()='tags')]">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="@tags">
<xsl:variable name="vwordNodes">
<xsl:call-template name="str-split-to-words">
<xsl:with-param name="pStr" select="."/>
<xsl:with-param name="pDelimiters"
select="','"/>
</xsl:call-template>
</xsl:variable>
<xsl:apply-templates select="ext:node-set($vwordNodes)/*"/>
</xsl:template>
<xsl:template match="word">
<tag>
<xsl:value-of select="."/>
</tag>
</xsl:template>
</xsl:stylesheet>
应用于与以前相同的 XML 文档时,会产生相同的正确输出.
三.使用 XPath 2.0 标准函数 tokenize()
来自 XSLT 2.0 转换
这是最简单的方法 -- 如果可以使用 XSLT 2.0 处理器.
This is the easiest way -- if one can use an XSLT 2.0 processor.
以下 XSLT 2.0 转换:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*[not(name()='tags')]">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template match="@tags">
<xsl:for-each select="tokenize(.,',')">
<tag><xsl:value-of select="."/></tag>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
再次应用于同一个 XML 文档时会产生想要的结果.
这篇关于如何将具有由 a 分隔的值的属性分解为单独的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!