首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将多行XML标签拆分成多个节点

将多行XML标签拆分成多个节点
EN

Stack Overflow用户
提问于 2020-01-16 08:24:01
回答 1查看 44关注 0票数 1

我在下面有一个XML,其中在Note__c标记的每一行后面都添加了新的行。我需要通过将它们拆分为多个Note__c标记来生成XML。输入XML-

代码语言:javascript
复制
<?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包含多个字符串,并在每个(除最后一个外)预期输出后添加新行-

代码语言:javascript
复制
<?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,但它在payload元素下缺少几个标记-

代码语言:javascript
复制
<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>

这个的输出是-

代码语言:javascript
复制
<?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>

不确定缺少了什么。

感谢Sugata

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-16 11:46:10

将XSLT更改为

代码语言:javascript
复制
<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>

输出应该是所需的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59761424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档