【问题标题】:Splitting Multiline XML tag into multiple Nodes将多行 XML 标记拆分为多个节点
【发布时间】:2020-01-16 00:24:01
【问题描述】:

我在下面有一个 XML,其中在 Note__c 标记的每一行之后添加了新行。我需要通过将它们拆分为多个 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】:

    将您的 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>
    

    输出应该符合要求。

    【讨论】:

    • 感谢 zx485 的帮助,它就像一个魅力。我还创建了一个 Java stringtokenizer 解决方案。为了这。这个 XSLT 比这更容易。
    猜你喜欢
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 2019-07-13
    • 1970-01-01
    • 2017-03-14
    • 2011-09-20
    • 1970-01-01
    相关资源
    最近更新 更多