我需要找到重复和空节点,而不是从XML中删除它们,所以我写了下面的XSL,并且能够通过读取XML来获取空节点列表。但是我是如果存在Duplicate节点,则无法获得结果。需要你的帮助来实现这一目标。
下面是输入XML:
<p:Organisation xmlns:p="http://www.tibco.com/schemas/prjDelimeter/Schema/Schema2.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.tibco.com/schemas/prjDelimeter/Schema/Schema2.xsd HeaderRecords.xsd ">
<p:EMP>
<p:ID>123</p:ID>
<p:Name>uday</p:Name>
<p:Designation>SoftwareEng</p:Designation>
<p:ExpertiseIn>SOA,OSB,TIBCO</p:ExpertiseIn>
</p:EMP>
<p:ASSETS>
<p:ASSET>
<p:AssetID>1000</p:AssetID>
<p:Name>Ego</p:Name>
<p:InUse>yes</p:InUse>
<p:AssignedDate>2005</p:AssignedDate>
</p:ASSET>
<p:ASSET>
<p:AssetID>2000</p:AssetID>
<p:Name>HP</p:Name>
<p:InUse></p:InUse>
<p:AssignedDate>2002</p:AssignedDate>
</p:ASSET>
<p:ASSET>
<p:AssetID>3000</p:AssetID>
<p:Name>Dell</p:Name>
<p:InUse>yes</p:InUse>
<p:AssignedDate>2010</p:AssignedDate>
</p:ASSET>
<p:ASSET>
<p:AssetID>4000</p:AssetID>
<p:Name></p:Name>
<p:InUse>yes</p:InUse>
<p:AssignedDate>2009</p:AssignedDate>
</p:ASSET>
<p:ASSET>
<p:AssetID>3000</p:AssetID>
<p:Name>Lenovo</p:Name>
<p:InUse>yes</p:InUse>
<p:AssignedDate>2011</p:AssignedDate>
</p:ASSET>
</p:ASSETS>
</p:Organisation>
以下是我看到任何空节点时获得的当前输出:
InUse in Line- 2 with Position-3 is empty
Name in Line- 4 with Position-2 is empty
下面的是预期的输出:
InUse in Line- 2 with Position-3 is empty
Name in Line- 4 with Position-2 is empty
Found Duplicate Asset ID's
下面是我已经工作并且能够获得到空节点列表的XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:p="http://www.tibco.com/schemas/prjDelimeter/Schema/Schema2.xsd">
<xsl:output omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:apply-templates mode="rule1"
select="p:Organisation/p:EMP/p:ID">
</xsl:apply-templates>
<xsl:apply-templates mode="rule2"
select="p:Organisation/p:EMP/p:Name">
</xsl:apply-templates>
<xsl:apply-templates mode="rule3"
select="p:Organisation/p:EMP/p:Designation">
</xsl:apply-templates>
<xsl:apply-templates mode="rule4"
select="p:Organisation/p:EMP/p:ExpertiseIn">
</xsl:apply-templates>
<xsl:apply-templates mode="rule5"
select="p:Organisation/p:ASSETS">
</xsl:apply-templates>
</xsl:template>
<!-- Employee data Validation -->
<xsl:template match="p:ID" mode="rule1">
<xsl:if test="current() = ''">
Employee ID is empty.
</xsl:if>
</xsl:template>
<xsl:template match="p:Name" mode="rule2">
<xsl:if test="current() = ''">
Employee Name is empty.
</xsl:if>
</xsl:template>
<xsl:template match="p:Designation" mode="rule3">
<xsl:if test="current() = ''">
Employee Designation is empty.
</xsl:if>
</xsl:template>
<xsl:template match="p:ExpertiseIn" mode="rule4">
<xsl:if test="current() = ''">
ExpertiseIn data can't be empty.
</xsl:if>
</xsl:template>
<!-- Assets data Validation -->
<xsl:template match="p:ASSETS/*" mode='rule5'>
<xsl:variable name="i" select="count(preceding-sibling::*)+1" />
<xsl:for-each select="child::*">
<xsl:if test="current() = '' ">
<xsl:value-of select="local-name()" /> in Line- <xsl:value-of select="$i" /> with Position-<xsl:value-of select="count(preceding-sibling::*)+1" /> is empty
</xsl:if>
</xsl:for-each>
</xsl:template>
请帮我找到重复的节点。
感谢您的帮助。
答案 0 :(得分:0)
要最小化手头问题的示例,请考虑以下样式表:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:p="http://www.tibco.com/schemas/prjDelimeter/Schema/Schema2.xsd">
<xsl:output method="text" encoding="UTF-8" />
<xsl:key name="asset" match="p:ASSET" use="p:AssetID" />
<xsl:template match="/p:Organisation">
<!-- other stuff -->
<xsl:if test="p:ASSETS/p:ASSET[count(key('asset', p:AssetID)) > 1]">Found Duplicate Asset ID's</xsl:if>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:0)
我刚才发现,即使我们可以使用generate-id()概念实现此要求,如下所示:
<xsl:if test="p:ASSETS/p:ASSET[generate-id()= generate-id(key('asset',p:AssetID)[2])]">Found Duplicate Asset ID's</xsl:if>
答案 2 :(得分:0)
这个简短的转换会生成一个以空格分隔的列表,列出所有具有重复值的p:AssetID
元素:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:p="http://www.tibco.com/schemas/prjDelimeter/Schema/Schema2.xsd">
<xsl:output method="text"/>
<xsl:key name="kAsseIdByVal" match="p:AssetID" use="."/>
<xsl:template match="p:AssetID[generate-id()=generate-id(key('kAsseIdByVal', .)[2])]">
<xsl:value-of select="concat(., ' ')"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
应用于此XML文档(提供的一个+多一个pAsset
元素,以便再有一组重复值):
<p:Organisation xmlns:p="http://www.tibco.com/schemas/prjDelimeter/Schema/Schema2.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.tibco.com/schemas/prjDelimeter/Schema/Schema2.xsd HeaderRecords.xsd ">
<p:EMP>
<p:ID>123</p:ID>
<p:Name>uday</p:Name>
<p:Designation>SoftwareEng</p:Designation>
<p:ExpertiseIn>SOA,OSB,TIBCO</p:ExpertiseIn>
</p:EMP>
<p:ASSETS>
<p:ASSET>
<p:AssetID>1000</p:AssetID>
<p:Name>Ego</p:Name>
<p:InUse>yes</p:InUse>
<p:AssignedDate>2005</p:AssignedDate>
</p:ASSET>
<p:ASSET>
<p:AssetID>2000</p:AssetID>
<p:Name>HP</p:Name>
<p:InUse></p:InUse>
<p:AssignedDate>2002</p:AssignedDate>
</p:ASSET>
<p:ASSET>
<p:AssetID>3000</p:AssetID>
<p:Name>Dell</p:Name>
<p:InUse>yes</p:InUse>
<p:AssignedDate>2010</p:AssignedDate>
</p:ASSET>
<p:ASSET>
<p:AssetID>4000</p:AssetID>
<p:Name></p:Name>
<p:InUse>yes</p:InUse>
<p:AssignedDate>2009</p:AssignedDate>
</p:ASSET>
<p:ASSET>
<p:AssetID>3000</p:AssetID>
<p:Name>Lenovo</p:Name>
<p:InUse>yes</p:InUse>
<p:AssignedDate>2011</p:AssignedDate>
</p:ASSET>
<p:ASSET>
<p:AssetID>4000</p:AssetID>
<p:Name></p:Name>
<p:InUse>yes</p:InUse>
<p:AssignedDate>2009</p:AssignedDate>
</p:ASSET>
</p:ASSETS>
</p:Organisation>
产生了想要的正确结果:
3000 4000