我正在研究Access上的新代码,我似乎无法解决这个问题。我对xslt很新,我理解“For-Each”的基本概念,但我仍然遇到麻烦。我希望你们中的一些人可以帮助我理解为什么我的代码不能正常工作。
我所希望的是在Access中的表中打印所有这些,并将所有分支名称分别放在不同的行中。目前,我将分支名称打印在一行中。
一切都有帮助。谢谢你们!
XSLT代码
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<ING_PNA_Import>
<xsl:for-each select="PNAResponse/PriceAndAvailability">
<Import>
<Test>
<SKU>
<xsl:value-of select="@SKU"/>
</SKU>
<VendorNumber>
<xsl:value-of select="VendorNumber"/>
</VendorNumber>
<Price>
<xsl:value-of select="Price"/>
</Price>
<MPN>
<xsl:value-of select="ManufacturerPartNumber"/>
</MPN>
<ID>
<xsl:value-of select="Branch/@ID"/>
<xsl:text>
</xsl:text>
</ID>
<Name>
<xsl:for-each select="Branch">
<xsl:value-of select="@Name"/>
</xsl:for-each>`
</Name>
<Availability>
<xsl:value-of select="Branch/Availability"/>
</Availability>
</Test>
</Import>
</xsl:for-each>
</ING_PNA_Import>
XML代码:
<?xml version="1.0" encoding="iso-8859-1"?>
<PNAResponse>
<Version>2.0</Version>
<TransactionHeader>
<SenderID>YOU</SenderID>
<ReceiverID>MD</ReceiverID>
<ErrorStatus ErrorNumber=""></ErrorStatus>
<DocumentID></DocumentID>
<TransactionID></TransactionID>
</TransactionHeader>
<PriceAndAvailability SKU="100150" Quantity="1">
<Price>21.21</Price>
<SpecialPriceFlag></SpecialPriceFlag>
<ManufacturerPartNumber>TEST1234</ManufacturerPartNumber>
<ManufacturerPartNumberOccurs></ManufacturerPartNumberOccurs>
<VendorNumber>123X</VendorNumber>
<Description>Cool Item</Description>
<ReserveInventoryFlag>Y</ReserveInventoryFlag>
<AvailableRebQty>0</AvailableRebQty>
<Branch Name="Town1" ID="01">
<Availability>8</Availability>
<OnOrder>0</OnOrder>
<ETADate></ETADate>
</Branch>
<Branch Name="Town2" ID="02">
<Availability>8</Availability>
<OnOrder>0</OnOrder>
<ETADate></ETADate>
</Branch>
<Branch Name="Town3" ID="03">
<Availability>6</Availability>
<OnOrder>5</OnOrder>
<ETADate></ETADate>
</Branch>
<Branch Name="Town4" ID="04">
<Availability>5</Availability>
<OnOrder>5</OnOrder>
<ETADate></ETADate>
</Branch>
<Branch Name="Town5" ID="05">
<Availability>10</Availability>
<OnOrder>0</OnOrder>
<ETADate></ETADate>
</Branch>
<UPC>123456789012345</UPC>
<CustomerPartNumber></CustomerPartNumber>
答案 0 :(得分:0)
Simply nest your <Name>
node within the Branch <xsl:for-each>
to achieve multiple child nodes:
Previous
<Name>
<xsl:for-each select="Branch">
<xsl:value-of select="@Name"/>
</xsl:for-each>`
</Name>
Updated
<xsl:for-each select="Branch">
<Name>
<xsl:value-of select="@Name"/>
</Name>
</xsl:for-each>
Output
<?xml version='1.0' encoding='UTF-8'?>
<ING_PNA_Import>
<Import>
<Test>
<SKU>100150</SKU>
<VendorNumber>123X</VendorNumber>
<Price>21.21</Price>
<MPN>TEST1234</MPN>
<ID>01</ID>
<Name>Town1</Name>
<Name>Town2</Name>
<Name>Town3</Name>
<Name>Town4</Name>
<Name>Town5</Name>
<Availability>8</Availability>
</Test>
</Import>
</ING_PNA_Import>
However, in MS Access, like most databases, you require distinct column names, so consider suffixing with a number, namely the node's position:
<xsl:for-each select="Branch">
<xsl:element name="{concat('Name', position())}">
<xsl:value-of select="@Name"/>
</xsl:element>
</xsl:for-each>
Output
<?xml version='1.0' encoding='UTF-8'?>
<ING_PNA_Import>
<Import>
<Test>
<SKU>100150</SKU>
<VendorNumber>123X</VendorNumber>
<Price>21.21</Price>
<MPN>TEST1234</MPN>
<ID>01</ID>
<Name1>Town1</Name1>
<Name2>Town2</Name2>
<Name3>Town3</Name3>
<Name4>Town4</Name4>
<Name5>Town5</Name5>
<Availability>8</Availability>
</Test>
</Import>
</ING_PNA_Import>
Finally, all this can be done without <xsl:for-each>
which often is regularly used by newcomers in XSLT so used to iterative programming coming from general purpose languages not yet aware of the recursive template mapping of XSLT. Also, your Availability currently only takes from first Branch node. Below re-creates but think about mapping all values with corresponding Branch in last template:
Alternative
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/PNAResponse">
<ING_PNA_Import>
<xsl:apply-templates select="PriceAndAvailability"/>
</ING_PNA_Import>
</xsl:template>
<xsl:template match="PriceAndAvailability">
<Import>
<Test>
<SKU><xsl:value-of select="@SKU"/></SKU>
<VendorNumber><xsl:value-of select="VendorNumber"/></VendorNumber>
<Price><xsl:value-of select="Price"/></Price>
<MPN><xsl:value-of select="ManufacturerPartNumber"/></MPN>
<ID><xsl:value-of select="Branch/@ID"/></ID>
<xsl:apply-templates select="Branch"/>
<xsl:copy-of select="Branch[1]/Availability"/>
</Test>
</Import>
</xsl:template>
<xsl:template match="Branch">
<xsl:element name="{concat('Name', position())}">
<xsl:value-of select="@Name"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>