XSLT: XML to JSON – If key/s repeated in data, Merge the Key/s data into one Key concatenate with ";"- "KEY1": "Rate1: – 2000; Rate2: – 3000"

huangapple go评论45阅读模式
英文:

XSLT: XML to JSON - If key/s repeated in data, Merge the Key/s data into one Key concatenate with ";"- "KEY1": "Rate1: - 2000; Rate2: - 3000"

问题

XSLT:合并“LineTransaction”关键列,如果有多个!!

请参考以下XML和JSON,并分享正确的XSLT代码

Source XML Data:

  <wd:Report_Entry>
    <wd:InvoiceRefID>INVOICE-9999</wd:InvoiceRefID>
    <wd:InvoiceNumber>999-1234</wd:InvoiceNumber>
    <wd:InvoiceLine>
    <wd:InvoiceLineRefID>INVOICE_LINE-9999-1</wd:InvoiceLineRefID>
    <wd:LineTransaction wd:Descriptor="Tax Rate1: - 2000">
    <wd:ID wd:type="WID">83888d0f7bb710017f5517e62ca00001</wd:ID>
    </wd:LineTransaction>
    <wd:LineTransaction wd:Descriptor="Vertex Tax Rate2: - 3000">
    <wd:ID wd:type="WID">83888d0f7bb710017f55174bfa7a0002</wd:ID>
    </wd:LineTransaction>
    </wd:InvoiceLine>
    </wd:Report_Entry>

请参考以下XSLT代码,

XSLT Code

<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:wd="urn:com.workday/InvoiceAndInvoiceLine" version="3.0">
	<xsl:output method="text"/>
	<xsl:variable name="linefeed" select="' '"/>
	<xsl:variable name="delimiter" select="'|'"/>
	<xsl:template match="/">
		<xsl:text>[
</xsl:text>
		<xsl:apply-templates select="wd:Report_Data/wd:Report_Entry"/>
        <xsl:text>
]</xsl:text>
	</xsl:template>

	<xsl:template match="wd:Report_Entry">
		<xsl:text>  {
 </xsl:text>
		<xsl:apply-templates select="* except wd:InvoiceLine"/>
		<xsl:text>,
"invoiceLine": [ 
 </xsl:text>
		<xsl:apply-templates select="wd:InvoiceLine/*[not(self::wd:LineTransaction)]"/>
        <xsl:text>
   ] 
   } </xsl:text>
		<xsl:if test="position() != last()">
			<xsl:text>,
</xsl:text>
		</xsl:if>
	</xsl:template>
	<xsl:template match="wd:LineTransaction">
        <xsl:if test="position() != 1">
            <xsl:text>;
</xsl:text>
        </xsl:if>
        <xsl:value-of select="@wd:Descriptor"/>
	</xsl:template>
	<xsl:template match="wd:*">
		<xsl:text> </xsl:text>
		<xsl:choose> 
			<xsl:when test="@wd:Descriptor"><xsl:value-of select="concat('"',local-name(),'":"',@wd:Descriptor,'"')"/></xsl:when>
			<xsl:otherwise><xsl:value-of select="concat('"',local-name(),'":"',.,'"')"/></xsl:otherwise>
		</xsl:choose>
		<xsl:if test="position() != last()">
			<xsl:text>,
</xsl:text>
		</xsl:if>
	</xsl:template>
	<xsl:template match="wd:InvoiceLine">
		<xsl:text>   { 
</xsl:text>
		<xsl:apply-templates select="* except wd:LineTransaction"/>
        <xsl:text>,
 </xsl:text>
		<xsl:apply-templates select="wd:LineTransaction"/>
		<xsl:text>
   }</xsl:text>
		<xsl:if test="position() != last()">
			<xsl:text>,
 </xsl:text>
		</xsl:if>		
	</xsl:template>
	<xsl:template match="wd:InvoiceLine/*">
		<xsl:choose> 
			<xsl:when test="@wd:Descriptor">
			<xsl:value-of select="concat('"',local-name(),'":"',@wd:Descriptor,'"')"/>
			</xsl:when>
			<xsl:otherwise>
			<xsl:value-of select="concat('&
英文:

XSLT : Merge "LineTransaction" Key column if more than one !!

Please refer below XML and JSON and share the correct XSLT Code

Source XML Data :

  <wd:Report_Entry>
    <wd:InvoiceRefID>INVOICE-9999</wd:InvoiceRefID>
    <wd:InvoiceNumber>999-1234</wd:InvoiceNumber>
    <wd:InvoiceLine>
    <wd:InvoiceLineRefID>INVOICE_LINE-9999-1</wd:InvoiceLineRefID>
    <wd:LineTransaction wd:Descriptor="Tax Rate1: - 2000">
    <wd:ID wd:type="WID">83888d0f7bb710017f5517e62ca00001</wd:ID>
    </wd:LineTransaction>
    <wd:LineTransaction wd:Descriptor="Vertex Tax Rate2: - 3000">
    <wd:ID wd:type="WID">83888d0f7bb710017f55174bfa7a0002</wd:ID>
    </wd:LineTransaction>
    </wd:InvoiceLine>
    </wd:Report_Entry>

Please refer below XSLT Code,

XSLT Code

<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:wd="urn:com.workday/InvoiceAndInvoiceLine" version="3.0">
	<xsl:output method="text"/>
	<xsl:variable name="linefeed" select="' '"/>
	<xsl:variable name="delimiter" select="'|'"/>
	<xsl:template match="/">
		<xsl:text>[
</xsl:text>
		<xsl:apply-templates select="wd:Report_Data/wd:Report_Entry"/>
        <xsl:text>
]</xsl:text>
	</xsl:template>

	<xsl:template match="wd:Report_Entry">
		<xsl:text>  {
 </xsl:text>
		<xsl:apply-templates select="* except wd:InvoiceLine"/>
		<xsl:text>,
"invoiceLine": [ 
 </xsl:text>
		<xsl:apply-templates select="wd:InvoiceLine"/>
		<xsl:text>
   ] 
   } </xsl:text>
		<xsl:if test="position() != last()">
			<xsl:text>,
</xsl:text>
		</xsl:if>
	</xsl:template>
	<xsl:template match="wd:*">
		<xsl:text> </xsl:text>
		<xsl:choose> 
			<xsl:when test="@wd:Descriptor"><xsl:value-of select="concat('"',local-name(),'":"',@wd:Descriptor,'"')"/></xsl:when>
			<xsl:otherwise><xsl:value-of select="concat('"',local-name(),'":"',.,'"')"/></xsl:otherwise>
		</xsl:choose>
		<xsl:if test="position() != last()">
			<xsl:text>,
</xsl:text>
		</xsl:if>
	</xsl:template>
	<xsl:template match="wd:InvoiceLine">
		<xsl:text>   { 
</xsl:text>
		<xsl:apply-templates select="*"/>
		<xsl:text>
   }</xsl:text>
		<xsl:if test="position() != last()">
			<xsl:text>,
 </xsl:text>
		</xsl:if>		
	</xsl:template>
	<xsl:template match="wd:InvoiceLine/*">
		<xsl:choose> 
			<xsl:when test="@wd:Descriptor">
			<xsl:value-of select="concat('"',local-name(),'":"',@wd:Descriptor,'"')"/>
			</xsl:when>
			<xsl:otherwise>
			<xsl:value-of select="concat('"',local-name(),'":"',.,'"')"/>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:if test="position() != last()">
			<xsl:text>,
 </xsl:text>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet>

Here is JSON Output:

[
 {
 "InvoiceRefID":"INVOICE-9999",
 "InvoiceNumber":"999-1234",
 "invoiceLine": [ 
	{ 
	  "InvoiceLineRefID":"INVOICE_LINE-9999-1",
	  "LineTransaction": "Tax Rate1: - 2000",
      "LineTransaction": "Tax Rate2: - 3000"
	}
  ]
  }
]

Here is Expected JSON Output: above XSLT code not produced right outout

[
	 {
	 "InvoiceRefID":"INVOICE-9999",
	 "InvoiceNumber":"999-1234",
	 "invoiceLine": [ 
		{ 
		  "InvoiceLineRefID":"INVOICE_LINE-9999-1",
		  "LineTransaction": "Tax Rate1: - 2000; Tax Rate2: - 3000"
		}
	  ]
	  }
	]

Please verify above and share right solution.

答案1

得分: 1

您的XSLT声明了version="3.0"。如果您的处理器支持XSLT 3.0,我建议您执行以下操作:

XSLT 3.0

<xsl:stylesheet version="3.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/2005/xpath-functions"
xmlns:wd="urn:com.workday/InvoiceAndInvoiceLine">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="/">
    <!-- 转换输入为JSON的XML -->
    <xsl:variable name="xml">
        <xsl:apply-templates/>
    </xsl:variable>
    <!-- 输出 -->
    <xsl:value-of select="xml-to-json($xml)"/>
</xsl:template>

<xsl:template match="wd:Report_Data">
    <array>
        <xsl:apply-templates/>
    </array>
</xsl:template>

<xsl:template match="wd:Report_Entry">
    <map>
        <xsl:apply-templates/>
    </map>
</xsl:template>

<xsl:template match="wd:InvoiceRefID | wd:InvoiceNumber">
    <string key="{local-name()}">
        <xsl:value-of select="."/>
    </string>
</xsl:template>

<xsl:template match="wd:InvoiceLine">
    <array key="invoiceLine">
        <map>
            <string key="InvoiceLineRefID">
                <xsl:value-of select="wd:InvoiceLineRefID"/>
            </string>
            <string key="LineTransaction">
                <xsl:value-of select="wd:LineTransaction/@wd:Descriptor" separator="; "/>
            </string>
        </map>
    </array>
</xsl:template>
</xsl:stylesheet>

对于一个格式良好的XML输入:

XML

<wd:Report_Data xmlns:wd="urn:com.workday/InvoiceAndInvoiceLine">
    <wd:Report_Entry>
        <wd:InvoiceRefID>INVOICE-9999</wd:InvoiceRefID>
        <wd:InvoiceNumber>999-1234</wd:InvoiceNumber>
        <wd:InvoiceLine>
            <wd:InvoiceLineRefID>INVOICE_LINE-9999-1</wd:InvoiceLineRefID>
            <wd:LineTransaction wd:Descriptor="Tax Rate1: - 2000">
                <wd:ID wd:type="WID">83888d0f7bb710017f5517e62ca00001</wd:ID>
            </wd:LineTransaction>
            <wd:LineTransaction wd:Descriptor="Vertex Tax Rate2: - 3000">
                <wd:ID wd:type="WID">83888d0f7bb710017f55174bfa7a0002</wd:ID>
            </wd:LineTransaction>
        </wd:InvoiceLine>
    </wd:Report_Entry>
</wd:Report_Data>

将生成:

Result

[{"InvoiceRefID":"INVOICE-9999","InvoiceNumber":"999-1234","invoiceLine":[{"InvoiceLineRefID":"INVOICE_LINE-9999-1","LineTransaction":"Tax Rate1: - 2000; Vertex Tax Rate2: - 3000"}]}]
英文:

Your XSLT declares version=&quot;3.0&quot;. If your processor supports XSLT 3.0 then I would suggest you do:

XSLT 3.0

&lt;xsl:stylesheet version=&quot;3.0&quot; 
xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
xmlns=&quot;http://www.w3.org/2005/xpath-functions&quot;
xmlns:wd=&quot;urn:com.workday/InvoiceAndInvoiceLine&quot;&gt;
&lt;xsl:output method=&quot;text&quot; encoding=&quot;UTF-8&quot;/&gt;

&lt;xsl:template match=&quot;/&quot;&gt;
	&lt;!-- CONVERT INPUT TO XML FOR JSON --&gt;
	&lt;xsl:variable name=&quot;xml&quot;&gt;
		&lt;xsl:apply-templates/&gt;
	&lt;/xsl:variable&gt;
	&lt;!-- OUTPUT --&gt;
	&lt;xsl:value-of select=&quot;xml-to-json($xml)&quot;/&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;wd:Report_Data&quot;&gt;
	&lt;array&gt;
		&lt;xsl:apply-templates/&gt;
	&lt;/array&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;wd:Report_Entry&quot;&gt;
	&lt;map&gt;
		&lt;xsl:apply-templates/&gt;
	&lt;/map&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;wd:InvoiceRefID | wd:InvoiceNumber&quot;&gt;
	&lt;string key=&quot;{local-name()}&quot;&gt;
		&lt;xsl:value-of select=&quot;.&quot;/&gt;
	&lt;/string&gt;
&lt;/xsl:template&gt;

&lt;xsl:template match=&quot;wd:InvoiceLine&quot;&gt;
	&lt;array key=&quot;invoiceLine&quot;&gt;
		&lt;map&gt;
			&lt;string key=&quot;InvoiceLineRefID&quot;&gt;
				&lt;xsl:value-of select=&quot;wd:InvoiceLineRefID&quot;/&gt;
			&lt;/string&gt;
			&lt;string key=&quot;LineTransaction&quot;&gt;
				&lt;xsl:value-of select=&quot;wd:LineTransaction/@wd:Descriptor&quot; separator=&quot;; &quot;/&gt;
			&lt;/string&gt;
		&lt;/map&gt;
	&lt;/array&gt;
&lt;/xsl:template&gt;
	
&lt;/xsl:stylesheet&gt;

With a well-formed (!) XML input:

XML

&lt;wd:Report_Data xmlns:wd=&quot;urn:com.workday/InvoiceAndInvoiceLine&quot;&gt;
	&lt;wd:Report_Entry&gt;
		&lt;wd:InvoiceRefID&gt;INVOICE-9999&lt;/wd:InvoiceRefID&gt;
		&lt;wd:InvoiceNumber&gt;999-1234&lt;/wd:InvoiceNumber&gt;
		&lt;wd:InvoiceLine&gt;
			&lt;wd:InvoiceLineRefID&gt;INVOICE_LINE-9999-1&lt;/wd:InvoiceLineRefID&gt;
			&lt;wd:LineTransaction wd:Descriptor=&quot;Tax Rate1: - 2000&quot;&gt;
				&lt;wd:ID wd:type=&quot;WID&quot;&gt;83888d0f7bb710017f5517e62ca00001&lt;/wd:ID&gt;
			&lt;/wd:LineTransaction&gt;
			&lt;wd:LineTransaction wd:Descriptor=&quot;Vertex Tax Rate2: - 3000&quot;&gt;
				&lt;wd:ID wd:type=&quot;WID&quot;&gt;83888d0f7bb710017f55174bfa7a0002&lt;/wd:ID&gt;
			&lt;/wd:LineTransaction&gt;
		&lt;/wd:InvoiceLine&gt;
	&lt;/wd:Report_Entry&gt;
&lt;/wd:Report_Data&gt;

this will produce:

Result

[{&quot;InvoiceRefID&quot;:&quot;INVOICE-9999&quot;,&quot;InvoiceNumber&quot;:&quot;999-1234&quot;,&quot;invoiceLine&quot;:[{&quot;InvoiceLineRefID&quot;:&quot;INVOICE_LINE-9999-1&quot;,&quot;LineTransaction&quot;:&quot;Tax Rate1: - 2000; Vertex Tax Rate2: - 3000&quot;}]}]

huangapple
  • 本文由 发表于 2023年3月21日 03:25:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定