XSLT - 从元素名称位于外部文档中的 XML 中提取值



输入数据

有两个 XML 文档: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>

options.xml

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

我意识到cars.xml中的Options元素应该这样定义:

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

cars.xml是来自外部源的输入文件。 但是,我可以控制options.xml文件的格式。

期望的输出

我想创建一个包含cars.xml汽车和options.xml中列出的选项价格的.csv。

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

问题

如何让样式表打印options.xml中列出的选项,价格以cars.xml为单位(如果汽车没有选项,则为 N/A)?

我喜欢使用xsl:value-of输出一行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>

我得到

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

我会做类似的事情

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

根据Michael的建议,我最终得到了这个样式表,它产生了所需的输出:

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

最新更新