首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >比较两个相似的XPaths中的属性

比较两个相似的XPaths中的属性
EN

Stack Overflow用户
提问于 2022-01-25 14:11:29
回答 1查看 34关注 0票数 0

XML:

代码语言:javascript
运行
复制
    <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’。我试过了,但它并没有像预期的那样起作用。

在转变之后,我想:

代码语言:javascript
运行
复制
<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代码尝试过:

代码语言:javascript
运行
复制
<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不太熟悉。有人能帮帮我吗?

EN

回答 1

Stack Overflow用户

发布于 2022-01-25 15:14:10

如果属性的名称是city,那么您的XPath需要是@city而不是@City。其他属性名也是如此。

当然,在匹配元素EmpAdd这样的某个节点的上下文中,您需要相对于该元素的路径:

代码语言:javascript
运行
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70850120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档