Xml 从列表中拾取元素
我需要将包含序列化逗号分隔列表的元素转换为单个XML元素,选择具有特定前缀的列表元素。结果列表的第一个组件应作为不同于其他组件的元素输出。 比如说,Xml 从列表中拾取元素,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我需要将包含序列化逗号分隔列表的元素转换为单个XML元素,选择具有特定前缀的列表元素。结果列表的第一个组件应作为不同于其他组件的元素输出。 比如说, <source>xxx-22-33, aa-11-11, aa-22-22, aa-33-33</source> xxx-22-33、aa-11-11、aa-22-22、aa-33-33 应该转化为 <ref>aa-11-11</ref> <xref>aa-22-22</xref
<source>xxx-22-33, aa-11-11, aa-22-22, aa-33-33</source>
xxx-22-33、aa-11-11、aa-22-22、aa-33-33
应该转化为
<ref>aa-11-11</ref>
<xref>aa-22-22</xref>
<xref>aa-33-33<xref>
aa-11-11
aa-22-22
aa-33-33
我为部分解决方案提出了以下模板,但在为特定处理选择第一个列表元素时遇到了困难。有什么建议吗?蒂亚
<xsl:template name="xrefs">
<xsl:param name="list"/>
<xsl:for-each select="tokenize($list, ', ')">
<xsl:variable name="el" select="."/>
<xsl:if test="starts-with($el, 'aa-')">
<xsl:element name="cve-id">
<xsl:value-of select="$el"/>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:template>
您不显示调用
外部参照
模板的模板定义。但是我可以给你一个没有命名模板的解决方案,即一个独立工作的完整样式表
样式表中有趣的部分是:
<xsl:variable name="element-name" select="if (position() = 1) then 'ref' else 'xref'"/>
XML输出
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="source">
<xsl:for-each select="tokenize(.,',')">
<xsl:variable name="element-name" select="if (position() = 1) then 'ref' else 'xref'"/>
<xsl:element name="{$element-name}">
<xsl:value-of select="normalize-space(.)"/>
</xsl:element>
</xsl:for-each>
</xsl:template>
</xsl:transform>
<?xml version="1.0" encoding="UTF-8"?>
<ref>xxx-22-33</ref>
<xref>aa-11-11</xref>
<xref>aa-22-22</xref>
<xref>aa-33-33</xref>
xxx-22-33
aa-11-11
aa-22-22
aa-33-33
请显示完整的XSLT样式表,而不仅仅是一个片段。特别是调用xrefs
模板的模板。感谢您的帮助。我使用的是XSLT2.0,您对第一个列表元素的假设是正确的。还要感谢normalize-space()-很高兴知道