簡體   English   中英

需要使用基於不同屬性和分組的 XSLT2.0 基於另一個 XML 創建 XML

[英]Need to create XML based on another XML using XSLT2.0 based on distinct attributes and group by

我正在嘗試基於其他 2 個 XML 生成 XML。 我正在輪詢一個返回人員詳細信息的數據庫(查詢中可以返回 n 人)。 最終的 XML 應該具有確切數量的數據標簽作為來自 DB 的 XML 中的不同名稱標簽 例如:

第一個 XML- 從數據庫中獲取

<parent>
    <child>
        <name>John</name>
        <city>Boston</city>
    </child>
    <child>
        <name>John</name>
        <city>Seattle</city>
    </child>
    <child>
        <name>Allison</name>
        <city>Houston</city>
    </child>
    <child>
        <name>John</name>
        <city>Boston</city>
    </child>
</parent>

第二個 XML- 從另一個來源獲取這個

<details>
    <detail>
        <city>Boston</city>
        <code>abc</code>
    </detail>
    <detail>
        <city>Houston</city>
        <code>xyz</code>
    </detail>
</details>

首先,我需要創建 2 個數據標簽,因為有 2 個不同的名稱(John 和 Allison),然后我需要一一檢查第一個 XML 中所有標簽的城市標簽是否與在第 2 個 XML 中,然后在最終 XML 中創建詳細信息標簽並復制代碼標簽及其值。

1)如果沒有匹配的條目,不應該創建詳細信息標簽,因為沒有匹配的條目。 2)不,永遠不會有多個匹配條目。 數據標簽應基於 Distinct NAME 標簽和 details 標簽基於匹配城市標簽的數量創建。

最終生成的 XML

<FinalData>
    <Data>
        <name>John</name>
        <details>
            <city>Boston</city>
            <code>xyz</code>
        </details>
    </Data>
    <Data>
        <name>Allison</name>
        <details>
            <city>Houston</city>
            <code>abc</code>
        </details>
    </Data>
</FinalData>

編輯1:

我在填充代碼標簽時遇到問題,因為我必須匹配兩個 XML 中的城市標簽,並且需要一個 for 循環進行匹配。

以下是我的嘗試-

<?xml version="1.0" encoding="UTF-8"?>
<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:param name="otherFile" select="document('SOF Input 2.xml')"/>
    <xsl:template match="parent">
        <FinalData>
            <xsl:for-each-group select="child" group-by="name">
                <data>
                    <name>
                        <xsl:value-of select="name"/>
                    </name>
                    <details>
                        <city>
                            <xsl:value-of select="city"/>
                        </city>
                        <code>
                            <xsl:for-each select="$otherFile/details/detail">
                                <xsl:if test="parent/child/city='/city'">
                                    <!--I am getting stuck here - not able to get value here..i guess both city tags are referring to the same-->
                                    <xsl:value-of select="/code"/>
                                </xsl:if>
                            </xsl:for-each>
                        </code>
                    </details>
                </data>
                <xsl:text>
</xsl:text>
            </xsl:for-each-group>
        </FinalData>
    </xsl:template>
</xsl:stylesheet>

我用這個得到了下面的 output -

<?xml version="1.0" encoding="UTF-8"?>
<FinalData><data>
        <name>John</name>
        <details>
            <city>Boston</city>
            <code/>
        </details>
    </data>
<data>
        <name>Allison</name>
        <details>
            <city>Houston</city>
            <code/>
        </details>
    </data>
</FinalData>

它的縮進也略有錯誤。 如果我錯過了什么,請告訴我。

編輯2:

恐怕邁克爾提到的情況比預期的要早得多。 城市標簽將重復但在標簽下。 city標簽的值在標簽中是唯一的。 我必須從所有匹配的城市標簽中一個一個地獲取所有父級的值,並以級標簽中匹配的任何內容進入 output XML 中的相應詳細信息標簽的方式進行填充。 PFB 示例 XML,可以更好地解釋 -

第一個 XML -

<parent>
    <child>
        <name>John</name>
        <city>Boston</city>
    </child>
    <child>
        <name>John</name>
        <city>Seattle</city>
    </child>
    <child>
        <name>Allison</name>
        <city>Houston</city>
    </child>
    <child>
        <name>John</name>
        <city>Boston</city>
    </child>
</parent>

第二個 XML-

<details>
    <parent>
        <detail>
            <city>Boston</city>
            <code>abc</code>
        </detail>
        <detail>
            <city>Houston</city>
            <code>xyz</code>
        </detail>
    </parent>
    <parent>
        <detail>
            <city>Boston</city>
            <code>abc</code>
        </detail>
        <detail>
            <city>Seattle</city>
            <code>mno</code>
        </detail>
    </parent>
    <parent>
        <detail>
            <city>Houston</city>
            <code>xyz</code>
        </detail>
        <detail>
            <city>Seattle</city>
            <code>mno</code>
        </detail>
    </parent>
</details>

最終預期的 XML-

<FinalData>
    <Data>
        <name>John</name>
        <details>
            <detail>
                <city value="Boston">abc</city>
            </detail>
            <detail>
                <city value="Boston">abc</city>
                <city value="Seattle">mno</city>
            </detail>
            <detail>
                <city value="Seattle">mno</city>
            </detail>
        </details>
    </Data>
    <Data>
        <name>Allison</name>
        <details>
            <detail>
                <city value="Houston">xyz</city>
            </detail>
            <detail>
                <city value="Houston">xyz</city>
            </detail>
        </details>
    </Data>
</FinalData>

希望這很清楚,因為我不擅長解釋。

聲明一個密鑰

<xsl:key name="city" match="detail" use="city"/>

然后使用

<xsl:copy-of select="key('city', city, $otherFile)/code"/>

在您的for-each-group內部到 output 匹配代碼。

試試這個方法?

XSLT 2.0

<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="*"/>

<xsl:param name="otherFile" select="document('SOF Input 2.xml')"/>

<xsl:key name="detail-by-city" match="detail" use="city" />

<xsl:template match="/parent">
    <FinalData>
        <xsl:for-each-group select="child" group-by="name">
            <data>
                <xsl:copy-of select="name"/>
                <xsl:for-each select="key('detail-by-city', current-group()/city, $otherfile)">
                    <details>
                         <xsl:copy-of select="*"/>
                    </details>
                </xsl:for-each>
            </data>
        </xsl:for-each-group>
    </FinalData>
</xsl:template>

</xsl:stylesheet>

這是假設一個組在另一個文檔中可以有多個匹配條目,並且您希望為每個條目創建一個單獨的details節點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM