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 に答える