XML:
<Emp id= "1">
<EmpAdd num = "1">
<Add city = "Brentwood" c2 = "TN" c3 = "US" c4 = "37027" c5="" c6=""/>
</EmpAdd>
<List>
<Emp id= "1">
<EmpAdd num = "1">
<Add city = "Oswego" c2 = "TN" c3 = "US" c7=""/>
</EmpAdd>
</Emp>
</List>
</Emp>我想比较两个元素名称相同的XPaths中的属性"Emp/EmpAdd/Add“和"List/Emp/EmpAdd/Add”。在两个XPaths中,属性值并不相同。
同样基于此,在Emp/List/Emp/@IsModified中添加一个新的属性IsModified,如果比较的(city、c2、c3)属性值与其他'N‘不同,则将该值设置为'Y’。我试过了,但它并没有像预期的那样起作用。
在转变之后,我想:
<Emp id="1">
<EmpAdd num="1">
<Add city="Brentwood" c2="TN" c3="US" c4="37027" c5="" c6=""/>
</EmpAdd>
<List>
<Emp id="1" IsModified="Y"/>
</List>
</Emp>但我在输出中得到了"N“。
XSLT代码尝试过:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match = "List/Emp/EmpAdd">
<xsl:choose>
<xsl:when test="((Emp/EmpAdd/Add/@City != List/Emp/EmpAdd/Add/@City)
or (Emp/EmpAdd/Add/@C2!= List/Emp/EmpAdd/Add/@C2)
or (Emp/EmpAdd/Add/@C3 != List/Emp/EmpAdd/Add/@C3)) ">
<xsl:attribute name="IsModified">Y</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="IsModified">N</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>我对XSLT不太熟悉。有人能帮帮我吗?
发布于 2022-01-25 15:14:10
如果属性的名称是city,那么您的XPath需要是@city而不是@City。其他属性名也是如此。
当然,在匹配元素EmpAdd这样的某个节点的上下文中,您需要相对于该元素的路径:
<xsl:template match = "List/Emp/EmpAdd">
<xsl:choose>
<xsl:when test="((../../../EmpAdd/Add/@city != Add/@city)
or (../../../EmpAdd/Add/@c2!= Add/@c2)
or (../../../EmpAdd/Add/@c3 != Add/@c3)) ">
<xsl:attribute name="IsModified">Y</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="IsModified">N</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:template>https://stackoverflow.com/questions/70850120
复制相似问题