xslt - XSLT :: 选择以下段直到下一个标记
问题描述
我有以下输入 XML。连续的 E_Records 是可选的,应该填充到 L_Record 中。我编写了以下 XSLT 编码。我应该做些什么改变吗?
输入 XML
<?xml version="1.0" encoding="UTF-8"?>
<Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>3</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>4</E_Qty>
</E_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
</Record>
<Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>1</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>2</E_Qty>
</E_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>5</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>6</E_Qty>
</E_Record>
</Record>
我期待的输出 XML 是
<Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>3</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>4</E_Qty>
</E_Record>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
</Record>
<Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>1</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>2</E_Qty>
</E_Record>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>5</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>6</E_Qty>
</E_Record>
</L_Record>
</Record>
我已经为此编写了 XSLT 映射,如下所示,但我没有得到所需的输出。你能帮我解决这个问题吗?
<?xml version="1.0" encoding="UTF-8"?>
<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="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Record/L_Record">
<L_Record>
<xsl:variable name="header" select="."/>
<xsl:apply-templates/>
<xsl:if test = "not(following-sibling::L_Record)">
<xsl:for-each select="following-sibling::E_Record[preceding-sibling::L_Record = $header]">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:if>
</L_Record>
</xsl:template>
</xsl:stylesheet>
请帮我解决这个问题?
当我执行上述代码时,记录 1 工作正常,但记录 2 工作不正常。E-Record 段未出现在 L-Record 段中。
解决方案
这是group-starting-with
马丁提到的一个例子......
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Record">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:for-each-group select="*" group-starting-with="L_Record">
<xsl:copy>
<xsl:apply-templates select="@*,node(),current-group()[self::E_Record]"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
推荐阅读
- intellij-idea - 如何在 IntelliJ IDEA 的调试器中通过引用来观察特定的 Java 对象?
- python - 点击功能不适用于投票元素
- java - org.springframework.orm.ObjectOptimisticLockingFailureException 在一个事务方法中,在 Spring 作业中具有 shedlock 和 hibernate
- c++ - 无法定义返回源文件(cpp)内的成员类的模板类的函数成员类
- python - Python中的等效R函数h2o.transform
- frama-c - Frama-C的Eva插件分析完后报“无效的用户输入”
- c# - 如何加载图像并从任何地方访问它?
- python - 在 python gdb 脚本中索引 c++ 向量
- reactjs - 在我的应用程序中获取用于设置 MapView 的用户坐标会生成未定义的值
- java - 用于异步处理的 Akka?