简体   繁体   中英

XSLT - Extract values from XML where element names are in external document

Input data

There are two XML documents: cars.xml :

<?xml version="1.0" encoding="UTF-8" ?>
<Cars>
  <Car Make="Cadillac" Country="U.S.A.">
    <Options>
      <Airbag Price="100" />
      <CruiseControl Price="200" />
      <SeatBelts Price="300" />
    </Options>
  </Car>
  <Car Make="Volvo" Country="Sweden">
    <Options>
      <Airbag Price="50" />
      <SeatBelts Price="75" />
    </Options>
  </Car>
</Cars>

and options.xml :

<?xml version="1.0" ?>
<Options>
  <Option><Name>Airbag</Name></Option>
  <Option><Name>CruiseControl</Name></Option>
  <Option><Name>FalconWings</Name></Option>
</Options>

I realize that the Options elements in cars.xml should have been defined like this:

<Option Name="Airbag" Price="100" />

But cars.xml is an input file from an external source. I do however have control over the format of the options.xml file.

Desired output

I want to create a .csv containing the cars in cars.xml and the prices for the options that are listed in options.xml .

# Make,Country,Airbag,CruiseControl,FalconWings
Cadillac,U.S.A.,100,200,N/A
Volvo,Sweden,50,N/A,N/A

Question

How do I get the stylesheet to print the options listed in options.xml with the price in cars.xml (or N/A if the car does not have the option)?

I like using xsl:value-of to output a line of a csv:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output method="text"/>

    <xsl:param name="options-url" select="'options.xml'"/>
    <xsl:variable name="options" select="doc($options-url)//Name"/>

    <xsl:template match="/">
        <xsl:value-of select="'Make', 'Country', $options" separator=","/>
        <xsl:text>&#10;</xsl:text>
        <xsl:apply-templates select="Cars/Car"/>
    </xsl:template>

    <xsl:template match="Car">
        <xsl:value-of select="@*, for $o in $options return ((current()/Options/*[name() = $o]/@Price, 'N/A')[1])" separator=","/>
        <xsl:text>&#10;</xsl:text>
    </xsl:template>

</xsl:stylesheet>

I get

Make,Country,Airbag,CruiseControl,FalconWings
Cadillac,U.S.A.,100,200,N/A
Volvo,Sweden,50,N/A,N/A

I would do something like

<xsl:variable name="options" select="doc('Options.xml')//Name" as="xs:string*"/>

<!-- header line -->
...
<xsl:value-of select="$options" separator="."/>
...

<xsl:for-each select="Cars/Car">
   <xsl:variable name="car" select="."/>
   <!-- detail line -->
   ...
   <xsl:for-each select="$options">
     <xsl:variable name="o" select="$car/Option/*[name()=current()]">
     <xsl:choose>
       <xsl:when test="exists($o)">
         <xsl:value-of select="',(' || name($o) || '=)' || $o/@Price"/>
       </xsl:when>
       <xsl:otherwise>,N/A</xsl:otherwise>
     </xsl:choose>
  </xsl:for-each>
</xsl:for-each>

Based on Michael's suggestion, I ended up with this stylesheet that produces the desired output:

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   exclude-result-prefixes="xs"
   version="2.0">

   <xsl:output method="text"/>

   <xsl:variable name="options" select="doc('options.xml')//Name" as="xs:string*"/>

   <xsl:template match="/">

      <!-- header line -->
      <xsl:text># Make,Country,</xsl:text>
      <xsl:value-of select="$options" separator=","/>
      <xsl:text>&#xa;</xsl:text> <!-- newline -->

      <xsl:for-each select="Cars/Car">
         <xsl:variable name="car" select="."/>

         <!-- detail line -->
         <xsl:value-of select="@Make"/>
         <xsl:text>,</xsl:text>
         <xsl:value-of select="@Country"/>

         <xsl:for-each select="$options" >
            <xsl:variable name="o" select="$car/Options/*[name()=current()]"/>
            <xsl:choose>
               <xsl:when test="exists($o)">
                  <xsl:text>,</xsl:text>
                  <xsl:value-of select="$o/@Price"/>
               </xsl:when>
               <xsl:otherwise>,N/A</xsl:otherwise>
            </xsl:choose>
         </xsl:for-each>

         <xsl:text>&#xa;</xsl:text> <!-- newline -->
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM