在XSL中使用If

时间:2012-07-23 18:47:18

标签: xml xslt xml-serialization xslt-1.0 xslt-2.0

我试图在xsl中使用if将输入xml转换为其他xml。 能够实现某些部分无法获得准确的输出。帮助会很高兴。

输入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 但是没有描述那么输出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>

2 个答案:

答案 0 :(得分:0)

如果您只是在一个循环中检查是否存在Music_Info / Description,它会告诉您是否应该输出未找到,所以这个:

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

这似乎是你想要的

答案 1 :(得分: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>

根据您的要求生成输出,其中&lt; not_found&gt;节点持续。

<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列表中,因为根据您的规则声明,它没有Description。