2

我是 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”,而不是“1”,并忽略该 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 应该是什么样子?请您的帮助,如果不清楚,请问我。谢谢,巴克布克。

4

2 回答 2

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>
于 2012-11-20T11:57:10.620 回答
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>

说明

正确使用和覆盖标识规则——这是最基本的 XSLT 设计模式。

于 2012-11-20T14:02:30.630 回答