[英]extract child node to create XML with respective parent node
我有一個類似以下結構的輸入XML。
<Root>
<STDS>
<DEPT>111</DEPT>
<COD>123</COD>
<PIN>100</PIN>
</STDS>
<STDS>
<DEPT>222</DEPT>
<COD>234</COD>
<PIN>200</PIN>
<DETS>
<NAM>ABC</NAM>
<AGE>20</AGE>
</DETS>
</STDS>
<STDS>
<DEPT>333</DEPT>
<COD>345</COD>
<PIN>300</PIN>
<DETS>
<NAM>XYZ</NAM>
<AGE>21</AGE>
</DETS>
<DETS>
<NAM>ZZZ</NAM>
<AGE>21</AGE>
</DETS>
</STDS>
</Root>
我正在使用以下代碼來解決這個問題。 我正在使用一個for-each循環,將所有STDS都轉換為,而另一個for-each則提取子DETS並將其與父DETS節點合並。 但是由於每個,它把所有節點放在一起。 而子節點(DETS)要求位於相應父節點的正下方。
<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0 userCSharp" version="1.0" xmlns:ns0="Students" xmlns:s0="STDS" xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
<xsl:template match="/">
<xsl:apply-templates select="/s0:Root" />
</xsl:template>
<xsl:template match="/s0:Root">
<ns0:Students>
<xsl:for-each select="s0:Root/s0:STDS">
<Students>
<Department>
<xsl:value-of select="s0:DEPT" />
</Department>
<Code>
<xsl:value-of select="s0:COD" />
</Code>
<Pin>
<xsl:value-of select="s0:PIN" /></Pin>
<Details>
<Name><xsl:value-of select="NAM" /></Name>
<Age><xsl:value-of select="AGE" /></Age>
</Details>
</Students>
</xsl:for-each>
<xsl:for-each select="s0:STDS/s0:DETS">
<Students>
<Department>
<xsl:value-of select="../DEPT" />
</Department>
<Code>
<xsl:value-of select="../COD" />
</Code>
<Pin>
<xsl:value-of select="../PIN" />
</Pin>
<Details>
<Name><xsl:value-of select="s0:NAM" /></Name>
<Age><xsl:value-of select="s0:AGE" /></Age>
</Details>
</Students>
</xsl:for-each>
我得到以下輸出。 由於每個,所有提取的節點都在末尾。
<Students>
<Department>222</Department>
<Code>234</Code>
<Pin>200</Pin>
</Students>
<Students>
<Department>333</Department>
<Code>345</Code>
<Pin>300</Pin>
</Students>
<Students>
<Department>222</Department>
<Code>234</Code>
<Pin>200</Pin>
<Details>
<Name>ABC</Name>
<Age>20</Age>
</Details>
</Students>
<Students>
<Department>333</Department>
<Code>345</Code>
<Pin>300</Pin>
<Details>
<Name>XYZ</Name>
<Age>21</Age>
</Details>
</Students>
<Students>
<Department>333</Department>
<Code>345</Code>
<Pin>300</Pin>
<Details>
<Name>ZZZ</Name>
<Age>21</Age>
</Details>
</Students>
而預期的輸出如下。
<Students>
<Department>111</Department>
<Code>123</Code>
<Pin>100</Pin>
</Students>
<Students>
<Department>222</Department>
<Code>234</Code>
<Pin>200</Pin>
</Students>
<Students>
<Department>222</Department>
<Code>234</Code>
<Pin>200</Pin>
<Details>
<Name>ABC</Name>
<Age>20</Age>
</Details>
</Students>
<Students>
<Department>333</Department>
<Code>345</Code>
<Pin>300</Pin>
</Students>
<Students>
<Department>333</Department>
<Code>345</Code>
<Pin>300</Pin>
<Details>
<Name>XYZ</Name>
<Age>21</Age>
</Details>
</Students>
<Students>
<Department>333</Department>
<Code>345</Code>
<Pin>300</Pin>
<Details>
<Name>ZZZ</Name>
<Age>21</Age>
</Details>
</Students>
這是一個非常奇怪的要求-但可以通過以下方法輕松實現:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Root">
<xsl:for-each select="STDS">
<xsl:variable name="common">
<Department>
<xsl:value-of select="DEPT" />
</Department>
<Code>
<xsl:value-of select="COD" />
</Code>
<Pin>
<xsl:value-of select="PIN" />
</Pin>
</xsl:variable>
<!-- DEPARTMENT -->
<Students>
<xsl:copy-of select="$common"/>
</Students>
<!-- STUDENTS -->
<xsl:for-each select="DETS">
<Students>
<xsl:copy-of select="$common"/>
<Details>
<Name>
<xsl:value-of select="NAM" />
</Name>
<Age>
<xsl:value-of select="AGE" />
</Age>
</Details>
</Students>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
請注意,結果是一個XML片段(沒有單個根元素)。
考慮避免使用多個xsl:for-each
(通常是使用循環而不是XSLT的遞歸樣式的應用層語言范例),並使用有條件地應用Details節點的模板來處理需求(如果存在):
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/Root">
<xsl:copy>
<xsl:apply-templates select="STDS"/>
</xsl:copy>
</xsl:template>
<xsl:template match="STDS">
<Students>
<Department><xsl:value-of select="DEPT"/></Department>
<Code><xsl:value-of select="COD"/></Code>
<Pin><xsl:value-of select="PIN"/></Pin>
</Students>
<xsl:apply-templates select="DETS"/>
</xsl:template>
<xsl:template match="DETS">
<Students>
<Department><xsl:value-of select="../DEPT"/></Department>
<Code><xsl:value-of select="../COD"/></Code>
<Pin><xsl:value-of select="../PIN"/></Pin>
<Details>
<Name><xsl:value-of select="NAM"/></Name>
<Age><xsl:value-of select="AGE"/></Age>
</Details>
</Students>
</xsl:template>
</xsl:transform>
在Mapper中,您需要做的就是通過循環Functoid將<DETS>
連接到<Students>
。
就是這樣,不需要自定義xslt。
一個注意事項:根據架構的組成,可能需要從<STDS>
到<Student>
另一個Looping Functoid。 嘗試不同的組合非常容易。
盡管從技術上講它們可能是正確的,但Xsl Answers並不是在BizTalk中執行此操作的正確方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.