我有以下XML 此resultSet来自数据库调用,它表示卡帐户与其首选项代码之间的多对多关系。 卡可能与电子邮件相关联-(DEST_EML_ADDR)和电子邮件选择(EML_PVT_TYPE_CD),并且这两个标签在多行中的同一卡号也将具有相同的值。 该卡还将具有唯一的偏好代码和一 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我是xslt的新手,因此无法理解这里提供的复杂解决方案。 以非Muenchian方式对沉重的数据结构进行分组(这意味着存在大量的属性和元素,因此无法与任何串联的解决方案一起使用)。 我觉得串联大数据会影响性能。 这是我尝试的示例输入数据,必需的输出和xsl:
<out:OuterSegment>
<out:Segment >
<out:from Code="CHN"/>
<out:tothis Code="HYD"/>
<out:group>0</out:group>
</out:Segment>
<out:Segment >
<out:from Code="HYD"/>
<out:tothis Code="BLR"/>
<out:group>1</out:group>
</out:Segment>
<out:Segment >
<out:from Code="BLR"/>
<out:tothis Code="TVN"/>
<out:group>2</out:group>
</out:Segment>
<out:Segment >
<out:from Code="TVN"/>
<out:tothis Code="DEL"/>
<out:group>2</out:group>
</out:Segment>
</out:OuterSegment>
<out2:OuterSegment>
<out2:Segment >
<out2:from Code="CHN"/>
<out2:tothis Code="HYD"/>
<out2:group>0</out2:group>
</out2:Segment>
</out2:OuterSegment>
<out2:OuterSegment>
<out2:Segment >
<out2:from Code="HYD"/>
<out2:tothis Code="BLR"/>
<out2:group>1</out2:group>
</out2:Segment>
</out2:OuterSegment>
<out2:OuterSegment>
<out2:Segment >
<out2:from Code="BLR"/>
<out2:tothis Code="TVN"/>
<out2:group>2</out2:group>
</out2:Segment>
</out2:OuterSegment>
<out2:OuterSegment>
<out2:Segment >
<out2:from Code="TVN"/>
<out2:tothis Code="DEL"/>
<out2:group>2</out2:group>
</out2:Segment>
</out2:OuterSegment>
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:message> its the first row </xsl:message>
<out:OuterSegment>
<xsl:apply-templates select="in2:IncomingSegment"
mode="localIncomingSegmentRefToSegment" />
</out:OuterSegment>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="prevgroup" select="//in2:IncomingSegment[position() - 1]/@Group"/>
<xsl:message> is the subsequent row</xsl:message>
<xsl:if test="$prevgroup = //in2:IncomingSegment[$prevgroup]/@Group">
<xsl:apply-templates select="in2:IncomingSegment"
mode="localIncomingSegmentRefToSegment" />
</xsl:if>
<xsl:if test="$prevgroup != //in2:IncomingSegment[$prevgroup]/@Group">
<out:OuterSegment>
<xsl:apply-templates select="in2:IncomingSegment"
mode="SegmentRefToSegment" />
</out:OuterSegment>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
我需要提到的是,上面的输入xml是由另一个xslt生成的,其输入为:
<in:RefSegment key="1" group="0">
<in:inSegment >
<in:from Code="CHN"/>
<in:tothis Code="HYD"/>
</in:inSegment>
</in:RefSegment>
<in:RefSegment key="2" group="1">
<in:inSegment >
<in:from Code="HYD"/>
<in:tothis Code="BLR"/>
</in:inSegment>
</in:RefSegment>
<in:RefSegment key="3" group="2">
<in:inSegment >
<in:from Code="BLR"/>
<in:tothis Code="TVN"/>
</in:inSegment>
</in:RefSegment>
<in:RefSegment key="4" group="2">
<in:inSegment >
<in:from Code="TVN"/>
<in:tothis Code="BLR"/>
</in:inSegment>
</in:RefSegment>
<in:DataSegments>
<in:Data >
<in:keyref> 0 </in:keyref>
</in:Data>
<in:Data >
<in:keyref> 1 </in:keyref>
</in:Data>
<in:Data >
<in:keyref> 2 </in:keyref>
</in:Data>
<in:Data >
<in:keyref> 2 </in:keyref>
</in:Data>
<in:DataSegments>
是否可以从上述实际的input.xml中一次性获得最终的output.xml?
我不知道您如何处理有关in
和out2
前缀的输入和输出xml的名称空间,以及名称空间是否值得关注,所以也许这种方法没有用,但我想分享一下这个想法。
这是基于以下假设<in:RefSegment>
您的实际输入XML建议,所有<in:Data>
节点都位于与<in:RefSegment>
对应的位置-如果不是这种情况,请将其添加到您的问题中,这样必须对<in:RefSegment>
的group
属性的值和相应的<in:keyref>
值进行<in:keyref>
。
作为对输入XML代码段的调整,我将<in:RefSegment>
节点包装在具有示例名称空间: <root xmlns:in="http://www.example.com">
并添加了示例名称空间xmlns:in="http://www.example.com"
和xmlns:out2="http://www.example2.com"
到样式表声明。 由于存在xmlns:out2
命名空间,因此将该命名空间添加到每个out2:OuterSegment
节点。
跟随XSLT
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:in="http://www.example.com" xmlns:out2="http://www.example2.com"
exclude-result-prefixes="in" version="1.0">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="in:RefSegment">
<xsl:variable name="position" select="position()"/>
<out2:OuterSegment>
<out2:Segment>
<out2:from>
<xsl:attribute name="Code">
<xsl:value-of select="in:inSegment/in:from/@Code" />
</xsl:attribute>
</out2:from>
<out2:tothis Code="HYD">
<xsl:attribute name="Code">
<xsl:value-of select="in:inSegment/in:tothis/@Code" />
</xsl:attribute>
</out2:tothis>
<out2:group>
<xsl:value-of select="normalize-space(//in:DataSegments/in:Data[$position]/in:keyref)"/>
</out2:group>
</out2:Segment>
</out2:OuterSegment>
</xsl:template>
<xsl:template match="in:DataSegments"/>
</xsl:transform>
通过调整缺少的结束</in:DataSegments>
应用于实际的输入XML时,将产生以下结果:
<out2:OuterSegment xmlns:out="http://www.example2.com">
<out2:Segment>
<out2:from Code="CHN"/>
<out2:tothis Code="HYD"/>
<out2:group>0</out2:group>
</out2:Segment>
</out2:OuterSegment>
<out2:OuterSegment xmlns:out="http://www.example2.com">
<out2:Segment>
<out2:from Code="HYD"/>
<out2:tothis Code="BLR"/>
<out2:group>1</out2:group>
</out2:Segment>
</out2:OuterSegment>
<out2:OuterSegment xmlns:out="http://www.example2.com">
<out2:Segment>
<out2:from Code="BLR"/>
<out2:tothis Code="TVN"/>
<out2:group>2</out2:group>
</out2:Segment>
</out:OuterSegment>
<out:OuterSegment xmlns:out="http://www.example2.com">
<out2:Segment>
<out2:from Code="TVN"/>
<out2:tothis Code="BLR"/>
<out2:group>2</out2:group>
</out2:Segment>
</out2:OuterSegment>
in:RefSegment
匹配的模板in:RefSegment
节点<xsl:template match="in:RefSegment">
生成<out2:OuterSegment>
节点。 <xsl:variable name="position" select="position()"/>
是当前位置:RefSegment。
生成的out2:from
和out2:tothis
节点的属性是从in:RefSegment
当前处理的检索到的:
<out2:from>
<xsl:attribute name="Code">
<xsl:value-of select="in:inSegment/in:from/@Code" />
</xsl:attribute>
</out2:from>
以及从in:Data
节点中位于相同位置的out2:group
的值:
<out2:group>
<xsl:value-of select="normalize-space(//in:DataSegments
/in:Data[$position/in:keyref)"/>
</out2:group>
带有附加的normalize-space()
来删除输入中的空间。
in:DataSegments
<xsl:template match="in:DataSegments"/>
为空,因此in:DataSegments
节点从输出中删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.