1

我有这个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>

非常感谢您的帮助!

4

1 回答 1

3

xsl:for-each需要在xsl:template. 但是,不要使用 ,而是xsl:for-each尝试使用基于模板的方法(推送而不是拉取):

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>
于 2012-12-14T22:11:34.390 回答