1

I'm muddling my way through learning XSLT and have a challenge where I need ultimately to filter and sort over 100 products that I can only retrieve 20 at a time from a webservice. I have been able to pull those products successfully and manipulate them but only in their original batches, not as if they are one list. So I've tried to transform the separate files into one as follows.

Combining multiple files like this one:

A.xml

<?xml version="1.0" encoding="UTF-8"?>
<productSearchResponse>
    <products>
        <product>
            <code>A1</code>
            <item> SAUVIGNON RESERVA</item>
            <country>Spain</country>
            <region>Penedes</region>
            <category>Red</category>
            <style>Full-bodied</style>
        </product>
        <product>
            <code>A2</code>
            <item>RESERVE RIESLING</item>
            <country>France</country>
            <region>Alsace</region>
            <category>White</category>
            <style>Aromatic</style>
        </product>
        <product>
            <code>A3</code>
            <item>GAMAY</item>
            <country>Canada</country>
            <region>Ontario</region>
            <category>Red</category>
            <style>Medium-bodied</style>
        </product>
    </products>
</productSearchResponse>

... with this one:

B.xml

<?xml version="1.0" encoding="UTF-8"?>
<productSearchResponse>
    <products>
        <product>
            <code>B1</code>
            <item>BOURGOGNE CHARDONNAY</item>
            <country>France</country>
            <region>Burgundy</region>
            <category>White</category>
            <style>Light</style>
        </product>
        <product>
            <code>B2</code>
            <item>ONTARIO RIESLING II</item>
            <country>Canada</country>
            <region>Ontario</region>
            <category>White</category>
            <style>Off-dry</style>
        </product>
        <product>
            <code>B3</code>
            <item>WEST COAST CAB SAUV</item>
            <country>USA</country>
            <region>California</region>
            <category>Red</category>
            <style>Full-bodied</style>
        </product>
    </products>
</productSearchResponse>

...to get a new xml file that looks like this one: all.xml

<?xml version="1.0" encoding="UTF-8"?>
<productSearchResponse>
    <products>
        <product>
            <code>A1</code>
            <item> SAUVIGNON RESERVA</item>
            <country>Spain</country>
            <region>Penedes</region>
            <category>Red</category>
            <style>Full-bodied</style>
        </product>
        <product>
            <code>A2</code>
            <item>RESERVE RIESLING</item>
            <country>France</country>
            <region>Alsace</region>
            <category>White</category>
            <style>Aromatic</style>
        </product>
        <product>
            <code>A3</code>
            <item>GAMAY</item>
            <country>Canada</country>
            <region>Ontario</region>
            <category>Red</category>
            <style>Medium-bodied</style>
        </product>
        <product>
            <code>B1</code>
            <item>BOURGOGNE CHARDONNAY</item>
            <country>France</country>
            <region>Burgundy</region>
            <category>White</category>
            <style>Light</style>
        </product>
        <product>
            <code>B2</code>
            <item>ONTARIO RIESLING II</item>
            <country>Canada</country>
            <region>Ontario</region>
            <category>White</category>
            <style>Off-dry</style>
        </product>
        <product>
            <code>B3</code>
            <item>WEST COAST CAB SAUV</item>
            <country>USA</country>
            <region>California</region>
            <category>Red</category>
            <style>Full-bodied</style>
        </product>
    </products>
</productSearchResponse>

I have tried using this

ABC.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="ABC.xsl"?>
<essentials>
    <webservice filename="A.xml"/>
    <webservice filename="B.xml"/>
    <!-- etc -->
</essentials>

with this

ABC.xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0"  indent="yes"/>
    <xsl:template match="/">
        <output>
            <xsl:for-each select="/essentials/webservice">
                <xsl:for-each select="document(@filename)/productSearchResponse/products/product">
                    <product>
                        <code>
                            <xsl:value-of select="code"/>
                        </code>
                        <item>
                            <xsl:value-of select="item"/>
                        </item> 
                        <country>
                            <xsl:value-of select="country"/>
                        </country>  
                        <region>
                            <xsl:value-of select="region"/>
                        </region>   
                        <category>
                            <xsl:value-of select="category"/>
                        </category>                 
                        <style>
                            <xsl:value-of select="style"/>
                        </style>
                    </product>              
                </xsl:for-each> 
            </xsl:for-each>
        </output>
    </xsl:template>
</xsl:stylesheet>

.... but end up with this in the browser:

A1 SAUVIGNON RESERVASpainPenedesRedFull-bodiedA2RESERVE RIESLINGFranceAlsaceWhiteAromaticA3GAMAYCanadaOntarioRedMedium-bodiedB1BOURGOGNE CHARDONNAYFranceBurgundyWhiteLightB2ONTARIO RIESLING IICanadaOntarioWhiteOff-dryB3WEST COAST CAB SAUVUSACaliforniaRedFull-bodiedC1BURGUNDY PINOT NOIRFranceBurgundyRedMedium-bodiedC2CALIFORNIAN WHITEUSACaliforniaWhiteOff-dryC3CANADIAN REDCanadaOntarioRedFull-bodied

Firebug shows that behind the scenes the result is the desired xml. Where I'm getting stumped is: How can I get the result into a form that I can use, that I can treat as regular xml and do further transformations upon? And have I found the right approach or am I barking up the wrong tree, making this somehow too complicated?

4

1 回答 1

1

Well XML to XML transformation directly in the browser is usually not a good way to use that form of transformation, most browsers or at least Mozilla browsers assume your target format is something like XHTML or SVG the browser knows to render. In your case it is not, so all Firefox does is show the unstyled content of text nodes.

And of course your code can be simplified:

<xsl:template match="/">
  <productSearchResponse>
    <products>
      <xsl:copy-of select="document(essentials/webservice/@filename)//product"/>
    </products>
  </productSearchResponse>
</xsl:template>

That will however not change the rendering in Firefox/Mozilla.

于 2013-01-08T15:21:57.973 回答