如果子项为空则删除父项

时间:2017-10-10 12:22:15

标签: xml xslt

我需要执行xslt将此xml转换为另一个WHERE:仅显示 effectiveDate 不为空且 ratingChar1 以“WL”开头  我真的不明白我在做什么,但这里是我的xslt: 我需要做xslt将这个xml转换为另一个WHERE:仅显示 effectiveDate 不为空, ratingChar1 以“WL”开头  我真的不明白我在做什么,但这里是我的xslt:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="xml" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

  <xsl:template match="effectiveDate[not('')]"/>

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

  <xsl:template match="effectiveDate[@value=''] | parent[child[@value='']]" />
  <xsl:template match="ratingDataModel[descendant::ratingChar1[(starts-with(.,'WL'))]]"/>
</xsl:transform>

这是XML:

<EagleML eaglemlVersion="2-0" actualBuild="1" eaglemlType="ReferenceTransactionMessage" xmlns="http://www.eagleinvsys.com/2011/EagleML-2-0">
<header>
    <batchIdentifier>
        <batchId>ADX_A.20121214</batchId>
        <sequenceNumber>44</sequenceNumber>
        <totalCount>44</totalCount>
    </batchIdentifier>
    <sentBy>ALLSTATE</sentBy>
    <creationTimestamp>2012-12-14T22:33:50.816-06:00</creationTimestamp>
</header>
<validation validationScheme="Allstate" xmlns="http://www.eagleinvsys.com/2011/EagleML-2-0">Allstate</validation>
<party id="Allstate" xmlns="http://www.eagleinvsys.com/2011/EagleML-2-0">
    <partyId>Allstate</partyId>
    <partyName>Allstate Investments</partyName>
</party>
<referenceTransaction>
    <header>
        <identifier id="Allstate">
            <transactionId transactionIdScheme="Allstate">Allstate</transactionId>
        </identifier>
    </header>
    <rating>
        <sourceName>ALADDIN</sourceName>
        <updateSource>ALDDNGAL</updateSource>
        <effectiveDate>2012-12-14</effectiveDate>
        <ratingDataModel>
            <alphaRating>2</alphaRating>
            <submittedBy>l5mbeatt</submittedBy>
            <submittedDate>2012-12-14T15:57:29-06:00</submittedDate>
            <ratingCode>RV_2</ratingCode>
            <ratingTypeIndicator/>
            <ratingChar1>RV_2</ratingChar1>
        </ratingDataModel>
    </rating>
</referenceTransaction>
<referenceTransaction>
    <header>
        <identifier id="Allstate">
            <transactionId transactionIdScheme="Allstate">Allstate</transactionId>
        </identifier>
    </header>
    <rating>
        <sourceName>ALADDIN</sourceName>
        <updateSource>ALDDNGAL</updateSource>
        <effectiveDate>2012-12-14</effectiveDate>
        <ratingDataModel>
            <alphaRating>45</alphaRating>
            <submittedBy>l5mbeatt</submittedBy>
            <submittedDate>2012-12-14T15:57:29-06:00</submittedDate>
            <ratingCode>TH_45</ratingCode>
            <ratingTypeIndicator/>
            <ratingChar1>TH_45</ratingChar1>
        </ratingDataModel>
    </rating>
</referenceTransaction>
<referenceTransaction>
    <header>
        <identifier id="Allstate">
            <transactionId transactionIdScheme="Allstate">Allstate</transactionId>
        </identifier>
    </header>
    <rating>
        <sourceName>ALADDIN</sourceName>
        <updateSource>ALDDNGAL</updateSource>
        <effectiveDate>2012-12-14</effectiveDate>
        <ratingDataModel>
            <alphaRating>CONCERN</alphaRating>
            <submittedBy>l5bprice</submittedBy>
            <submittedDate>2012-12-14T15:43:59-06:00</submittedDate>
            <ratingCode>WL_CONCERN</ratingCode>
            <ratingTypeIndicator>WATCHLIST</ratingTypeIndicator>
            <ratingChar1>WL_CONCERN</ratingChar1>
            <ratingDate1>2012-12-14</ratingDate1>
        </ratingDataModel>
    </rating>
</referenceTransaction>
<referenceTransaction>
    <header>
        <identifier id="Allstate">
            <transactionId transactionIdScheme="Allstate">Allstate</transactionId>
        </identifier>
    </header>
    <rating>
        <sourceName>ALADDIN</sourceName>
        <updateSource>ALDDNGAL</updateSource>
        <effectiveDate></effectiveDate>
        <ratingDataModel>
            <alphaRating>CONCERN</alphaRating>
            <submittedBy>nfallon</submittedBy>
            <submittedDate>2012-12-14T16:05:25-06:00</submittedDate>
            <ratingCode>WL_CONCERN</ratingCode>
            <ratingTypeIndicator>WATCHLIST</ratingTypeIndicator>
            <ratingChar1>WL_CONCERN</ratingChar1>
            <ratingDate1>2012-12-14</ratingDate1>
        </ratingDataModel>
    </rating>
</referenceTransaction>
</EagleML>

预期的XML:

<EagleML eaglemlVersion="2-0" actualBuild="1" eaglemlType="ReferenceTransactionMessage" xmlns="http://www.eagleinvsys.com/2011/EagleML-2-0">
<header>
    <batchIdentifier>
        <batchId>ADX_A.20121214</batchId>
        <sequenceNumber>44</sequenceNumber>
        <totalCount>44</totalCount>
    </batchIdentifier>
    <sentBy>ALLSTATE</sentBy>
    <creationTimestamp>2012-12-14T22:33:50.816-06:00</creationTimestamp>
</header>
<validation validationScheme="Allstate" xmlns="http://www.eagleinvsys.com/2011/EagleML-2-0">Allstate</validation>
<party id="Allstate" xmlns="http://www.eagleinvsys.com/2011/EagleML-2-0">
    <partyId>Allstate</partyId>
    <partyName>Allstate Investments</partyName>
</party>
<referenceTransaction>
    <header>
        <identifier id="Allstate">
            <transactionId transactionIdScheme="Allstate">Allstate</transactionId>
        </identifier>
    </header>
    <rating>
        <sourceName>ALADDIN</sourceName>
        <updateSource>ALDDNGAL</updateSource>
        <effectiveDate>2012-12-14</effectiveDate>
        <ratingDataModel>
            <alphaRating>CONCERN</alphaRating>
            <submittedBy>l5bprice</submittedBy>
            <submittedDate>2012-12-14T15:43:59-06:00</submittedDate>
            <ratingCode>WL_CONCERN</ratingCode>
            <ratingTypeIndicator>WATCHLIST</ratingTypeIndicator>
            <ratingChar1>WL_CONCERN</ratingChar1>
            <ratingDate1>2012-12-14</ratingDate1>
        </ratingDataModel>
    </rating>
</referenceTransaction>
</EagleML>

2 个答案:

答案 0 :(得分:0)

如果您要排除某些referenceTransaction元素,则确实需要一个与referenceTransaction匹配的模板,条件为[]。在条件方面,你应该颠倒逻辑。你应该否定它以给出你想要删除的条件,而不是满足你想要保留的条件。

 <xsl:template match="referenceTransaction
                      [rating/effectiveDate='' or not(starts-with(rating/ratingDataModel/ratingChar1,'WL'))]"/>

即如果referenceTransaction为空或effectiveDate不以ratingChar1开头,请删除WL

然而,你有第二个问题。在您的XML中,您有默认命名空间

xmlns="http://www.eagleinvsys.com/2011/EagleML-2-0"

但是你没有在XSLT中考虑到这一点。如果您确实在使用XSLT 2.0,则可以使用xpath-default-namespace来处理此问题。

试试这个XSLT

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xpath-default-namespace="http://www.eagleinvsys.com/2011/EagleML-2-0">
  <xsl:output method="xml" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

  <xsl:strip-space elements="*" />

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

  <xsl:template match="referenceTransaction[rating/effectiveDate='' or not(starts-with(rating/ratingDataModel/ratingChar1,'WL'))]"/>
</xsl:transform>

但是,如果您使用的是XSLT 1.0,则必须定义名称空间前缀并在模板匹配中使用

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:eml="http://www.eagleinvsys.com/2011/EagleML-2-0">
  <xsl:output method="xml" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

  <xsl:strip-space elements="*" />

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

  <xsl:template match="eml:referenceTransaction[eml:rating/eml:effectiveDate='' or not(starts-with(eml:rating/eml:ratingDataModel/eml:ratingChar1,'WL'))]"/>
</xsl:transform>

答案 1 :(得分:0)

试试这个

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:a="http://www.eagleinvsys.com/2011/EagleML-2-0"
    version="2.0">
    <xsl:output method="xml" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />


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

    <xsl:template match="a:referenceTransaction[normalize-space(descendant::a:effectiveDate)='' and starts-with(descendant::a:ratingChar1, 'WL')]"/>

</xsl:transform>