将多行XML标记拆分为多个节点

问题描述 投票:-1回答:1

我下面有一个XML,在Note__c标记的每一行之后添加新行。我需要通过将XML分成多个Note__c标签来生成XML。输入XML-

<?xml version="1.0" encoding="UTF-8"?>
<snotification>
	<data>
		<schema>yify-xjmoeLTbNXA560rHQ</schema>
		<payload>
			<Note__c>01/15/2020
			123456
			DFGRTE766
			6tgBFR</Note__c>
			<Line_Length__c>72.0</Line_Length__c>
			<CreatedById>00554000003OENsAAO</CreatedById>
			<Contact_Name__c/>
			<Sent_By_Name__c>SBM</Sent_By_Name__c>
			<CreatedDate>2020-01-15T16:10:40.551Z</CreatedDate>
			<Order_Number__c>14831</Order_Number__c>
			<Does_not_require_reformatting__c>false</Does_not_require_reformatting__c>
		</payload>
		<event>
			<replayId>139219</replayId>
		</event>
	</data>
	<channel>/event/Order_Note__e</channel>
</snotification>

其中Note__c包含多个字符串,并在每个字符串之后添加新行(最后一个除外)预期输出-

<?xml version="1.0" encoding="UTF-8"?>
<snotification>
	<data>
		<schema>yify-xjmoeLTbNXA560rHQ</schema>
		<payload>
      <Notes>
			<Note__c>01/15/2020</Note__c>
			<Note__c>123456</Note__c>
			<Note__c>DFGRTE766</Note__c>
			<Note__c>6tgBFR</Note__c>
      </Notes>
			<Line_Length__c>72.0</Line_Length__c>
			<CreatedById>00554000003OENsAAO</CreatedById>
			<Contact_Name__c/>
			<Sent_By_Name__c>SBM</Sent_By_Name__c>
			<CreatedDate>2020-01-15T16:10:40.551Z</CreatedDate>
			<Order_Number__c>14831</Order_Number__c>
			<Does_not_require_reformatting__c>false</Does_not_require_reformatting__c>
		</payload>
		<event>
			<replayId>139219</replayId>
		</event>
	</data>
	<channel>/event/Order_Note__e</channel>
</snotification>

我已经写了这个XSLT,但是它在有效负载元素下面缺少一些标签-

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="snotification/data/payload">
    <Notes>
            <xsl:for-each select="tokenize(Note__c,'\n')">
                <Note__c>
                    <xsl:value-of select="."/>
                </Note__c>
            </xsl:for-each>
        </Notes>
</xsl:template>
</xsl:stylesheet>

此输出-

<?xml version="1.0" encoding="UTF-8"?>
<snotification>
   <data>
      <schema>yify-xjmoeLTbNXA560rHQ</schema>
      <Notes>
         <Note__c>01/15/2020</Note__c>
         <Note__c>			123456</Note__c>
         <Note__c>			DFGRTE766</Note__c>
         <Note__c>			6tgBFR</Note__c>
      </Notes>
      <event>
         <replayId>139219</replayId>
      </event>
   </data>
   <channel>/event/Order_Note__e</channel>
</snotification>

不确定是否缺少什么。

谢谢杉田

xslt xslt-1.0 xslt-2.0 xslt-grouping
1个回答
0
投票

将您的XSLT更改为

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="snotification/data/payload/Note__c">
        <Notes>
            <xsl:for-each select="tokenize(.,'\n')">
                <Note__c>
                    <xsl:value-of select="normalize-space(.)"/>
                </Note__c>
            </xsl:for-each>
        </Notes>
    </xsl:template>
</xsl:stylesheet>

输出应为所需。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.