也有人问过类似的问题,我读过这些问题,并试图通过教程和其他方法来解决,但都没能解决。我确信这是写正确的xpath的问题,但我似乎无法解决。我正在尝试获取一个文件列表(基本上是文件夹中的所有文件),并将它们组合成不同的模式格式。诀窍在于,来自各个文件的部分信息需要用作生成的XML中的查找表。我的解决方案需要纯XSLT1.0。不用说,下面的一切都是虚构的。。。除了"清单"xml文件的结构,它看起来如下:
<files>
<file>request1.xml</xml>
<file>request2.xml</xml>
<file>request3.xml</xml>
<files>
Request1.xml文件可能如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<ProductList xmlns:pl="http://products.produsor.com/pml" xmlns:pi="http://standards.product.produsor.com/pml" createDateTime="2014-05-06T18:13:51.0Z" version="5.0">
<pl:Request requestId="ADF87A9DF7" quantity="1">
<pl:SystemIdentifier name="GUID">38DDF5C1-A049-44DB-9EEA-3F5CB831228D</pl:SystemIdentifier>
<pl:SystemIdentifier name="UPC">4236483268</pl:SystemIdentifier>
<pl:Product>
<pl:Names>
<pi.ProductNameLongDescription>Classic Design Round Dinning Table</pi.ProductNameLongDescription>
<pi.ProductNameShort>Dinning Table</pi.ProductNameShort>
</pl:Names>
<pl:Description>
<pi.ProductLongDescription>This is a really awesome table.</pi.ProductLongDescription>
<pi.ProductShortDescription>It's made of wood</pi.ProductShortDescription>
</pl:Description>
<pl:Category>
<pl:Name>Table</pl:Name>
<pl:Description>This category is for tables</pl:Description>
<pl:Priority>1</pl:Priority>
</pl:Category>
<pl:Category>
<pl:Name>Dinning Furniture</pl:Name>
<pl:Description>This category is for Dinning Furniture</pl:Description>
<pl:Priority>2</pl:Priority>
</pl:Category>
<pl:Category>
<pl:Name>Wood Furniture</pl:Name>
<pl:Description>This category is for Wood Furniture</pl:Description>
<pl:Priority>3</pl:Priority>
</pl:Category>
</pl:Product>
</pl:Request>
<pl:Request requestId="DA7FDAFDA9" quanitity="1">
<pl:SystemIdentifier name="GUID">DA7FDAFD-B049-45DB-9FFA-3F5CB834328D</pl:SystemIdentifier>
<pl:SystemIdentifier name="UPC">4236483269</pl:SystemIdentifier>
<pl:Product>
<pl:Names>
<pi.ProductNameLongDescription>Classic Design Round Coffee Table</pi.ProductNameLongDescription>
<pi.ProductNameShort>Coffee Table</pi.ProductNameShort>
</pl:Names>
<pl:Description>
<pi.ProductLongDescription>This is a really awesome table.</pi.ProductLongDescription>
<pi.ProductShortDescription>It is made of wood</pi.ProductShortDescription>
</pl:Description>
<pl:Category>
<pl:Name>Table</pl:Name>
<pl:Description>This category is for tables</pl:Description>
<pl:Priority>1</pl:Priority>
</pl:Category>
<pl:Category>
<pl:Name>Living Room Furniture</pl:Name>
<pl:Description>This category is for Dinning Furniture</pl:Description>
<pl:Priority>4</pl:Priority>
</pl:Category>
<pl:Category>
<pl:Name>Wood Furniture</pl:Name>
<pl:Description>This category is for Wood Furniture</pl:Description>
<pl:Priority>3</pl:Priority>
</pl:Category>
</pl:Product>
</pl:Request>
</ProductList>
Request2.xml应该是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<ProductList xmlns:pl="http://products.produsor.com/pml" xmlns:pi="http://standards.product.produsor.com/pml" createDateTime="2014-05-06T18:13:51.0Z" version="5.0">
<pl:Request requestId="DFADF08D0A" quantity="10">
<pl:SystemIdentifier name="GUID">38DDF5C1-A049-44DB-9EEA-3F5CB831228D</pl:SystemIdentifier>
<pl:SystemIdentifier name="UPC">4236483268</pl:SystemIdentifier>
<pl:Product>
<pl:Names>
<pi.ProductNameLongDescription>Classic Design Round Dinning Table</pi.ProductNameLongDescription>
<pi.ProductNameShort>Dinning Table</pi.ProductNameShort>
</pl:Names>
<pl:Description>
<pi.ProductLongDescription>This is a really awesome table.</pi.ProductLongDescription>
<pi.ProductShortDescription>It's made of wood</pi.ProductShortDescription>
</pl:Description>
<pl:Category>
<pl:Name>Table</pl:Name>
<pl:Description>This category is for tables</pl:Description>
<pl:Priority>1</pl:Priority>
</pl:Category>
<pl:Category>
<pl:Name>Dinning Furniture</pl:Name>
<pl:Description>This category is for Dinning Furniture</pl:Description>
<pl:Priority>2</pl:Priority>
</pl:Category>
<pl:Category>
<pl:Name>Wood Furniture</pl:Name>
<pl:Description>This category is for Wood Furniture</pl:Description>
<pl:Priority>3</pl:Priority>
</pl:Category>
</pl:Product>
</pl:Request>
<pl:Request requestId="RER7689EQ9" quanitity="10">
<pl:SystemIdentifier name="GUID">DA7FDAFD-B049-45DB-9FFA-3F5CB834328D</pl:SystemIdentifier>
<pl:SystemIdentifier name="UPC">4236483269</pl:SystemIdentifier>
<pl:Product>
<pl:Names>
<pi.ProductNameLongDescription>Classic Design Round Coffee Table</pi.ProductNameLongDescription>
<pi.ProductNameShort>Coffee Table</pi.ProductNameShort>
</pl:Names>
<pl:Description>
<pi.ProductLongDescription>This is a really awesome table.</pi.ProductLongDescription>
<pi.ProductShortDescription>It is made of wood</pi.ProductShortDescription>
</pl:Description>
<pl:Category>
<pl:Name>Table</pl:Name>
<pl:Description>This category is for tables</pl:Description>
<pl:Priority>1</pl:Priority>
</pl:Category>
<pl:Category>
<pl:Name>Living Room Furniture</pl:Name>
<pl:Description>This category is for Dinning Furniture</pl:Description>
<pl:Priority>4</pl:Priority>
</pl:Category>
<pl:Category>
<pl:Name>Wood Furniture</pl:Name>
<pl:Description>This category is for Wood Furniture</pl:Description>
<pl:Priority>3</pl:Priority>
</pl:Category>
</pl:Product>
</pl:Request>
</ProductList>
我想要的是:
<ProductList xmlns:pl="http://products.produsor.com/pml">
<pl:Submission>
<!--********* This is the problem area *************-->
<pl:Descriptions>
<pl:Description id="1">This is a really awesome table.</pl:Description>
</pl:Descriptions>
<pl:Categories>
<pl:Category id="1">Table</pl:Category>
<pl:Category id="2">Dinning Furniture</pl:Category>
<pl:Category id="3">Living Room Furniture</pl:Category>
<pl:Category id="4">Wood Furniture</pl:Category>
</pl:Categories>
<!--****************************************************-->
<pl:Product>
<pl:SystemIdentifier type="GUID">DA7FDAFD-B049-45DB-9FFA-3F5CB834328D</pl:SystemIdentifier>
<pl:SystemIdentifier name="UPC">4236483268</pl:SystemIdentifier>
<pl:ProductName descriptionId="1">Dinning Table</pl:ProductName>
<cat catId="1"/>
<cat catId="2"/>
<cat catId="3"/>
</pl:Product>
<pl:Product>
<pl:SystemIdentifier type="GUID">DA7FDAFD-B049-45DB-9FFA-3F5CB834328D</pl:SystemIdentifier>
<pl:SystemIdentifier name="UPC">4236483268</pl:SystemIdentifier>
<pl:ProductName descriptionId="1">Dinning Table</pl:ProductName>
<cat catId="1"/>
<cat catId="3"/>
<cat catId="4"/>
</pl:Product>
<pl:Product>
<pl:SystemIdentifier type="GUID">DA7FDAFD-B049-45DB-9FFA-3F5CB834328D</pl:SystemIdentifier>
<pl:SystemIdentifier name="UPC">4236483268</pl:SystemIdentifier>
<pl:ProductName descriptionId="1">Dinning Table</pl:ProductName>
<cat catId="1"/>
<cat catId="2"/>
<cat catId="3"/>
</pl:Product>
<pl:Product>
<pl:SystemIdentifier type="GUID">DA7FDAFD-B049-45DB-9FFA-3F5CB834328D</pl:SystemIdentifier>
<pl:SystemIdentifier name="UPC">4236483268</pl:SystemIdentifier>
<pl:ProductName descriptionId="1">Dinning Table</pl:ProductName>
<cat catId="1"/>
<cat catId="3"/>
<cat catId="4"/>
</pl:Product>
</pl:Submission>
</ProductList>
诀窍是我不能在pl:Description
或pl:category
标签中有重复值。如果产品元素在文件中重复,则要求它们重复。我构建了xslt模板来构建所有内容,包括描述和类别,但它对每个文件都这样做。我需要它构建描述和类别,包括来自所有文件的不同数据,然后是所有产品元素。以下是我目前所掌握的内容,它构建了产品元素。
<xsl:template match="/">
<xsl:for-each select="/files/file">
<xsl:apply-templates select="document(.)/ProductList/pl:Request"/>
</xsl:for-each>
</xsl:template>
由于这已经很长了,我只想说请求模板用于创建产品元素,我有一个"ProductList"模板,它将创建描述和类别元素结构。
这里有一个例子,它将所有类别复制到一个结果树片段中,使用exsl:node-set
和Muenchian分组来识别唯一的类别,然后在复制请求元素时引用它们:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:pl="http://products.produsor.com/pml"
version="1.0"
exclude-result-prefixes="exsl">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="input-docs" select="document(files/file)"/>
<xsl:variable name="cats-rtf">
<xsl:copy-of select="$input-docs//pl:Category"/>
</xsl:variable>
<xsl:key name="group" match="pl:Category" use="pl:Name"/>
<xsl:variable name="distinct-cats-rtf">
<xsl:for-each select="exsl:node-set($cats-rtf)/pl:Category[generate-id() = generate-id(key('group', pl:Name)[1])]">
<pl:Category id="{position()}">
<xsl:value-of select="pl:Name"/>
</pl:Category>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="distinct-cats" select="exsl:node-set($distinct-cats-rtf)/pl:Category"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<ProductList>
<pl:Submission>
<pl:Categories>
<xsl:copy-of select="$distinct-cats"/>
</pl:Categories>
<xsl:apply-templates select="$input-docs//pl:Request"/>
</pl:Submission>
</ProductList>
</xsl:template>
<xsl:template match="pl:Category">
<cat catId="{$distinct-cats[. = current()/pl:Name]/@id}"/>
</xsl:template>
</xsl:stylesheet>
您可以使用相同的方法来识别独特的描述并引用它们。