0

こんにちは、入力 xml を他の xml に変換するために xsl で if を使用しようとしています。正確な出力が得られない部分を達成できた可能性があります。助けていただければ幸いです。

入力 XML:

<?xml version="1.0" encoding="UTF-8"?>
<Music>
<Music_Model>
    <Electronic>Guitar</Electronic>
    <Beats>Pad</Beats>
    <No_of_used>
        <info>4</info>
    </No_of_used>
    <Music_info>
        <Value>1234566</Value>
        <Description>
            <Value>1</Value>
        </Description>
    </Music_info>
</Music_Model>
<Music_Model>
    <Electronic>Piano</Electronic>
    <Beats>Pad</Beats>
    <No_of_used>
        <info>2</info>
    </No_of_used>
</Music_Model>
<Music_Model>
    <Electronic>Guitar123</Electronic>
    <Beats>Flute</Beats>
    <No_of_used>
        <info>3</info>
    </No_of_used>
    <Music_info>
        <Value>128888902</Value>
        <Description>
            <Value>2</Value>
        </Description>
    </Music_info>
</Music_Model>
<Music_Model>
    <Electronic>tabala</Electronic>
    <Beats>Pad</Beats>
    <No_of_used>
        <info>40</info>
    </No_of_used>
    <Music_info>
        <Value>1298932</Value>
    </Music_info>
</Music_Model>
</Music>

注: 上記の入力 xml. 「入力 xml にMusci_info要素がなく、 Music_infoもあり、 Descriptionがない場合、出力 xml は要素No_of_usedを使用し、その値のみを表示する必要があります。Music_info要素とその値が利用可能な場合、出力は正常である必要があります。できました。除去を達成しますが、正確なものを得ることができません。

期待される出力:

<?xml version="1.0" encoding="UTF-8"?>
<Music>
<Record>
    <Electronic>Guitar</Electronic>
    <Beats>Pad</Beats>
    <music_info_value>1234566</music_info_value>
    <test_level>1</test_level>
</Record>
<Record>
    <Electronic>Guitar123</Electronic>
    <Beats>Flute</Beats>
    <music_info_value>128888902</music_info_value>
    <test_level>2</test_level>
</Record>
<not_found>
    <product>2</product>
</not_found>
</Music>

私が働いたコードで表示する出力:

<?xml version="1.0" encoding="UTF-8"?>
<Music>
<Record>
    <Electronic>Guitar</Electronic>
    <Beats>Pad</Beats>
    <music_info_value>1234566</music_info_value>
    <test_level>1</test_level>
</Record>
<Record>
    <Electronic>Guitar123</Electronic>
    <Beats>Flute</Beats>
    <music_info_value>128888902</music_info_value>
    <test_level>2</test_level>
</Record>
<Record>
    <Electronic>tabala</Electronic>
    <Beats>Pad</Beats>
    <music_info_value>1298932</music_info_value>
    <test_level/>
</Record>
<not_found>
    <product>4</product>
</not_found>
<not_found>
    <product>3</product>
</not_found>
<not_found>
    <product>40</product>
</not_found>
 </Music>

私が働いたコード:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <Music>
        <xsl:for-each select="//Music_Model">
            <xsl:if test="//Music_Model/Music_info!=Music_info ">
                <Record>
                    <Electronic>
                        <xsl:value-of select="Electronic"/>
                    </Electronic>
                    <Beats>
                        <xsl:value-of select="Beats"/>
                    </Beats>
                    <music_info_value>
                        <xsl:value-of select="Music_info/Value"/>
                    </music_info_value>
                    <test_level>
                        <xsl:value-of select="Music_info/Description/Value"/>
                    </test_level>
                </Record>
            </xsl:if>
        </xsl:for-each>
        <xsl:for-each select="//Music_Model">
            <xsl:if test="//Music_Model/Music_info!=Music_info">
                <not_found>
                    <product>
                        <xsl:value-of select="No_of_used/info"/>
                    </product>
                </not_found>
            </xsl:if>
        </xsl:for-each>
    </Music>
</xsl:template>

4

2 に答える 2

0

Music_Info/Description の存在をチェックする 1 つのループでそれを実行した場合、not found または not を出力する必要があるかどうかが通知されるため、次のようになります。

<xsl:template match="/">
    <Music>
        <xsl:for-each select="//Music_Model">
            <xsl:choose>
                <xsl:when test="Music_info/Description">
                    <Record>
                        <Electronic>
                            <xsl:value-of select="Electronic"/>
                        </Electronic>
                        <Beats>
                            <xsl:value-of select="Beats"/>
                        </Beats>
                        <music_info_value>
                            <xsl:value-of select="Music_info/Value"/>
                        </music_info_value>
                        <test_level>
                            <xsl:value-of select="Music_info/Description/Value"/>
                        </test_level>
                    </Record>
                </xsl:when>
                <xsl:otherwise>
                    <not_found>
                        <product>
                            <xsl:value-of select="No_of_used/info"/>
                        </product>
                    </not_found>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </Music>
</xsl:template>

データに適用すると、次の出力が生成されます。

<Music>
    <Record>
        <Electronic>Guitar</Electronic>
        <Beats>Pad</Beats>
        <music_info_value>1234566</music_info_value>
        <test_level>1</test_level>
    </Record>
    <not_found>
        <product>2</product>
    </not_found>
    <Record>
        <Electronic>Guitar123</Electronic>
        <Beats>Flute</Beats>
        <music_info_value>128888902</music_info_value>
        <test_level>2</test_level>
    </Record>
    <not_found>
        <product>40</product>
    </not_found>
</Music>

これはあなたが望むもののようです

于 2012-07-23T21:27:07.070 に答える
0

これは、より単純なプッシュ スタイル スタイル シートです。

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

<xsl:template match="/">
 <Music>
   <xsl:apply-templates select="*/Music_Model[    Music_info/Description ]"/>
   <xsl:apply-templates select="*/Music_Model[not(Music_info/Description)]"/>
 </Music>
</xsl:template>

<xsl:template match="Music_Model">  
 <Record> 
  <xsl:copy-of select="Electronic|Beats"/> 
  <music_info_value><xsl:value-of select="Music_info/Value" /></music_info_value>
  <test_level><xsl:value-of select="Music_info/Description/Value" /></test_level>
 </Record>  
</xsl:template>

<xsl:template match="Music_Model[not(Music_info/Description)]">  
 <not_found>
   <product><xsl:value-of select="No_of_used/info" /></product>
 </not_found>
</xsl:template>

</xsl:stylesheet>

これにより、指定された要件に従って出力が生成され、 <not_found> ノードが最後になります。

<Music>
  <Record>
    <Electronic>Guitar</Electronic>
    <Beats>Pad</Beats>
    <music_info_value>1234566</music_info_value>
    <test_level>1</test_level>
  </Record>
  <Record>
    <Electronic>Guitar123</Electronic>
    <Beats>Flute</Beats>
    <music_info_value>128888902</music_info_value>
    <test_level>2</test_level>
  </Record>
  <not_found>
    <product>2</product>
  </not_found>
  <not_found>
    <product>40</product>
  </not_found>
</Music>

ノート

tabala (製品 40) は not_found リストに含まれていることに注意してください。これは、ルールが述べているように、説明がないためです。

于 2012-07-24T01:36:06.273 に答える