嗨,我正在尝试在 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>