0

Required Stylesheet should refer the refer.xml and must get only the values of that elements from the input.xml. How to do that

Input.xml which is the request

<?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 - here we will have our elements from the input

<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 - This shows the values of elements which are shown in 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>

Thanks

4

2 回答 2

0

我不确定我是否正确理解了您的意图,但是对于您的示例,我提出了以下建议:想法是,进入您的 refer.xml 并获取您的 cd 标题。然后打开 input.xml 并在其中搜索匹配的标题。我的代码不会检查找到的 CD 是否来自正确的国家/地区,但此更改很容易完成。

XSL 文件

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

这将创建您想要的输出。我不知道这三个嵌套循环是否需要,或者是否有更清洁的版本可以做到这一点,但它会给你一个开始。

于 2015-02-11T11:00:57.833 回答
0

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

于 2013-02-04T22:38:48.413 回答