根据 XSLT 中的字段值创建重复元素



下面是具有重复的reasoncase元素的输入xml。如果 reasonCase 下的代码的值以"申请人"开头,则创建申请人。

<input>
    <case>
        <reasonCase>
            <code>Applicant#1234</code>
            <key>asd345</key>
        </reasonCase>
        <reasonCase>
            <code>Applicant#1234</code>
            <key>asd34567</key>
        </reasonCase>
        <reasonCase>
            <code>Applicant#3456</code>
            <key>asd1111</key>
        </reasonCase>
        <reasonCase>
            <code>Applicant#7889</code>
            <key>asd9999</key>
        </reasonCase>
    </case>
</input>

预期输出是 reasoncase 下的代码具有相同值的案例应位于输出中的同一申请人元素中,相应的元素也应位于同一申请人元素中

<output>
        <case>
            <applicant>
                <objectid>1234</objectid>
                <reason>
                    <key>asd345</key>
                </reason>
                <reason>
                    <key>asd34567</key>
                </reason>
            </applicant>
            <applicant>
                <objectid>3456</objectid>
                <reason>
                    <key>asd1111</key>
                </reason>
            </applicant>
            <applicant>
                <objectid>7889</objectid>
                <reason>
                    <key>asd9999</key>
                </reason>
            </applicant>
        </case>
    </output>

基于原因案例/代码=开头为(申请人(必须创建重复的元素申请人和相应的元素。
请找到我尝试过的以下xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml"/>
        <xsl:template match="/">
            <output>
                <xsl:for-each select="/input/case/reasonCase">
                    <xsl:variable name="code">
                        <xsl:value-of select="./code"/>
                    </xsl:variable>
                    <xsl:choose>
                        <xsl:when test="starts-with(./code,'Applicant')">
                            <Applicants>
                                <objectId>
                                    <xsl:value-of select="substring-after(/*/*/*[code=$code]/code,'Applicant#')"/>
                                </objectId>
                                <reason>
                                    <key><xsl:value-of select="/*/*/*[code=$code]/key"/></key>
                                </reason>
                            </Applicants>
                        </xsl:when>
                    </xsl:choose>
                </xsl:for-each>
            </output>
        </xsl:template>
    </xsl:stylesheet>

我得到的输出如下

<output>
    <Applicants>
        <objectId>1234</objectId>
        <reason>
            <key>asd345</key>
        </reason>
    </Applicants>
    <Applicants>
        <objectId>1234</objectId>
        <reason>
            <key>asd345</key>
        </reason>
    </Applicants>
    <Applicants>
        <objectId>3456</objectId>
        <reason>
            <key>asd1111</key>
        </reason>
    </Applicants>
    <Applicants>
        <objectId>7889</objectId>
        <reason>
            <key>asd9999</key>
        </reason>
    </Applicants>
</output>

请帮助我以获得预期的输出。

对于 Xsl 1.0,您可以使用

<xsl:template match="input">
<output>
    <case>
        <xsl:for-each select="//code[.!=preceding::code or not(preceding::code)]">
            <xsl:variable name="code" select="text()"/>
            <Applicants>
                <objectid><xsl:value-of select="substring-after(text(), '#')"/></objectid>
                <xsl:for-each select="//key[preceding-sibling::code = $code]">
                    <reason>
                        <xsl:copy-of select="."/>
                    </reason>
                </xsl:for-each>
            </Applicants>
        </xsl:for-each>
    </case>
</output>

为此输出为

    <?xml version="1.0" encoding="UTF-8"?>
<output>
   <case>
      <Applicants>
         <objectid>1234</objectid>
         <reason>
            <key>asd345</key>
         </reason>
         <reason>
            <key>asd34567</key>
         </reason>
      </Applicants>
      <Applicants>
         <objectid>3456</objectid>
         <reason>
            <key>asd1111</key>
         </reason>
      </Applicants>
      <Applicants>
         <objectid>7889</objectid>
         <reason>
            <key>asd9999</key>
         </reason>
      </Applicants>
   </case>
</output>

xslt

此问题
<xsl:template match="input">
    <output>
        <case>
            <xsl:for-each-group select="//code" group-by="text()">
                <xsl:variable name="code" select="text()"/>
                <applicant>
                    <objectid><xsl:value-of select="substring-after(text(), '#')"/></objectid>
                    <xsl:for-each select="//key[preceding-sibling::code = $code]">
                        <reason>
                            <xsl:copy-of select="."/>
                        </reason>
                    </xsl:for-each>
                </applicant>
            </xsl:for-each-group>
        </case>
    </output>
</xsl:template>

和输出

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <case>
      <applicant>
         <objectid>1234</objectid>
         <reason>
            <key>asd345</key>
         </reason>
         <reason>
            <key>asd34567</key>
         </reason>
      </applicant>
      <applicant>
         <objectid>3456</objectid>
         <reason>
            <key>asd1111</key>
         </reason>
      </applicant>
      <applicant>
         <objectid>7889</objectid>
         <reason>
            <key>asd9999</key>
         </reason>
      </applicant>
   </case>
</output>

相关内容

  • 没有找到相关文章

最新更新