簡體   English   中英

xslt重新排列xml節點(刪除其中一些節點)並使用基於屬性值的名稱創建新節點

[英]xslt rearrange xml nodes (remove some of them) and create new node with name based on attribute value

我有這個xml:

<?xml version="1.0" encoding="UTF-8"?>
<General>
    <Ports>
        <Port>
            <PortID>32434</PortID>
            <PortName>PortName 1</PortName>
            <Section>
                <SectionHeader ID="63">General overview</SectionHeader>
                <PAR ID="111">bla bla bla</PAR>
            </Section>
            <Section>
                <SectionHeader ID="61">Max Size</SectionHeader>
                <PAR ID="222">blu blu blu</PAR>
            </Section>
        </Port>
        <Port>
            <PortID>777</PortID>
            <PortName>PortName 2</PortName>
            <Section>
                <SectionHeader ID="63">General overview</SectionHeader>
                <PAR ID="333">bla2 bl2a bla2</PAR>
            </Section>
            <Section>
                <SectionHeader ID="61">Max Size</SectionHeader>
                <PAR ID="444">blu2 blu2 blu2</PAR>
            </Section>
        </Port>
    </Ports>
</General>

我需要在轉換后使用這個xml:

<?xml version="1.0" encoding="UTF-8"?>
<Ports>
    <Port>
        <PortID>32434</PortID>
        <PortName>PortName 1</PortName>
        <PAR_111>bla bla bla</PAR_111>
        <PAR_222>blu blu blu</PAR_222>
    </Port>
    <Port>
        <PortID>777</PortID>
        <PortName>PortName 2</PortName>
        <PAR_333>bla2 bl2a bla2</PAR_333>
        <PAR_444>blu2 blu2 blu2</PAR_444>
    </Port>
</Ports>

基本上xsl需要涵蓋以下幾點:1。重新排列節點,只選擇PortID,PortName和PAR到轉換后的xml(省略其他節點)。 2.獲取PAR節點並創建名稱(PAR)及其屬性值(ID)組合的節點。

我正在嘗試for-each和其他一些技術,但我失敗了。 這是我當前的版本無效

<?xml version="1.0" encoding="UTF-8"?>
<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:for-each select="//Port">
        <xsl:copy-of select="*[name()='PortID' or name()='PortName']" />
        <xsl:copy-of select="Section/*[name()='PAR']">
            <xsl:element name="PAR_{@ID}">
                <xsl:value-of select="." />
            </xsl:element>
        </xsl:copy-of>
    </xsl:for-each>

</xsl:stylesheet>

非常感謝您的幫助!

您的xsl:for-each需要位於xsl:template 但是,不要使用xsl:for-each ,而是嘗試使用基於模板的方法(push而不是pull):

XML輸入

<General>
    <Ports>
        <Port>
            <PortID>32434</PortID>
            <PortName>PortName 1</PortName>
            <Section>
                <SectionHeader ID="63">General overview</SectionHeader>
                <PAR ID="111">bla bla bla</PAR>
            </Section>
            <Section>
                <SectionHeader ID="61">Max Size</SectionHeader>
                <PAR ID="222">blu blu blu</PAR>
            </Section>
        </Port>
        <Port>
            <PortID>777</PortID>
            <PortName>PortName 2</PortName>
            <Section>
                <SectionHeader ID="63">General overview</SectionHeader>
                <PAR ID="333">bla2 bl2a bla2</PAR>
            </Section>
            <Section>
                <SectionHeader ID="61">Max Size</SectionHeader>
                <PAR ID="444">blu2 blu2 blu2</PAR>
            </Section>
        </Port>
    </Ports>
</General>

XSLT 1.0

<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="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="General|Section">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="SectionHeader"/>

    <xsl:template match="PAR">
        <xsl:element name="PAR_{@ID}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>

XML輸出

<Ports>
   <Port>
      <PortID>32434</PortID>
      <PortName>PortName 1</PortName>
      <PAR_111>bla bla bla</PAR_111>
      <PAR_222>blu blu blu</PAR_222>
   </Port>
   <Port>
      <PortID>777</PortID>
      <PortName>PortName 2</PortName>
      <PAR_333>bla2 bl2a bla2</PAR_333>
      <PAR_444>blu2 blu2 blu2</PAR_444>
   </Port>
</Ports>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM