获取要为每个父节点显示的子节点

时间:2013-11-13 01:10:51

标签: xslt

我知道这可能听起来像是一个常见的问题,但我在这个网站上尝试了所有类似的解决方案并没有成功。现在我正在尝试使用xslt将现有的xml文档转换为符合新架构的xml文档。我正在尝试为每个产品显示名称和partnum。但是我只得到一个包含所有名称和partnums的产品节点。我尝试过为每个循环使用一个,我尝试过这个解决方案how to get a transformed xml file with all child tags of every occurance of parent tag under only one parent tag的结果非常接近,但唯一的问题是我将所有产品终止标签嵌套在产品标签中。

这是我当前的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>

1 个答案:

答案 0 :(得分:0)

我不确定你为什么要在那里进行身份转换,因为你只想要一个非常有限的特定子集。但是,这应该可以解决问题:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/CATALOG">
     <xsl:copy>
       <xsl:apply-templates />
     </xsl:copy>
    </xsl:template>

    <xsl:template match="PRODUCT">
     <xsl:copy>
        <NAME><xsl:value-of select="@NAME"/></NAME>
        <PARTNUM><xsl:value-of select="@PARTNUM"/></PARTNUM>
     </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

当应用于您的输入XML时,请提供:

<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>
    <PRODUCT>
        <NAME>Hammer</NAME>
        <PARTNUM>1</PARTNUM>
    </PRODUCT>
    <PRODUCT>
        <NAME>Screwdriver</NAME>
        <PARTNUM>2</PARTNUM>
    </PRODUCT>
    <PRODUCT>
        <NAME>Saw</NAME>
        <PARTNUM>3</PARTNUM>
    </PRODUCT>
    <PRODUCT>
        <NAME>Black and Decker Variable Drill</NAME>
        <PARTNUM>2456</PARTNUM>
    </PRODUCT>
    <PRODUCT>
        <NAME>Black and Decker Rotary Tablesaw</NAME>
        <PARTNUM>2364</PARTNUM>
    </PRODUCT>
    <PRODUCT>
        <NAME>Black and Decker Workman's Bench</NAME>
        <PARTNUM>3446</PARTNUM>
    </PRODUCT>
</CATALOG>