[英]Distinct Values in XSLT
我已經編寫了xslt代碼,以所需的格式轉換xml代碼。 但是父節點每次都在子節點之后重復。 我想先顯示父節點,然后再顯示其子節點。 請讓我知道我可以在代碼中做什么以獲得所需的輸出。
Transform.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:app="http://www.eee.com/app">
<xsl:output method="text" />
<xsl:strip-space elements="*" />
<xsl:template match="//*">
<xsl:param name="prefix" />
<xsl:param name="inLast" select="true()" />
<xsl:value-of select="$prefix"/>
<xsl:text>
</xsl:text>
<xsl:value-of select="concat( local-name(), ' ', normalize-space())"/>
<xsl:if test="not($inLast) or position() != last()">
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:template>
<xsl:template match="//*[*]">
<xsl:param name="inLast" select="true()" />
<xsl:variable name="num">
<xsl:number />
</xsl:variable>
<xsl:apply-templates>
<xsl:with-param name="prefix" select="local-name()" />
<xsl:with-param name="inLast" select="$inLast and position() = last()" />
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>
Input.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<ns0:LogDeliveryDocumentNotification xmlns:ns0="http://fmc.fmcworld.com/pi/MTD/LogDelivery">
<Employee1>
<Name>ABC</Name>
<Age>25</Age>
</Employee1>
<Employee2>
<Name>DEF</Name>
<Age>26</Age>
</Employee2>
</ns0:LogDeliveryDocumentNotification>
輸出:
Employee1
Name ABC
Employee1
Age 25
Employee2
Name DEF
Employee2
Age 26
我希望輸出看起來像這樣:
Employee1
Name ABC
Age 25
Employee2
Name DEF
Age 26
您的輸入中沒有重復的值,因此無需提取不同的值。 您想要的結果可以通過以下方式輕松產生:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/*">
<xsl:for-each select="*">
<xsl:value-of select="name()" />
<xsl:text> </xsl:text>
<xsl:for-each select="*">
<xsl:value-of select="name()" />
<xsl:text> </xsl:text>
<xsl:value-of select="." />
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.