gpt4 book ai didi

xml - XSLT - 根据条件修改元素兄弟的值

转载 作者:数据小太阳 更新时间:2023-10-29 02:26:24 26 4
gpt4 key购买 nike

<?xml version="1.0" encoding="UTF-8"?>
<schools>
<city>Marshall</city>
<state>Maryland</state>
<highschool>
<schoolname>Marshalls</schoolname>
<department id="1">
<deptCode>D1</deptCode>
<deptName>Chemistry</deptName>
<deptHead>Henry Carl</deptHead>
</department>
<department id="2">
<deptCode>D2</deptCode>
<deptName>Science-Physics</deptName>
<deptHead>Martin Sean</deptHead>
</department>
<department id="3">
<deptCode>D3</deptCode>
<deptName>Science-Botany</deptName>
<deptHead>Susanne Charles</deptHead>
</department>
<department id="4">
<deptCode>D4</deptCode>
<deptName>Science-Chemistry</deptName>
<deptHead>Henry Carl</deptHead>
</department>
<highschool>
<schools>

从上面的 xml 中,如果城市是 Marshal,学校名称是 Marshalls,则检查两个部门名称 Chemistry 和 Science-Chemistry 是否存在, 如果为真,则删除化学部门元素。如果 Science-Chemistry 不存在,则修改 Chemistry 部门的值 DeptCode为4,Dept Name为Science-Chemistry,Department attribute id为4。

下面是我正在使用的 XSLT。如果部门名称 Science Chemistry 存在,我需要在 remDept 模板下编写代码以删除部门。 如果Science Chemistry不存在,修改模板modifyDept中的Dept Code和Dept Name为D4。有人能帮我吗?提前致谢

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />

<xsl:template match="node()!@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
<xsl:copy>
</xsl:template>

<xsl:template match="/schools[city='Marshall' and /highschools/schoolname='Marshalls']">
<xsl:if test="contains(deptName='Science-Chemistry')">
<xsl:choose>
<xsl:when test="contains(deptName='Chemistry'">
<xsl:call-template name="remDept">
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="modifyDept">
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>

<xsl:template name="remDept">
// TO DO
</xsl:template>

<xsl:template name="modifyDept">
// TO DO
</xsl:template>


</xsl:stylesheet>

最佳答案

From the above xml, if city is Marshal and school name is Marshalls, then check if both dept names Chemistry and Science-Chemistry exists, if true, remove the department element of Chemistry. If Science-Chemistry does not exist, then modify the Chemistry department values with DeptCode as 4 and Dept Name as Science-Chemistry and Department attribute id as 4.

也许我遗漏了什么,但在我看来这可以(相对)简单地通过以下方式完成:

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:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

<xsl:template match="department[../../city='Marshall' and ../schoolname='Marshalls' and deptName='Chemistry']">
<xsl:if test="not(../department[deptName='Science-Chemistry'])">
<department id="4">
<deptCode>D4</deptCode>
<deptName>Science-Chemistry</deptName>
<xsl:copy-of select="deptHead"/>
</department>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

关于xml - XSLT - 根据条件修改元素兄弟的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26578375/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com