使用xsl删除名称空间声明

时间:2018-10-18 06:38:33

标签: xml xslt xsd xslt-2.0

我有一个转换xml的需求,我不想在输出xml中声明名称空间。我正在使用xsl进行转换。


源xml


<?xml version="1.0" encoding="UTF-8"?>
<laml:tcga_bcr xsi:schemaLocation="http://tcga.nci/bcr/xml/clinical/laml/2.7 https://raw.githubusercontent.com/nchbcr/xsd/2.7/tcga.nci/bcr/xml/clinical/laml/2.7/TCGA_BCR.LAML_Clinical.xsd" schemaVersion="2.7" xmlns:laml="http://tcga.nci/bcr/xml/clinical/laml/2.7" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:admin="http://tcga.nci/bcr/xml/administration/2.7" xmlns:clin_shared="http://tcga.nci/bcr/xml/clinical/shared/2.7" xmlns:shared="http://tcga.nci/bcr/xml/shared/2.7" xmlns:rx="http://tcga.nci/bcr/xml/clinical/pharmaceutical/2.7" xmlns:rad="http://tcga.nci/bcr/xml/clinical/radiation/2.7">
<admin:admin>
<admin:bcr xsd_ver="1.17">Washington University</admin:bcr>
<admin:file_uuid xsd_ver="2.6">ADA599EB-1014-4A04-8422-675E668888EE</admin:file_uuid>
<admin:batch_number xsd_ver="1.17">25.17.0</admin:batch_number>
<admin:project_code xsd_ver="">TCGA</admin:project_code>
<admin:disease_code xsd_ver="2.6">LAML</admin:disease_code>
<admin:day_of_dcc_upload xsd_ver="1.17">9</admin:day_of_dcc_upload>
<admin:month_of_dcc_upload xsd_ver="1.17">12</admin:month_of_dcc_upload>
<admin:year_of_dcc_upload xsd_ver="1.17">2015</admin:year_of_dcc_upload>
<admin:patient_withdrawal>
    <admin:withdrawn>false</admin:withdrawn>
</admin:patient_withdrawal>
</admin:admin>
<laml:patient>
<admin:additional_studies/>
<clin_shared:tumor_tissue_site preferred_name="submitted_tumor_site" display_order="9999" cde="3427536" cde_ver="2.000" xsd_ver="2.6" tier="2" owner="TSS" procurement_status="Completed" restricted="false" source_system_identifier="414398">Bone Marrow</clin_shared:tumor_tissue_site>
<clin_shared:tumor_tissue_site_other preferred_name="site_of_primary_tumor_other" display_order="999" cde="2584114" cde_ver="1.000" xsd_ver="2.6" tier="2" owner="TSS" procurement_status="Not Applicable" restricted="false" source_system_identifier="414399" xsi:nil="true"/>
<laml:leukemia_specimen_cell_source_type preferred_name="cells_used_for_analysis_source" display_order="41" cde="64583" cde_ver="3.000" xsd_ver="2.4" tier="1" owner="TSS" procurement_status="Completed" restricted="false" source_system_identifier="414400">Bone marrow aspirate</laml:leukemia_specimen_cell_source_type>
<shared:other_dx preferred_name="history_other_malignancy" display_order="13" cde="3382736" cde_ver="2.000" xsd_ver="2.5" tier="1" owner="TSS" procurement_status="Completed" restricted="false" source_system_identifier="414376">No</shared:other_dx>
<shared:gender preferred_name="gender" display_order="10" cde="2200604" cde_ver="3.000" xsd_ver="1.8" tier="1" owner="TSS" procurement_status="Completed" restricted="false" source_system_identifier="414368">FEMALE</shared:gender>
<clin_shared:race_list>
    <clin_shared:race preferred_name="race" display_order="11" cde="2192199" cde_ver="1.000" xsd_ver="1.8" tier="2" owner="TSS" procurement_status="Completed" restricted="false" source_system_identifier="414374">WHITE</clin_shared:race>
</clin_shared:race_list>
<shared:bcr_patient_barcode preferred_name="" display_order="9999" cde="2673794" cde_ver="" xsd_ver="1.8" owner="TSS" procurement_status="Completed" restricted="false">TCGA-AB-2846</shared:bcr_patient_barcode>
<shared:tissue_source_site cde="" cde_ver="" xsd_ver="2.4" owner="TSS" procurement_status="Completed" restricted="false">AB</shared:tissue_source_site>
<shared:patient_id cde="" cde_ver="" xsd_ver="2.4" owner="TSS" procurement_status="Completed" restricted="false">2846</shared:patient_id>
<shared:bcr_patient_uuid preferred_name="" display_order="9999" cde="" cde_ver="" xsd_ver="2.3" owner="TSS" procurement_status="Completed" restricted="false">02634746-93b2-4cc4-9912-4a6d0bcd2e15</shared:bcr_patient_uuid>

</laml:patient>
</laml:tcga_bcr>

正在使用xsl


    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:admin="http://tcga.nci/bcr/xml/administration/2.7"
xmlns:rx="http://tcga.nci/bcr/xml/clinical/pharmaceutical/2.7"
xmlns:laml="http://tcga.nci/bcr/xml/clinical/laml/2.7"
xmlns:shared="http://tcga.nci/bcr/xml/shared/2.7"
xmlns:clin_shared="http://tcga.nci/bcr/xml/clinical/shared/2.7"
xmlns:rad="http://tcga.nci/bcr/xml/clinical/radiation/2.7">
<xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="no"/>
<!-- Copy all other nodes -->
<!--<xsl:template match="node()|@*" priority="-2">
<xsl:copy />      
</xsl:template> -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>  
<!-- suppress schemaLocation -->
<xsl:template match="*/@xsi:schemaLocation"/>
<xsl:template match="admin:admin"/>  
<xsl:template match="laml:patient"> 
<dx>
<xsl:attribute name="disease_code"><xsl:value-of select="../admin:admin/admin:disease_code"/></xsl:attribute> 
</dx>
<patient>
<xsl:attribute name="id"><xsl:value-of select="../laml:patient/shared:patient_id"/></xsl:attribute>
<xsl:attribute name="patient_bar_cd"><xsl:value-of select="../laml:patient/shared:bcr_patient_barcode"/></xsl:attribute>
<gender>
<xsl:attribute name="cde"><xsl:value-of select="../laml:patient/shared:gender/@cde"/></xsl:attribute>
<xsl:value-of select="../laml:patient/shared:gender"/>
</gender>
</patient>
<consent>
<xsl:attribute name="cde"><xsl:value-of select="../laml:patient/clin_shared:informed_consent_verified/@cde"/></xsl:attribute>
<xsl:value-of select="../laml:patient/clin_shared:informed_consent_verified"/>
</consent>
<anatomical_site>
<xsl:attribute name="cde"><xsl:value-of select="../laml:patient/clin_shared:tumor_tissue_site/@cde"/></xsl:attribute>
<xsl:value-of select="../laml:patient/clin_shared:tumor_tissue_site"/>
</anatomical_site>
<!--  Handle all coded OBS Values first -->
<!--  3121502 -->
<xsl:call-template name="obs_coded">
<xsl:with-param name="cd"
        select="../laml:patient/laml:acute_myeloid_leukemia_calgb_cytogenetics_risk_category/@cde"/>
<xsl:with-param name="value_cd"
        select="../laml:patient/laml:acute_myeloid_leukemia_calgb_cytogenetics_risk_category"/>
</xsl:call-template>
<!--  2625735  -->
<xsl:call-template name="obs_coded">
<xsl:with-param name="cd"
        select="../laml:patient/laml:atra_exposure/@cde"/>
<xsl:with-param name="value_cd"
        select="../laml:patient/laml:atra_exposure"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="obs_coded">
<xsl:param name="cd"/>
<xsl:param name="value_cd"/>
<xsl:if test="$value_cd!=''">
<observation>
<xsl:attribute name="value_type">coded</xsl:attribute>
<xsl:attribute name="cd"><xsl:value-of select="$cd"/></xsl:attribute>
<xsl:value-of select="$value_cd"/>
</observation>
</xsl:if>
</xsl:template> 
</xsl:stylesheet>

当前输出


    <?xml version="1.0" encoding="utf-8"?>
<laml:tcga_bcr xmlns:laml="http://tcga.nci/bcr/xml/clinical/laml/2.7"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:admin="http://tcga.nci/bcr/xml/administration/2.7"
xmlns:clin_shared="http://tcga.nci/bcr/xml/clinical/shared/2.7"
xmlns:shared="http://tcga.nci/bcr/xml/shared/2.7"
xmlns:rx="http://tcga.nci/bcr/xml/clinical/pharmaceutical/2.7"
xmlns:rad="http://tcga.nci/bcr/xml/clinical/radiation/2.7"
schemaVersion="2.7">

<dx disease_code="LAML"/>
<patient id="2846" patient_bar_cd="TCGA-AB-2846">
<gender cde="2200604">FEMALE</gender>
</patient>
<consent cde=""/>
<anatomical_site cde="3427536">Bone Marrow</anatomical_site>
</laml:tcga_bcr>

我想要的输出


    <?xml version="1.0" encoding="utf-8"?>
<tcga_bcr >
<dx disease_code="LAML"/>
<patient id="2846" patient_bar_cd="TCGA-AB-2846">
<gender cde="2200604">FEMALE</gender>
</patient>
<consent cde=""/>
<anatomical_site cde="3427536">Bone Marrow</anatomical_site>
</tcga_bcr>
********************************

能帮我修改当前的xsl吗?我无法找到抑制名称空间声明并删除任何前缀的方法。

1 个答案:

答案 0 :(得分:0)

您没有与根元素laml:tcga_bcr匹配的显式模板,因此身份模板当前正在与之匹配,身份模板将对其进行复制,并保留名称空间。您只需要添加一个模板来匹配它,然后创建一个新元素即可

<xsl:template match="laml:tcga_bcr">
    <tcga_bcr>
        <xsl:apply-templates 
    </tcga_bcr>
</xsl:template>

对于所有名称空间声明,您可以为此在exclude-result-prefixes元素中添加xsl:stylesheet

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:admin="http://tcga.nci/bcr/xml/administration/2.7"
   xmlns:rx="http://tcga.nci/bcr/xml/clinical/pharmaceutical/2.7"
   xmlns:laml="http://tcga.nci/bcr/xml/clinical/laml/2.7"
   xmlns:shared="http://tcga.nci/bcr/xml/shared/2.7"
   xmlns:clin_shared="http://tcga.nci/bcr/xml/clinical/shared/2.7"
   xmlns:rad="http://tcga.nci/bcr/xml/clinical/radiation/2.7"
   exclude-result-prefixes='#all'>