xml 列出元素 XSLT 的子节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20156750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List child nodes of element XSLT
提问by Enzero
So I want to list all the child nodes with name and text of Specs node.
所以我想列出所有带有 Specs 节点名称和文本的子节点。
<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>
I cannot do this hard-coded as these are not values that I have readily available and can be added or removed. So I need something that could dynamically list them.
我无法进行硬编码,因为这些不是我随时可用的值,可以添加或删除。所以我需要一些可以动态列出它们的东西。
So far what I've been able to do is
到目前为止我能做的是
<xsl:for-each select="DisplayItem/Item/Specs">
<xsl:for-each select="node()">
<xsl:value-of select="."/>
<br/>
</xsl:for-each>
</xsl:for-each>
The above lists the values on a separate line now I need to be able to display the element name.
上面列出了单独一行的值,现在我需要能够显示元素名称。
采纳答案by mwil.me
Instead of cycling through all children you should rather use templates for the Specs elements you find and turn each into text.
与其循环遍历所有子元素,不如为找到的 Specs 元素使用模板并将每个元素转换为文本。
I would use the following stylesheet (it seems you want to generate HTML):
我将使用以下样式表(您似乎想要生成 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'><!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>
You will still face a problem with nested Specs like <Size>for example. With this template, the output is (minus some additional whitespace):
您仍然会面临嵌套规范的问题,<Size>例如。使用此模板,输出为(减去一些额外的空格):
<p>Size = 70 mm 7 mm</p>
回答by Enzero
The solution that I found for this was
我为此找到的解决方案是
<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>
I am unsure if it is the correct way.
我不确定这是否是正确的方法。
回答by Hash
You can try this too.
你也可以试试这个。
XSL:
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>
Applied to your input XML will;
应用于您输入的 XML 将;
Output:
输出:
<?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>

