我在XSLT世界中真的很新,但我有XML:
<Response>
<Campaign Position_Aut="1" Campaign_Id="A" Campaign_Status="Active" >
<Groups Name="gplust_campaign_test_group"></Groups>
<Groups Name="g1"><Position PositionId="1-6MOJ6"></Position></Groups>
</Campaign>
<Campaign Position_Aut="1" Campaign_Id="B" Campaign_Status="Active" >
<Groups Name="g2"></Groups>
</Campaign>
<Campaign Position_Aut="1" Campaign_Id="C" Campaign_Status="Completed" >
</Campaign>
</Response>
我需要更改xml,以便在Campaign有一个Position时,属性Position_Aut应为“0”,并且忽略Campaign的所有子项。 这是我需要的XML版本:
<Response>
<Campaign Position_Aut="0" Campaign_Id="A" Campaign_Status="Active" ></Campaign>
<Campaign Position_Aut="1" Campaign_Id="B" Campaign_Status="Active" ></Campaign>
<Campaign Position_Aut="1" Campaign_Id="C" Campaign_Status="Completed" ></Campaign>
</Response>
XSLT应该是什么样的? 请你帮忙,如果不清楚,请问我。 谢谢,Bakbuk。
答案 0 :(得分:0)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@Position_Aut[../Groups/Position]">
<xsl:attribute name="{name()}">
<xsl:value-of select="'0'" />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
......产出产出......
<Response>
<Campaign Position_Aut="0" Campaign_Id="A" Campaign_Status="Active">
<Groups Name="gplust_campaign_test_group" />
<Groups Name="g1">
<Position PositionId="1-6MOJ6" />
</Groups>
</Campaign>
<Campaign Position_Aut="1" Campaign_Id="B" Campaign_Status="Active">
<Groups Name="g2" />
</Campaign>
<Campaign Position_Aut="1" Campaign_Id="C" Campaign_Status="Completed" />
</Response>
要删除Campaign的子项,请包含此模板...
<xsl:template match="Campaign">
<xsl:copy>
<xsl:apply-templates select="@*"/>
</xsl:copy>
</xsl:template>
答案 1 :(得分:0)
以下是一个简单而完整的转换:
<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="Campaign[Groups/Position]">
<Campaign Position_Aut="0">
<xsl:apply-templates select="@*[not(name()='Position_Aut')]"/>
</Campaign>
</xsl:template>
<xsl:template match="Campaign/node()"/>
</xsl:stylesheet>
在提供的XML文档上应用此转换时:
<Response>
<Campaign Position_Aut="1" Campaign_Id="A" Campaign_Status="Active" >
<Groups Name="gplust_campaign_test_group"></Groups>
<Groups Name="g1"><Position PositionId="1-6MOJ6"></Position></Groups>
</Campaign>
<Campaign Position_Aut="1" Campaign_Id="B" Campaign_Status="Active" >
<Groups Name="g2"></Groups>
</Campaign>
<Campaign Position_Aut="1" Campaign_Id="C" Campaign_Status="Completed" >
</Campaign>
</Response>
产生了想要的正确结果:
<Response>
<Campaign Position_Aut="0" Campaign_Id="A" Campaign_Status="Active"/>
<Campaign Position_Aut="1" Campaign_Id="B" Campaign_Status="Active"/>
<Campaign Position_Aut="1" Campaign_Id="C" Campaign_Status="Completed"/>
</Response>
<强>解释强>:
正确使用和覆盖identity rule - 这是最基本的XSLT设计模式。