使用XSL根据另一个节点id删除xml的节点

时间:2014-09-04 12:15:58

标签: xslt xslt-1.0 xslt-2.0

我需要有关XSL的帮助,请在下面找到我的xml

<TEST xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns:i="http://www.w3.org/2001/XMLSchemainstance">
  <TEST>
    <TEST1>
      <TEST2>
        <A>
          <Rule>e1fa7f63820a406bb97f1c1b11af8d09</Rule>
          <MountPoint>0</MountPoint>
        </A>
        <A>
          <Rule>917271928cea4a75bdfa903b49ed23e5</Rule>
          <MountPoint>0</MountPoint>
        </A>
        <A>
          <Rule>6b6336722d574e8285b73192ea057b45</Rule>
          <MountPoint>0</MountPoint>
        </A>
      </TEST2>
    </TEST1>
    <CHECK>
      <CHECK1>
        <Rule>
          <Name>test1</Name>
          <ID>6b6336722d574e8285b73192ea057b45</ID>
          <Type>DicomHeaderAttribute</Type>
          <Category>SeriesDate</Category>
          <Operator>Equals</Operator>
          <Value>as</Value>
        </Rule>
        <Rule>
          <Name>sdsd</Name>
          <ID>e1fa7f63820a406bb97f1c1b11af8d09</ID>
          <Type>DicomHeaderAttribute</Type>
          <Category>SeriesInformation</Category>
          <Operator>Equals</Operator>
          <Value>sdsdsd</Value>
        </Rule>
        <Rule>
          <Name>fdfdf</Name>
          <ID>917271928cea4a75bdfa903b49ed23e5</ID>
          <Type>DicomHeaderAttribute</Type>
          <Category>ReferringPhysician</Category>
          <Operator>Equals</Operator>
          <Value>assd</Value>
        </Rule>
      </CHECK1>
    </CHECK>
  </TEST>
</TEST>

在上面的xml中,我需要删除与Category值匹配的规则'SeriesInformation',以及与'SeriesInformation'规则的ID匹配的相应“A”节点,

预期的XML:

<TEST xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns:i="http://www.w3.org/2001/XMLSchemainstance">
  <TEST>
    <TEST1>
      <TEST2>
        <A>
          <Rule>917271928cea4a75bdfa903b49ed23e5</Rule>
          <MountPoint>0</MountPoint>
        </A>
        <A>
          <Rule>6b6336722d574e8285b73192ea057b45</Rule>
          <MountPoint>0</MountPoint>
        </A>
      </TEST2>
    </TEST1>
    <CHECK>
      <CHECK1>
        <Rule>
          <Name>test1</Name>
          <ID>6b6336722d574e8285b73192ea057b45</ID>
          <Type>DicomHeaderAttribute</Type>
          <Category>SeriesDate</Category>
          <Operator>Equals</Operator>
          <Value>as</Value>
        </Rule>
        <Rule>
          <Name>fdfdf</Name>
          <ID>917271928cea4a75bdfa903b49ed23e5</ID>
          <Type>DicomHeaderAttribute</Type>
          <Category>ReferringPhysician</Category>
          <Operator>Equals</Operator>
          <Value>assd</Value>
        </Rule>
      </CHECK1>
    </CHECK>
  </TEST>
</TEST>

请在XSL中提供帮助。

嗨,这是使用

的xsl iam
 <?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <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="Rule[Category = 'SeriesInformation']">
  </xsl:template>
</xsl:stylesheet>

我之后无法转发,我可以删除与类别匹配的规则作为SeriesInformation,但之后如何删除  A节点取决于ID iam无法做到

1 个答案:

答案 0 :(得分:2)

要删除A元素,您可以定义一个键,以Rule

查找ID元素
<xsl:key name="rule" match="Rule[ID]" use="ID" />

然后,忽略A元素的模板匹配如下:

<xsl:template match="A[key('rule', Rule)/Category='SeriesInformation']" />

试试这个XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />
    <xsl:key name="rule" match="Rule[ID]" use="ID" />

    <xsl:template match="Rule[Category='SeriesInformation']" />

    <xsl:template match="A[key('rule', Rule)/Category='SeriesInformation']" />

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