删除属性,但使用XSLT将其元素名称更改为该属性的值

时间:2017-07-10 01:29:48

标签: xml xslt

我需要使用XSLT将XML文档更改为其他格式。我已经尝试了一些我遇到过的例子,但到目前为止我还没有成功,因为我不是XSLT的大师。我的来源始终包含 Value 元素和名为 AttributeID 的属性,需要替换 即可。

我的来源是:

<?xml version="1.0" encoding="UTF-8"?>
<STEP-ProductInformation ContextID="Context1" WorkspaceID="Main">
   <Entities>
      <Entity UserTypeID="Target" ParentID="Target_Category">
         <Name>p53 (Thr155) phosphate</Name>
         <Values>
           <Value AttributeID="XXXXXXX">T15010</Value>
           <Value AttributeID="YYYYYYYYYYY">ENSG00000141510</Value>
           <Value AttributeID="ZZZZZZZZZZZZ" ID="NA">NA</Value>
...

期望的输出:

<?xml version="1.0" encoding="UTF-8"?>
<STEP-ProductInformation ContextID="Context1" WorkspaceID="Main">
   <Entities>
      <Entity UserTypeID="Target" ParentID="Target_Category">
         <Name>p53 (Thr155) phosphate</Name>
         <Values>
           <XXXXXXX>T15010</XXXXXXX>
           <YYYYYYYYYYY>ENSG00000141510</YYYYYYYYYYY>
           <ZZZZZZZZZZZZ ID="NA">NA</ZZZZZZZZZZZZ>
...

我的上一个XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"   
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="Value">
      <xsl:element name="{@AttributeID}">
          <xsl:copy-of select="*"/>
     </xsl:element>
  </xsl:template>
</xsl:stylesheet>

哪不能产生我想要的东西:

<?xml version="1.0" encoding="UTF-8"?>
<STEP-ProductInformation ContextID="Context1" WorkspaceID="Main">
   <Entities>
      <Entity UserTypeID="Target" ParentID="Target_Category">
         <Name>p53 (Thr155) phosphate</Name>
         <Values>
            <Value AttributeID="XXXXXXX">T15010</Value>
            <Value AttributeID="YYYYYYYYYYY">ENSG00000141510</Value>
            <Value AttributeID="ZZZZZZZZZZZZ" ID="NA">NA</Value>
...

我显然遗漏了一些东西。

整个来源示例:

<STEP-ProductInformation ContextID="Context1" WorkspaceID="Main">
 <Entities>
  <Entity UserTypeID="Target" ParentID="Target_Category">
     <Name>p53 (Thr155) phosphate</Name>
     <Values>
        <Value AttributeID="TARGETNO">T15010</Value>
        <Value AttributeID="ACCESSION_ENSGENE">ENSG00000141510</Value>
        <Value AttributeID="WHOLEPROTEINFLAG_YNNA" ID="NA">NA</Value>
        <Value AttributeID="ALTNAMES">Antigen NY-CO-13,Cellular tumor antigen p53,FLJ92943,LFS1,P53,Phosphoprotein p53,TP53,TRP53,Tumor suppressor p53,p53 antigen,p53 transformation suppressor,p53 tumor suppressor,transformation-related protein 53,tumor protein p53</Value>
        <Value AttributeID="PNAME">p53</Value>
        <Value AttributeID="TARGETNAME">p53 (Thr155) phosphate</Value>
        <Value AttributeID="MW">43653.17774</Value>
        <Value AttributeID="ASSENSIONID">P04637</Value>
        <MultiValue AttributeID="TARGET_KEYWORDS">
           <Value>pp53</Value>
           <Value>p-p53</Value>
        </MultiValue>
        <Value AttributeID="SORTTERM">P53</Value>
        <Value AttributeID="ACCESSION_GP">NP_000537</Value>
        <Value AttributeID="GNAMES">TP53</Value>
        <Value AttributeID="PROTEIN_GROUP_NAME">p53</Value>
        <Value AttributeID="UNIPROT">P04637</Value>
        <Value AttributeID="PROTEIN_GROUP_DESCR">a transcription factor and major tumor suppressor.</Value>
        <Value AttributeID="ACCESSION_GENE">7157</Value>
        <Value AttributeID="ORGANISM_COMMON_NAME">human</Value>
        <MultiValue AttributeID="DEVSECTIONS">
           <Value>Cell Biology</Value>
        </MultiValue>
        <Value AttributeID="TARGETTYPE">Protein</Value>
        <Value AttributeID="TSCOMPOSITESCORE">359.25922</Value>
        <MultiValue AttributeID="DEVSUBSECTIONS">
           <Value>DNA Damage</Value>
        </MultiValue>
        <MultiValue AttributeID="TARGET_DISEASES">
           <Value>Cancer &gt; Basal cell carcinoma</Value>
           <Value>Cancer &gt; Bladder cancer</Value>
           <Value>Cancer &gt; Chronic myeloid leukemia</Value>
           <Value>Cancer &gt; Colorectal cancer</Value>
           <Value>Cancer &gt; Endometrial cancer</Value>
           <Value>Cancer &gt; Glioma</Value>
           <Value>Cancer &gt; Melanoma</Value>
           <Value>Cancer &gt; Non-small cell lung cancer</Value>
           <Value>Cancer &gt; Pancreatic cancer</Value>
           <Value>Cancer &gt; Pathways in cancer</Value>
           <Value>Cancer &gt; Prostate cancer</Value>
           <Value>Cancer &gt; Small cell lung cancer</Value>
           <Value>Cancer &gt; Thyroid cancer</Value>
           <Value>Neurodegenerative Diseases &gt; Amyotrophic lateral sclerosis (ALS)</Value>
           <Value>Neurodegenerative Diseases &gt; Huntington&apos;s disease</Value>
        </MultiValue>
        <MultiValue AttributeID="TARGET_PATHWAYS">
           <Value>Crosstalk between PTMs</Value>
           <Value>Warburg Effect</Value>
           <Value>SAPK/JNK Signaling Cascades</Value>
           <Value>Protein Acetylation</Value>
           <Value>Regulation of P38 MAPKs</Value>
           <Value>ErbB/HER Signaling</Value>
           <Value>G2/M DNA Damage Checkpoint</Value>
           <Value>G1/S Checkpoint</Value>
           <Value>Apoptosis Regulation</Value>
           <Value>Mitochondrial Control of Apoptosis</Value>
           <Value>AMPK Signaling</Value>
           <Value>PI3K/Akt Signaling</Value>
        </MultiValue>
        <MultiValue AttributeID="TARGET_RESEARCHAREAS">
           <Value>Neurobiology &gt; Overview</Value>
           <Value>Immunology &gt; Overview</Value>
           <Value>Development &gt; Wnt Signaling</Value>
           <Value>Neurobiology &gt; Neurodegenerative Disorders</Value>
           <Value>Growth and Viability &gt; MAPK Signaling</Value>
           <Value>Growth and Viability &gt; DNA Damage and Repair</Value>
           <Value>Growth and Viability &gt; Apoptosis/Necrosis/Autophagy</Value>
           <Value>Growth and Viability &gt; Cell Cycle and Checkpoint Control</Value>
        </MultiValue>
        <Value AttributeID="ACCESSION_ENSP">ENSP00000269305</Value>
        <Value AttributeID="MODIFICATION">phosphate</Value>
        <Value AttributeID="PRIMARYDEVSECTION">Cell Biology</Value>
        <Value AttributeID="PRIMARYDEVSUBSECTION">DNA Damage</Value>
        <Value AttributeID="PROTORG_SEQUENCE">MEEPQSDPSVEPPLSQETFSDLWKLLPENNVLSPLPSQAMDDLMLSPDDIEQWFTEDPGPDEAPRMPEAAPPVAPAPAAPTPAAPAPAPSWPLSSSVPSQKTYQGSYGFRLGFLHSGTAKSVTCTYSPALNKMFCQLAKTCPVQLWVDSTPPPGTRVRAMAIYKQSQHMTEVVRRCPHHERCSDSDGLAPPQHLIRVEGNLRVEYLDDRNTFRHSVVVPYEPPEVGSDCTTIHYNYMCNSSCMGGMNRRPILTIITLEDSSGNLLGRNSFEVRVCACPGRDRRTEEENLRKKGEPHHELPPGSTKRALPNNTSSSPQPKKKPLDGEYFTLQIRGRERFEMFRELNEALELKDAQAGKEPGGSRAHSSHLKSKKGQSTSRHKKLMFKTEGPDSD</Value>
        <Value AttributeID="SITE">Thr155</Value>
        <MultiValue AttributeID="PROTEIN_KEYWORDS">
           <Value>TP53</Value>
        </MultiValue>
     </Values>
  </Entity>
 </Entities>
</STEP-ProductInformation>

预期结果:

<STEP-ProductInformation ContextID="Context1" WorkspaceID="Main">
 <Entities>
  <Entity UserTypeID="Target" ParentID="Target_Category">
     <Name>p53 (Thr155) phosphate</Name>
     <Values>
        <TARGETNO>T15010</TARGETNO>
        <ACCESSION_ENSGENE>ENSG00000141510</ACCESSION_ENSGENE>
        <WHOLEPROTEINFLAG_YNNA ID="NA">NA</WHOLEPROTEINFLAG_YNNA>
        <ALTNAMES>Antigen NY-CO-13,Cellular tumor antigen p53,FLJ92943,LFS1,P53,Phosphoprotein p53,TP53,TRP53,Tumor suppressor p53,p53 antigen,p53 transformation suppressor,p53 tumor suppressor,transformation-related protein 53,tumor protein p53</ALTNAMES>
        <PNAME>p53</PNAME>
        <TARGETNAME>p53 (Thr155) phosphate</TARGETNAME>
        <MW>43653.17774</MW>
        <ASSENSIONID>P04637</ASSENSIONID>
        <TARGET_KEYWORDS>
           <Value>pp53</Value>
           <Value>p-p53</Value>
        </TARGET_KEYWORDS>
        <SORTTERM>P53</SORTTERM>
        <ACCESSION_GP>NP_000537</ACCESSION_GP>
        <GNAMES>TP53</GNAMES>
        <PROTEIN_GROUP_NAME>p53</PROTEIN_GROUP_NAME>
        <UNIPROT>P04637</UNIPROT>
        <PROTEIN_GROUP_DESCR>a transcription factor and major tumor suppressor.</PROTEIN_GROUP_DESCR>
        <ACCESSION_GENE>7157</ACCESSION_GENE>
        <ORGANISM_COMMON_NAME>human</ORGANISM_COMMON_NAME>
        <DEVSECTIONS>
           <Value>Cell Biology</Value>
        </DEVSECTIONS>
        <TARGETTYPE>Protein</TARGETTYPE>
        <TSCOMPOSITESCORE>359.25922</TSCOMPOSITESCORE>
        <DEVSUBSECTIONS>
           <Value>DNA Damage</Value>
        </DEVSUBSECTIONS>
        <TARGET_DISEASES>
           <Value>Cancer &gt; Basal cell carcinoma</Value>
           <Value>Cancer &gt; Bladder cancer</Value>
           <Value>Cancer &gt; Chronic myeloid leukemia</Value>
           <Value>Cancer &gt; Colorectal cancer</Value>
           <Value>Cancer &gt; Endometrial cancer</Value>
           <Value>Cancer &gt; Glioma</Value>
           <Value>Cancer &gt; Melanoma</Value>
           <Value>Cancer &gt; Non-small cell lung cancer</Value>
           <Value>Cancer &gt; Pancreatic cancer</Value>
           <Value>Cancer &gt; Pathways in cancer</Value>
           <Value>Cancer &gt; Prostate cancer</Value>
           <Value>Cancer &gt; Small cell lung cancer</Value>
           <Value>Cancer &gt; Thyroid cancer</Value>
           <Value>Neurodegenerative Diseases &gt; Amyotrophic lateral sclerosis (ALS)</Value>
           <Value>Neurodegenerative Diseases &gt; Huntington&apos;s disease</Value>
        </TARGET_DISEASES>
        <TARGET_PATHWAYS>
           <Value>Crosstalk between PTMs</Value>
           <Value>Warburg Effect</Value>
           <Value>SAPK/JNK Signaling Cascades</Value>
           <Value>Protein Acetylation</Value>
           <Value>Regulation of P38 MAPKs</Value>
           <Value>ErbB/HER Signaling</Value>
           <Value>G2/M DNA Damage Checkpoint</Value>
           <Value>G1/S Checkpoint</Value>
           <Value>Apoptosis Regulation</Value>
           <Value>Mitochondrial Control of Apoptosis</Value>
           <Value>AMPK Signaling</Value>
           <Value>PI3K/Akt Signaling</Value>
        </TARGET_PATHWAYS>
        <TARGET_RESEARCHAREAS>
           <Value>Neurobiology &gt; Overview</Value>
           <Value>Immunology &gt; Overview</Value>
           <Value>Development &gt; Wnt Signaling</Value>
           <Value>Neurobiology &gt; Neurodegenerative Disorders</Value>
           <Value>Growth and Viability &gt; MAPK Signaling</Value>
           <Value>Growth and Viability &gt; DNA Damage and Repair</Value>
           <Value>Growth and Viability &gt; Apoptosis/Necrosis/Autophagy</Value>
           <Value>Growth and Viability &gt; Cell Cycle and Checkpoint Control</Value>
        </TARGET_RESEARCHAREAS>
        <ACCESSION_ENSP>ENSP00000269305</ACCESSION_ENSP>
        <MODIFICATION>phosphate</MODIFICATION>
        <PRIMARYDEVSECTION>Cell Biology</PRIMARYDEVSECTION>
        <PRIMARYDEVSUBSECTION>DNA Damage</PRIMARYDEVSUBSECTION>
        <PROTORG_SEQUENCE>MEEPQSDPSVEPPLSQETFSDLWKLLPENNVLSPLPSQAMDDLMLSPDDIEQWFTEDPGPDEAPRMPEAAPPVAPAPAAPTPAAPAPAPSWPLSSSVPSQKTYQGSYGFRLGFLHSGTAKSVTCTYSPALNKMFCQLAKTCPVQLWVDSTPPPGTRVRAMAIYKQSQHMTEVVRRCPHHERCSDSDGLAPPQHLIRVEGNLRVEYLDDRNTFRHSVVVPYEPPEVGSDCTTIHYNYMCNSSCMGGMNRRPILTIITLEDSSGNLLGRNSFEVRVCACPGRDRRTEEENLRKKGEPHHELPPGSTKRALPNNTSSSPQPKKKPLDGEYFTLQIRGRERFEMFRELNEALELKDAQAGKEPGGSRAHSSHLKSKKGQSTSRHKKLMFKTEGPDSD</PROTORG_SEQUENCE>
        <SITE>Thr155</SITE>
        <PROTEIN_KEYWORDS>
           <Value>TP53</Value>
        </PROTEIN_KEYWORDS>
     </Values>
  </Entity>
 </Entities>
</STEP-ProductInformation>

2 个答案:

答案 0 :(得分:0)

只需添加一个模板即可从根元素<Values>遍历树。同样使用text(),因为您在当前节点中,而*查找当前节点的子节点,因此不返回任何内容:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/Values">
      <xsl:copy>
          <xsl:apply-templates select="Value"/>
     </xsl:copy>
  </xsl:template>

  <xsl:template match="Value">
      <xsl:element name="{@AttributeID}">
          <xsl:copy-of select="@*[name()!='AttributeID']"/>
          <xsl:copy-of select="text()"/>
     </xsl:element>
  </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:0)

这是获得预期结果的一种方法:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/Values">
    <xsl:copy>
        <xsl:for-each select="Value">
            <xsl:element name="{@AttributeID}">
                <xsl:copy-of select="@*[not(name()='AttributeID')]"/>
                <xsl:value-of select="."/>
            </xsl:element>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

这是另一个:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="Value">
    <xsl:element name="{@AttributeID}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

<xsl:template match="@AttributeID"/>

</xsl:stylesheet>

请注意,两者都假设AttributeID包含有效的XML元素名称。

为响应变更的要求而添加:

我猜你想做点什么:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

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

<xsl:template match="Value[@AttributeID] | MultiValue[@AttributeID]">
    <xsl:element name="{@AttributeID}">
        <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

<xsl:template match="@AttributeID"/>

</xsl:stylesheet>