我可以使用一个XSLT为元素内容创建一些不同的更改吗?

时间:2017-05-18 13:59:08

标签: xml xslt

我有一个XML文档,如下所示:

<Patient>
    <Treatment>
        <Medications>
            <Medication>
                <MedicationCode>X</MedicationCode>
            </Medication>
        </Medications>
        <Procedures>
            <Procedure>
                <ProcdName>A</ProcdName>
            </Procedure>
            <Procedure>
                <ProcdName>B</ProcdName>
            </Procedure>
        </Procedures>
    </Treatment>
</Patient>

转换后应该如下所示:

<Patient>
    <Procedures>
        <ProcdName>A</ProcdName>
    </Procedures>
    <Procedures>
        <ProcdName>B</ProcdName>
    </Procedures>
</Patient>

我设法做到了,但只使用了4个不同的XSL转换:第一个用于删除Medications元素,第二个用于删除处理元素但保留其内容,第三个用于删除Procedures元素,第四个用于删除将Procedure元素重命名为Procedures。当我尝试在一次转换中完成所有操作时,只有其中一些生效。是因为他们都在修改相关元素吗?有没有办法通过一次转换实现这一切?

更新 - 2017年5月25日

很抱歉,但这里真的是完整的图片。 原始XML:

<ns0:PatMedData xmlns:ns0="http://Maccabi.Hospitals.Integration">
<MessageId>AS80000000000041</MessageId>
<Status>3</Status>
<UpdateNumber>001</UpdateNumber>
<UpdateReason/>
<TargetCode>2</TargetCode>
<ServiceProviderType>1</ServiceProviderType>
<ServiceProviderCode>1761</ServiceProviderCode>
<UnitType>2</UnitType>
<UnitCode>33845</UnitCode>
<ExecuteTime>2017-01-22T14:20:00</ExecuteTime>
<ExecuteDocID>3575718</ExecuteDocID>
<ExecuteDocName>Dr</ExecuteDocName>
<ExecuteDocLic>654788</ExecuteDocLic>
<DiagnosticTime>2017-05-10T15:09:21</DiagnosticTime>
<DiagnosticanDocID/>
<DiagnosticanDocName/>
<DiagnosticanDocLic/>
<MessageSubType>26</MessageSubType>
<RelatedDocID/>
<RelatedDocName/>
<PatID>2232</PatID>
<IDPatCode>1</IDPatCode>
<PatLastName>last</PatLastName>
<PatFirstName>first</PatFirstName>
<ExaminationName></ExaminationName>
<PDFFiles>
    <FileName>as80000000000041011.pdf</FileName>
    <FileContent/>
</PDFFiles>
<Solved/>
<Pathological>3</Pathological>
<AccNum/>
<HitNum/>
<Parameters>
    <Parameter>
        <ParameterCode>31</ParameterCode>
        <HebText/>
        <EngText/>
        <Numerical>0</Numerical>
    </Parameter>
</Parameters>
<Absences>
    <Absence>
        <AbsenceStratDate/>
        <AbsenceDep/>
        <AbsenceType/>
        <AbsenceEndDate/>
    </Absence>
</Absences>
<Prognosis>
    <Sensitivities>
        <Sensitivity>
            <SensitivityDate>2017-04-23 09:42:05</SensitivityDate>
            <SensitivityCode/>
            <SensitivityType>Other</SensitivityType>
            <SensitivityName>Latex</SensitivityName>
            <SensitivityCreated>2017-04-23 09:42:05</SensitivityCreated>
        </Sensitivity>
    </Sensitivities>
    <Diags>
        <Diag>
            <DiagName>Blighted ovum</DiagName>
            <DiagCode>631.8</DiagCode>
            <DiagMod/>
            <DiagDetail/>
            <DiagDate>2017-02-20 00:00:00</DiagDate>
            <DiagStatus/>
            <DiagSide/>
            <DiagCertainty/>
            <DiagSPerformer>Dr</DiagSPerformer>
            <DiagCreated>2017-02-20 17:22:11</DiagCreated>
            <DiagPriority/>
            <DiagSolved/>
        </Diag>
    </Diags>
</Prognosis>
<Treatment>
    <Medications>
        <Medication>
            <MedicationCode>136688</MedicationCode>
            <MedicationStatus>Recommendation</MedicationStatus>
            <MedicationCaption>SIFROL tab 0.25mg</MedicationCaption>
            <MedicationForm>tab</MedicationForm>
            <MedicationIngredient>PRAMIPEXOLE</MedicationIngredient>
            <MedicationDosage>0.25 mg</MedicationDosage>
            <MedicationRate>2</MedicationRate>
            <MedicationRoute>P.O</MedicationRoute>
            <MedicationInstruction/>
            <MedicationCreated>2017-04-23 09:45:58</MedicationCreated>
            <MedicationDuration>20</MedicationDuration>
        </Medication>
    </Medications>
    <Procedures>
        <Procedure>
            <ProcdName>Blighted ovum</ProcdName>
            <ProcdCode>631.8</ProcdCode>
            <ProcdDetail/>
            <ProcdValidity>2017-02-20 17:08:36</ProcdValidity>
            <ProcdClass>SRG</ProcdClass>
            <ProcdStatus>Actual</ProcdStatus>
            <ProcdPerformer>Dr</ProcdPerformer>
            <ProcdSide/>
            <ProcdCreated>2017-02-20 17:08:36</ProcdCreated>
        </Procedure>
        <Procedure>
            <ProcdName>Blighted ovum</ProcdName>
            <ProcdCode>631.8</ProcdCode>
            <ProcdDetail/>
            <ProcdValidity>2017-02-20 17:12:19</ProcdValidity>
            <ProcdClass>SRG</ProcdClass>
            <ProcdStatus>Actual</ProcdStatus>
            <ProcdPerformer>Dr</ProcdPerformer>
            <ProcdSide/>
            <ProcdCreated>2017-02-20 17:12:19</ProcdCreated>
        </Procedure>
    </Procedures>
</Treatment>
<SeverityCode/>
<internet>1</internet>
<VisitCode>90800314</VisitCode>
<CompletePatID>000002232</CompletePatID>
<ExamCode/>
</ns0:PatMedData>

XSLT之后:

<ns0:PatMedData xmlns:ns0="http://Maccabi.Hospitals.Integration">
<MessageId>AS80000000000041</MessageId>
<Status>3</Status>
<UpdateNumber>001</UpdateNumber>
<UpdateReason/>
<TargetCode>2</TargetCode>
<ServiceProviderType>1</ServiceProviderType>
<ServiceProviderCode>1761</ServiceProviderCode>
<UnitType>2</UnitType>
<UnitCode>33845</UnitCode>
<ExecuteTime>2017-01-22T14:20:00</ExecuteTime>
<ExecuteDocID>3575718</ExecuteDocID>
<ExecuteDocName>Dr</ExecuteDocName>
<ExecuteDocLic>654788</ExecuteDocLic>
<DiagnosticTime>2017-05-10T15:09:21</DiagnosticTime>
<DiagnosticanDocID/>
<DiagnosticanDocName/>
<DiagnosticanDocLic/>
<MessageSubType>26</MessageSubType>
<RelatedDocID/>
<RelatedDocName/>
<PatID>2232</PatID>
<IDPatCode>1</IDPatCode>
<PatLastName>last</PatLastName>
<PatFirstName>first</PatFirstName>
<ExaminationName></ExaminationName>
<PDFFiles>
    <FileName>as80000000000041011.pdf</FileName>
    <FileContent/>
</PDFFiles>
<Solved/>
<Pathological>3</Pathological>
<AccNum/>
<HitNum/>
<Prognosis>
    <Diags>
        <Diag>
            <DiagName>Blighted ovum</DiagName>
            <DiagCode>631.8</DiagCode>
            <DiagMod/>
            <DiagDetail/>
        </Diag>
    </Diags>
</Prognosis>
<Procedures>
    <ProcdName>Blighted ovum</ProcdName>
    <ProcdCode>631.8</ProcdCode>
    <ProcdDetail/>
</Procedures>
<Procedures>
    <ProcdName>Blighted ovum</ProcdName>
    <ProcdCode>631.8</ProcdCode>
    <ProcdDetail/>
</Procedures>
<SeverityCode/>
<internet>1</internet>
<VisitCode>90800314</VisitCode>
<CompletePatID>000002232</CompletePatID>
<ExamCode/>
</ns0:PatMedData>

为了更清楚,这些是我正在使用的XSL,正如我所说,它们确实给了我正确的输出,但只有当它作为4个单独的转换运行时。 XSL1:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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="Absences"/>
    <xsl:template match="Sensitivities"/>
    <xsl:template match="DiagDate"/>
    <xsl:template match="DiagStatus"/>
    <xsl:template match="DiagSide"/>
    <xsl:template match="DiagCertainty"/>
    <xsl:template match="DiagSPerformer"/>
    <xsl:template match="DiagCreated"/>
    <xsl:template match="DiagPriority"/>
    <xsl:template match="DiagSolved"/>      
    <xsl:template match="Medications"/>         
    <xsl:template match="ProcdValidity"/>
    <xsl:template match="ProcdClass"/>
    <xsl:template match="ProcdStatus"/>
    <xsl:template match="ProcdPerformer"/>
    <xsl:template match="ProcdSide"/>
    <xsl:template match="ProcdCreated"/>                
    <xsl:template match="Parameters[Parameter/ParameterCode='31']"/>        
</xsl:stylesheet>

XSL2:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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 ="Treatment">
    <xsl:copy-of select="node()"/>
    </xsl:template>                 
</xsl:stylesheet>

XSL3:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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 ="Procedures">
    <xsl:copy-of select="@*|node()"/>
    </xsl:template>     
</xsl:stylesheet>

XSL4:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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="Procedure">
    <Procedures><xsl:apply-templates select="@*|node()" /></Procedures>
    </xsl:template>         
</xsl:stylesheet>

很抱歉这个故事很长,但我对XSL很新,看起来我无法解释我的问题。谢谢。

1 个答案:

答案 0 :(得分:0)

这个简单的样式表应该这样做:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <!--
    Whenever you see one of these elements, only apply the rules for all
    child elements of that element, but don't copy the element itself
    into the output.
    -->
    <xsl:template match="Treatment | Procedures">
        <xsl:apply-templates/>
    </xsl:template>

    <!--
    Use an empty template to omit an element from the output completely.
    -->
    <xsl:template match="Medications | Parameters | Absences | Sensitivities"/>

    <!--
    For every <Procedure> element, create a <Procedures> element.

    Apply the templates for the selected child elements. In this case, the
    template that matches each of <ProcdName>, <ProcdCode> and <ProcdDetail>
    is the identity template (see below), so those nodes are copied into the
    output XML as is.
    -->
    <xsl:template match="Procedure">
        <Procedures>
            <xsl:apply-templates select="ProcdName | ProcdCode | ProcdDetail"/>
        </Procedures>
    </xsl:template>

    <!--
    XSLT identity transform. The XSLT processor applies this template for
    every node in the source document that doesn't match a more specific
    template (that is, a template with a higher priority).

    See:

    https://en.wikipedia.org/wiki/Identity_transform
    http://lenzconsulting.com/how-xslt-works/#conflict_resolution_for_template_rules
    -->
    <xsl:template match="@* | node()" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>