繁体   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