2013-11-23 65 views
2

所以我想列出所有的子節點名稱和Specs節點的文本。列出元素的子節點XSLT

<Item-Request model="MZ-7TE1T0BW" Supplier-Code="TOMSAM1TB"> 
    <DisplayItem> 
     <Item href="SAM1TBMZ7TE1T0BW.jpg" model="MZ-7TE1T0BW"> 
      <Name>Samsung SSD MZ-7TE1T0BW 1 TB 2.5 inch</Name> 
      <Price>630.99</Price> 
      <SupplierCode>TOMSAM1TB</SupplierCode> 
      <Description/> 
      <Specs> 
       <Capacity>1 TB</Capacity> 
       <Reading>540 MB/s</Reading> 
       <Writing>520 MB/s</Writing> 
       <FormFactor>2.5 "</FormFactor> 
       <Connecor>Sata III</Connecor> 
       <Size> 
        <Width>70 mm</Width> 
        <Height>7 mm</Height> 
       </Size> 
       <Weight>53 g</Weight> 
      </Specs> 
      <Supplier>TOM001</Supplier> 
      <SupplierName>Tom PC Hardware</SupplierName> 
      <Manufacturer>Samsung</Manufacturer> 
     </Item> 
    </DisplayItem> 
</Item-Request> 

我不能這樣做硬編碼,因爲這些都不是值,我已經隨時可用,可以添加或刪除。所以我需要一些可以動態列出它們的東西。

到目前爲止,我已經能夠做的就是

<xsl:for-each select="DisplayItem/Item/Specs"> 
           <xsl:for-each select="node()"> 
            <xsl:value-of select="."/> 
            <br/> 
           </xsl:for-each> 
          </xsl:for-each> 

以上列出一個單獨的行中的值,現在我需要能夠顯示該元素的名稱。

回答

1

而不是騎自行車穿越所有的孩子,你應該使用你找到的規格元素的模板,並將每個元素轉換爲文本。

我會用下面的樣式表(它似乎要生成HTML):

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" 
           xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" indent="yes"/> 

    <xsl:template match="/"> 
    <xsl:text disable-output-escaping='yes'>&lt;!DOCTYPE html></xsl:text> 
     <html> 
     <body> 
     <xsl:apply-templates/> 
     </body> 
     </html> 
    </xsl:template> 

    <xsl:template match="Specs"> 
     <xsl:for-each select="*"> <!-- take all descendants of Specs --> 
     <p> 
      <xsl:value-of select="name()"/> 
      <xsl:text> = </xsl:text> 

      <!-- this just copies the text() of possible descendants! --> 
      <xsl:value-of select="."/> 
     </p> 
     </xsl:for-each> 
    </xsl:template> 

    <!-- do nothing for unmatched text or attribute nodes --> 
    <xsl:template match="text()|@*"/> 

</xsl:stylesheet> 

你仍將面臨一個問題,嵌套的規格一樣<Size>例如。使用此模板,輸出是(減去一些額外的空格):

<p>Size = 70 mm 7 mm</p> 
+0

我將解決,通過分別將它們放置 – Enzero

1

,我發現這個解決方案是

<xsl:for-each select="DisplayItem/Item/Specs"> 
    <xsl:for-each select="node()"> 
     <xsl:value-of select="name()"/> 
     <xsl:value-of select="."/> 
     <br/> 
    </xsl:for-each> 
</xsl:for-each> 

我不能確定它是否是正確的方法。

0

您也可以試試。

XSL:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output indent="yes" /> 

    <xsl:template match="/"> 
    <xsl:copy-of select="//Specs"/> 
    </xsl:template> 
</xsl:stylesheet> 

應用到你的輸入XML的意願;

輸出:

<?xml version="1.0" encoding="UTF-8"?> 
<Specs> 
    <Capacity>1 TB</Capacity> 
    <Reading>540 MB/s</Reading> 
    <Writing>520 MB/s</Writing> 
    <FormFactor>2.5 "</FormFactor> 
    <Connecor>Sata III</Connecor> 
    <Size> 
     <Width>70 mm</Width> 
     <Height>7 mm</Height> 
    </Size> 
    <Weight>53 g</Weight> 
</Specs>