需要一个引用另一个xml的xslt转换

时间:2013-02-04 21:54:50

标签: xml xslt

必需的样式表应该引用refer.xml,并且必须仅从input.xml获取该元素的值。怎么做

Input.xml 这是请求

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
</cd>
<cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
</cd>
<cd>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <country>USA</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
</cd>
<cd>
    <title>Still got the blues</title>
    <artist>Gary Moore</artist>
    <country>UK</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
</cd>
<cd>
    <title>Eros</title>
    <artist>Eros Ramazzotti</artist>
    <country>EU</country>
    <company>BMG</company>
    <price>9.90</price>
    <year>1997</year>
</cd>
<cd>
    <title>One night only</title>
    <artist>Bee Gees</artist>
    <country>UK</country>
    <company>Polydor</company>
    <price>10.90</price>
    <year>1998</year>
</cd>
<cd>
    <title>Romanza</title>
    <artist>Andrea Bocelli</artist>
    <country>EU</country>
    <company>Polydor</company>
    <price>10.80</price>
    <year>1996</year>
</cd>
<cd>
    <title>When a man loves a woman</title>
    <artist>Percy Sledge</artist>
    <country>USA</country>
    <company>Atlantic</company>
    <price>8.70</price>
    <year>1987</year>
</cd>
</catalog>

refer.xml - 这里我们将从输入中获取元素

<catalog>
<cd>
    <country name="USA">
    <title>When a man loves a woman</title>
    <title>Greatest Hits</title>
    </country>
    <country name="UK">
    <title>Still got the blues</title>
    <title>One night only</title>
    </country>
</cd>
</catalog>

Output.xml - 显示在refer.xml

中显示的元素的值
<country name="USA">
<cd>
    <title>When a man loves a woman</title>
    <price>8.70</price>
</cd>
<cd>
    <title>Greatest Hits</title>
    <price>9.90</price>
</cd>
</country>
<country name="UK">
<cd>
    <title>Still got the blues</title>
    <price>10.20</price>
</cd>
<cd>
    <title>One night only</title>
    <price>10.90</price>
</cd>
</country>

由于

2 个答案:

答案 0 :(得分:0)

您可以使用XSLT中的document function从正在转换的XML文件中查找不同的XML文件中的值。

答案 1 :(得分:0)

我不确定我是否理解你的意图,但对于你的例子,我想出了以下内容: 我的想法是,进入你的refer.xml并获得你的CD标题。然后打开input.xml并在那里搜索匹配的标题。我的代码不会检查找到的CD是否来自正确的国家/地区,但这种更改很容易完成。

XSL-FILE

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <catalog>
            <xsl:for-each select="catalog/cd/country/@name">
                <country>
                    <xsl:attribute name="name">
                        <xsl:value-of select="."/>
                    </xsl:attribute>
                    <xsl:for-each select="../title">
                        <xsl:variable name="reftitle" select="."/>
                        <xsl:for-each select="document('input.xml')/catalog/cd">
                            <xsl:variable name="title" select="title"/>     
                            <xsl:variable name="price" select="price"/>


                            <xsl:if test="$reftitle=$title">
                            <cd>
                                <title>
                                    <xsl:value-of select="$reftitle"/>
                                </title>
                                <price>
                                    <xsl:value-of select="$price"/>
                                </price>
                            </cd>
                            </xsl:if>
                        </xsl:for-each>
                    </xsl:for-each>
                </country>
            </xsl:for-each>
        </catalog>
    </xsl:template>
</xsl:stylesheet>

这会创建您想要的输出。我不知道如果三个嵌套循环是nessecary,或者如果有一个更干净的版本,但它会给你一个开始。

相关问题