如何从输出中删除父节点?

时间:2014-11-20 14:29:04

标签: xml xslt

以下是基于我在下面粘贴的xslt代码的输出。 此输出重复 ext:PersonBirthDate 。我想删除第一个和最后一个分机:PersonBirthDate 我只想让3个extPersonBirthdate 而不是父。 我该怎么做? 这是输出

<ext:PersonBirthDate>
    <ext:PersonBirthDate approximateDateIndicator="true" currentIndicator="false">1956-03-04</ext:PersonBirthDate>
    <ext:PersonBirthDate ext:approximateDateIndicator="false" ext:currentIndicator="false">1956-04-21</ext:PersonBirthDate>
    <ext:PersonBirthDate ext:approximateDateIndicator="false" ext:currentIndicator="true">1956-05-21</ext:PersonBirthDate>
</ext:PersonBirthDate>

我希望我的输出看起来像这样

<ext:PersonBirthDate ext:approximateDateIndicator="true" ext:currentIndicator="false">1956-03-04</ext:PersonBirthDate>
<ext:PersonBirthDate ext:approximateDateIndicator="false" ext:currentIndicator="false">1956-04-21</ext:PersonBirthDate>
<ext:PersonBirthDate ext:approximateDateIndicator="false" ext:currentIndicator="true">1956-05-21</ext:PersonBirthDate>

我的xml输入文档包含更多代码

   <Integration xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:tsg="http://tsgweb.com" xmlns:IXML="http://tsgweb.com" xmlns:CMCodeQueryHelper="urn:CMCodeQueryHelper" PackageID="BCA PO Notification" MessageID="57810746" xmlns="">
    <ControlPoint Timestamp="11/12/2014 12:29:07 PM" UserID="KimaniW">SAVE-PROTECTION-ORDER</ControlPoint>
    <Case InternalID="1616758667" ID="11681295" xmlns:user="http://tylertechnologies.com">
        <FiledDate>10/30/2014</FiledDate>
        <CaseCategory>FAM</CaseCategory>
        <CaseType Word="DMA">Domestic Abuse</CaseType>
        <BaseCaseType>Civil Domestic Violence</BaseCaseType>
        <CaseTitle>In the Matter of Randy Carl Shogren vs Faith Marie Dieter</CaseTitle>
        <SecurityGroup Word="CONFPOR">Conf - Protective Order</SecurityGroup>
        <Active>true</Active>
        <Deleted>false</Deleted>
        <Expunged>false</Expunged>
        <CaseNumber>10-FA-14-118</CaseNumber>
        <Court>
            <NodeID>111</NodeID>
            <CourtName>Carver County</CourtName>
            <CourtNCIC>MN010015J</CourtNCIC>
        </Court>
        <Assignment Current="true">
            <Court>
                <NodeID>111</NodeID>
                <CourtName>Carver County</CourtName>
                <CourtNCIC>MN010015J</CourtNCIC>
            </Court>
            <CaseNumber>10-FA-14-118</CaseNumber>
            <AssignmentDate>10/30/2014</AssignmentDate>
            <NoticeJudicialOfficer>false</NoticeJudicialOfficer>
            <TimestampCreate>10/30/2014 13:21:50:077</TimestampCreate>
        </Assignment>
        <CaseParty ID="14474176" InternalCasePartyID="1633582774" InternalPartyID="1612366618">
            <ObservedRace Word="A">Asian</ObservedRace>
            <Connection Word="RSP" BaseConnection="DF" ID="34642711" InternalCasePartyConnectionID="1635190267">
                <Description>Respondent</Description>
            </Connection>
            <CasePartyName Current="true" ID="5194942" InternalNameID="1612772263">
                <FormattedName>Jana, Deoe Madue</FormattedName>
            </CasePartyName>
        </CaseParty>
    </Case>
    <Party ID="14474176" InternalPartyID="1612366618">
        <Gender Word="F ">Female</Gender>
        <ApproximateDOB>3/4/1956</ApproximateDOB>
        <DateOfBirth>04/21/1956</DateOfBirth>
        <DateOfBirth Current="true">05/21/1956</DateOfBirth>
        <PartyName ID="5194942" Current="true" InternalNameID="1612772263">
            <FormattedName>Jana, Deoe Madue</FormattedName>
        </PartyName>
        <PersonID>
            <PersonIDNumber ID="3554946"/>
            <PersonIDGroup>Person-ID</PersonIDGroup>
        </PersonID>
    </Party>
    <ProtectionOrder Op="E" InternalProtectionOrderID="1305" xmlns:user="http://tylertechnologies.com">
        <RespondentPartyID InternalPartyID="1612366618">1612366618</RespondentPartyID>
        <RespondentNameID InternalNameID="1612772263">1612772263</RespondentNameID>
    </ProtectionOrder>
</Integration>

这是我的xslt,它有更多代码

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://www.courts.state.dc.us/ProtectionOrderExtension/1.0" xmlns:nc="http://niem.gov/niem/niem-core/2.0" xmlns:s="http://niem.gov/niem/structures/2.0" xmlns:mscef="courts.state.dc.us/extfun" extension-element-prefixes="exsl" exclude-result-prefixes="mscef msxsl exsl"
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template name="ProtectionOrder">
    <ext:ProtectionOrder>
<!--Respondent -->
    <xsl:for-each select="RespondentPartyID">
        <xsl:for-each select="//CaseParty[(@InternalPartyID=current()/@InternalPartyID) and (Connection[(@Word='RSP') ])]">
            <xsl:for-each select="//Party[@InternalPartyID=current()/@InternalPartyID]">
                <xsl:call-template name="Respondent">
                    <xsl:with-param name="pProtectionOrderID">
                        <xsl:value-of select="$vProtectionOrderID"/>
                    </xsl:with-param>
                </xsl:call-template>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:for-each>
    </ext:ProtectionOrder>
</xsl:template>
<!--
Template for DateOfBirth for the respondent
-->
    <xsl:template match="DateOfBirth">
        <ext:PersonBirthDate>
            <xsl:attribute name="ext:approximateDateIndicator">false</xsl:attribute>
            <xsl:attribute name="ext:currentIndicator">false</xsl:attribute>
            <xsl:value-of select="mscef:formatDate(string(.))"/>
        </ext:PersonBirthDate>
    </xsl:template>

<!--Template for DateOfBirth or the respondent where Current = true -->
    <xsl:template match="DateOfBirth[@Current='true']">
        <ext:PersonBirthDate>
            <xsl:attribute name="ext:approximateDateIndicator">false</xsl:attribute>
            <xsl:attribute name="ext:currentIndicator">true</xsl:attribute>
            <xsl:value-of select="mscef:formatDate(string(.))"/>
        </ext:PersonBirthDate>
    </xsl:template>

<!--Template for ApproximateDOB -->
    <xsl:template match="ApproximateDOB">
        <ext:PersonBirthDate approximateDateIndicator="true" currentIndicator="{not(../DateOfBirth)}" >
           <xsl:value-of select="mscef:formatDate(string(.))"/>
        </ext:PersonBirthDate>
    </xsl:template>

<!--Respondent Template-->
    <xsl:template name="Respondent">
        <xsl:param name="pProtectionOrderID"/>
        <ext:Respondent>
            <nc:PersonEthnicityCode>
                <xsl:choose>
                    <xsl:when test="//CaseParty[@ID=current()/@ID]/ObservedEthnicity/@Word='NH'">N</xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="@Word"/>
                    </xsl:otherwise>
                </xsl:choose>
            </nc:PersonEthnicityCode>
            <nc:PersonEyeColorCode>
                <xsl:value-of select="EyeColor/@Word"/>
            </nc:PersonEyeColorCode>
            <nc:PersonHairColorCode>
            <xsl:for-each select="Gender">
                <nc:PersonSexCode>
                    <xsl:value-of select="@Word"/>
                </nc:PersonSexCode>
            </xsl:for-each>

<!--Guardian who is the respondent-->
            <xsl:for-each select="//CaseParty[(Connection[(@Word='GRD')])][1]">
                <xsl:for-each select="//Party[@InternalPartyID=current()/@InternalPartyID]">
                    <xsl:call-template name="Guardian"/>
                </xsl:for-each>
            </xsl:for-each>
            <ext:PersonBirthDate>
            <xsl:apply-templates select="ApproximateDOB|DateOfBirth"/>
            </ext:PersonBirthDate>
            <xsl:for-each select="PartyName[@Current='true']">
                <ext:PersonName>
                    <xsl:attribute name="ext:currentIndicator"><xsl:value-of select="@Current"/></xsl:attribute>
                    <nc:PersonFullName>
                        <xsl:value-of select="FormattedName"/>
                    </nc:PersonFullName>
                </ext:PersonName>
            </xsl:for-each>
        </ext:Respondent>
    </xsl:template>
<!--Guardian Template-->
    <xsl:template name="Guardian">
        <ext:Guardian>
            <ext:AddressReference>
                <xsl:for-each select="//Address[@PartyCurrent='true']">
                    <xsl:attribute name="ext:currentIndicator"><xsl:value-of select="@PartyCurrent"/></xsl:attribute>
                </xsl:for-each>
                <nc:LocationReference>
                    <xsl:attribute name="s:ref"><xsl:text>INT</xsl:text><xsl:value-of select="Address/@ID"/></xsl:attribute>
                </nc:LocationReference>
            </ext:AddressReference>
            <ext:PersonBirthDate>
                <xsl:choose>
                    <xsl:when test="DateOfBirth[@Current='true']">
                        <xsl:attribute name="ext:approximateDateIndicator">false</xsl:attribute>
                        <xsl:attribute name="ext:currentIndicator">true</xsl:attribute>
                        <xsl:value-of select="mscef:formatDate(string(DateOfBirth[@Current='true']))"/>
                    </xsl:when>
                    <xsl:when test="ApproximateDOB">
                        <xsl:attribute name="ext:approximateDateIndicator">true</xsl:attribute>
                        <xsl:attribute name="ext:currentIndicator">true</xsl:attribute>
                        <xsl:value-of select="mscef:formatDate(string(ApproximateDOB))"/>
                    </xsl:when>
                </xsl:choose>
            </ext:PersonBirthDate>
            <xsl:for-each select="PartyName[@Current='true']">
                <nc:PersonName>
                    <nc:PersonFullName>
                        <xsl:value-of select="FormattedName"/>
                    </nc:PersonFullName>
                </nc:PersonName>
            </xsl:for-each>
            <xsl:for-each select="Gender">
                <nc:PersonSexCode>
                    <xsl:value-of select="@Word"/>
                </nc:PersonSexCode>
            </xsl:for-each>
        </ext:Guardian>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

你发布的xslt中仍然存在语法问题,我尝试重现时得到的输出不是你的样子,不管怎么说,我认为你的问题就在那里:

<ext:PersonBirthDate>
            <xsl:apply-templates select="ApproximateDOB|DateOfBirth"/>
</ext:PersonBirthDate>

在对ApproximateDOB和DateOfBirth应用模板之前,先创建第一个<ext:PersonBirthDate>,如果你不想要这个父元素,只需挤压xslt中的指令。

我认为您也可以简化部分代码。 匹配ApproximateDOBDateOfBirthDateOfBirth[@Current='true']的模板可以写在一个独特的模板中,如下所示:

<xsl:template match="DateOfBirth | ApproximateDOB">
            <ext:PersonBirthDate>
                <xsl:attribute name="ext:approximateDateIndicator" select="name()'ApproximateDOB'"/>
                <xsl:attribute name="ext:currentIndicator" select="@Current"/>
                <xsl:value-of select="string(.)"/>
            </ext:PersonBirthDate>
</xsl:template>

请注意,我认为您希望current DateOfBirthApproximateDOB的currentIndicator的工作方式相同,而您发布的xslt情况并非如此。

希望这会有所帮助。