我知道这听起来像是一个常见问题,但我已经尝试了该站点上的所有类似解决方案,但均未成功。现在我正在尝试使用 xslt 将现有的 xml 文档转换为符合新模式的 xml 文档。我正在尝试获取名称和零件编号以显示每个产品。但相反,我只得到一个包含所有名称和部分名称的产品节点。我已经尝试使用 for each 循环,我已经尝试过这个解决方案如何获取一个转换后的 xml 文件,其中只有一个父标签下的父标签每次出现的所有子标签,结果非常接近,但唯一的问题是我让所有产品终止嵌套在产品标签中的标签。
这是我当前的xml:
<CATALOG>
<PRODUCT NAME="18 Volt Cordless DRILL" PARTNUM="12">
<SPECIFICATIONS><WEIGHT>8 oz. </WEIGHT><POWER>18 Volt</POWER></SPECIFICATIONS>
<OPTIONS FINISH="Polished"/>
<PRICE> <MSRP>139.99</MSRP>
<WHOLESALE>89.99</WHOLESALE><SHIPPING>10.00</SHIPPING></PRICE>
<NOTES>On sale for $99.99 for one week only.</NOTES>
</PRODUCT>
<PRODUCT NAME="Variable Speed Drill Press" PARTNUM="76" CATEGORY="Shop-Professional">
<SPECIFICATIONS><WEIGHT>100 lbs.</WEIGHT><POWER>120 Volt</POWER></SPECIFICATIONS>
<OPTIONS FINISH="Metal"/>
<PRICE> <MSRP>299.99</MSRP>
<WHOLESALE>199.99</WHOLESALE><SHIPPING>50.00</SHIPPING></PRICE>
<NOTES>Ground shipping only.</NOTES>
</PRODUCT>
<PRODUCT NAME="Gas powered generator" PARTNUM="54" CATEGORY="Shop-Professional">
<SPECIFICATIONS><WEIGHT>75 lbs.</WEIGHT><POWER>120 Volt</POWER></SPECIFICATIONS>
<OPTIONS FINISH="Metal" ADAPTER="Optional" CASE="NotApplicable"/>
<PRICE> <MSRP>1122.99</MSRP><WHOLESALE>654.99</WHOLESALE><SHIPPING>41.00</SHIPPING></PRICE>
<NOTES>The most powerfull generator made.</NOTES>
</PRODUCT>
<PRODUCT NAME="Hammer" PARTNUM="1" CATEGORY="HandTool">
<SPECIFICATIONS><WEIGHT>5 lbs.</WEIGHT></SPECIFICATIONS>
<PRICE> <MSRP>9.99</MSRP><WHOLESALE>7.99</WHOLESALE><SHIPPING>2.00</SHIPPING></PRICE>
</PRODUCT>
<PRODUCT NAME="Screwdriver" PARTNUM="2" CATEGORY="HandTool">
<SPECIFICATIONS><WEIGHT>1 lbs.</WEIGHT></SPECIFICATIONS>
<PRICE> <MSRP>11.99</MSRP><WHOLESALE>9.99</WHOLESALE><SHIPPING>3.00</SHIPPING></PRICE>
</PRODUCT>
<PRODUCT NAME="Saw" PARTNUM="3" >
<SPECIFICATIONS><WEIGHT>3 lbs.</WEIGHT></SPECIFICATIONS>
<OPTIONS FINISH="Metal" ADAPTER="NotApplicable" CASE="NotApplicable"/>
<PRICE> <MSRP>5.99</MSRP><WHOLESALE>7.99</WHOLESALE><SHIPPING>2.50</SHIPPING></PRICE>
</PRODUCT>
<PRODUCT NAME="Black and Decker Variable Drill" PARTNUM="2456" CATEGORY="HandTool">
<SPECIFICATIONS><WEIGHT>2 lbs." </WEIGHT><POWER>1/3HP</POWER></SPECIFICATIONS>
<OPTIONS FINISH="Metal" ADAPTER="Included" CASE="HardShell"/>
<PRICE> <MSRP>35.99</MSRP>
<WHOLESALE>24.95</WHOLESALE><SHIPPING>4.95</SHIPPING></PRICE>
<NOTES>This item is on clearance.</NOTES>
</PRODUCT>
<PRODUCT NAME="Black and Decker Rotary Tablesaw" PARTNUM="2364" CATEGORY="Table">
<SPECIFICATIONS><WEIGHT>25 lbs.</WEIGHT><POWER>2/3HP</POWER></SPECIFICATIONS>
<OPTIONS FINISH="Metal" ADAPTER="NotApplicable" CASE="NotApplicable"/>
<PRICE> <MSRP>45.95</MSRP><WHOLESALE>34.95</WHOLESALE><SHIPPING>4.95</SHIPPING></PRICE>
<NOTES>See Manufacturer's warning label.</NOTES>
</PRODUCT>
<PRODUCT NAME="Black and Decker Workman's Bench" PARTNUM="3446" CATEGORY="Shop-Professional">
<SPECIFICATIONS><WEIGHT>50 lbs.</WEIGHT></SPECIFICATIONS>
<OPTIONS FINISH="Matte" ADAPTER="NotApplicable" CASE="NotApplicable"/>
<PRICE> <MSRP>72.45</MSRP><WHOLESALE>56.99</WHOLESALE><SHIPPING>10.95</SHIPPING></PRICE>
<NOTES />
</PRODUCT>
</CATALOG>
这是我得到的输出
<?xml version="1.0" encoding="utf-8"?>
<CATALOG>
<PRODUCT>
<PRODUCT/>
<NAME>18 Volt Cordless DRILL</NAME>
<PARTNUM>12</PARTNUM>
<PRODUCT/>
<NAME>Variable Speed Drill Press</NAME>
<PARTNUM>76</PARTNUM>
<PRODUCT/>
<NAME>Gas powered generator</NAME>
<PARTNUM>54</PARTNUM>
<PRODUCT/>
<NAME>Hammer</NAME>
<PARTNUM>1</PARTNUM>
<PRODUCT/>
<NAME>Screwdriver</NAME>
<PARTNUM>2</PARTNUM>
<PRODUCT/>
<NAME>Saw</NAME>
<PARTNUM>3</PARTNUM>
<PRODUCT/>
<NAME>Black and Decker Variable Drill</NAME>
<PARTNUM>2456</PARTNUM>
<PRODUCT/>
<NAME>Black and Decker Rotary Tablesaw</NAME>
<PARTNUM>2364</PARTNUM>
<PRODUCT/>
<NAME>Black and Decker Workman's Bench</NAME>
<PARTNUM>3446</PARTNUM>
<PRODUCT/>
</PRODUCT>
</CATALOG>
这是我要检索的输出
<CATALOG>
<PRODUCT>
<NAME>18 Volt Cordless DRILL</NAME>
<PARTNUM>12</PARTNUM>
<PRODUCT/>
<PRODUCT>
<NAME>Variable Speed Drill Press</NAME>
<PARTNUM>76</PARTNUM>
<PRODUCT/>
<PRODUCT>
<NAME>Gas powered generator</NAME>
<PARTNUM>54</PARTNUM>
<PRODUCT/>
<!---And so on, you get the idea-->
</CATALOG>
这是xslt
<?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="/">
<xsl:element name="CATALOG">
<xsl:apply-templates select="/CATALOG"/>
</xsl:element>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:element name="PRODUCT" >
<xsl:apply-templates select="node()|@*"/>
</xsl:element>
</xsl:template>
<xsl:template match="PRODUCT">
<xsl:element name="NAME">
<xsl:value-of select="@NAME"/>
</xsl:element>
<xsl:element name="PARTNUM">
<xsl:value-of select="@PARTNUM"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>