根据发生次数更改xml节点的层次结构

时间:2019-04-12 16:32:59

标签: xml xslt xslt-1.0 xslt-2.0

我正在尝试根据到达的时间将节点移动到先前节点的同一级别。尝试使用XSLT代码。

Pay,Remit,Trailer可以在xml中重复。但是每笔付款之后都会有n个汇款节点。 在输出中。 pay应该包含它下面的所有汇款节点。

我尝试使用XSLT代码,但不知何故我没有得到预期的结果。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output  method="xml" encoding='UTF-8'/>
<!--Identity template, 
        provides default behavior that copies all content into the output -->

      <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:for-each select="pay">
    <xsl:template match="Remit">
        <xsl:value-of select="."/>
        </xsl:template>
    </xsl:for-each>


</xsl:stylesheet>

输入-

<?xml version="1.0" encoding="utf-8"?>
<Message>
<Record>
    <Header>
        <H></H>
    </Header>
    <Trailer>
        <AA>1</AA>
    </Trailer>
    <Pay>
        <BB>1</BB>
        <amount>11</amount>
    </Pay>
    <Remit>
        <Type>30</Type>
        <Transaction>I</Transaction>
    </Remit>
    <Trailer>
        <AA>1</AA>
    </Trailer>
    <Pay>
        <BB>1</BB>
        <amount>78</amount>
    </Pay>
    <Remit>
        <Type>30</Type>
        <Transaction>I</Transaction>
    </Remit>
    <Remit>
        <Type>30</Type>
        <Transaction>I</Transaction>
    </Remit>
    <Trailer>
        <AA>1</AA>
    </Trailer>
</Record>
</Message>

预期输出:

<?xml version="1.0" encoding="utf-8"?>
<Message>
<Record>
    <Header>
        <H>1</H>
    </Header>`enter code here`
    <Trailer>
        <AA>1</AA>
    </Trailer>
    <Pay>
        <BB>1</BB>
        <amount>11</amount>
        <Remit>
        <Type>30</Type>
        <Transaction>I</Transaction>
            </Remit>
    </Pay>
    <Trailer>
        <AA>1</AA>
    </Trailer>
    <Pay>
        <BB>1</BB>
        <amount>78</amount>
            <Remit>
        <Type>30</Type>
        <Transaction>I</Transaction>
            </Remit>
            <Remit>
        <Type>30</Type>
        <Transaction>I</Transaction>
            </Remit>
    </Pay>
    <Trailer>
        <AA>1</AA>
    </Trailer>
</Record>
</Message>

1 个答案:

答案 0 :(得分:0)

POST firma-2019.04/_update_by_query?conflicts=proceed { "script": { "source": "ctx._source.remove('person_full');ctx._source.remove('company_full')", "lang": "painless" } } https://www.w3.org/TR/xslt20/#xsl-for-each-group)的简单用例:

for-each-group group-starting-with

https://xsltfiddle.liberty-development.net/3NJ38Zm

对于XSLT 2处理器,将<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="#all" version="3.0"> <xsl:output indent="yes"/> <xsl:strip-space elements="*"/> <xsl:mode on-no-match="shallow-copy"/> <xsl:template match="Record"> <xsl:copy> <xsl:for-each-group select="*" group-starting-with="Pay"> <xsl:choose> <xsl:when test="self::Pay"> <xsl:copy> <xsl:apply-templates select="node(), current-group()[self::Remit]"/> </xsl:copy> <xsl:apply-templates select="(current-group() except .)[not(self::Remit)]"/> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="current-group()"/> </xsl:otherwise> </xsl:choose> </xsl:for-each-group> </xsl:copy> </xsl:template> </xsl:stylesheet> 声明替换为您的身份模板

xsl:mode