【发布时间】:2017-02-02 21:48:00
【问题描述】:
在下面的 XML 中,我需要搜索标签 attrGroupMany name="allergenRelatedInformation" 并 删除所有子节点 () 除了它下面的第一个节点。
不确定 XSLT/Java DOM 是否是实现这一目标的最佳方式。请帮助。
XPathExpression delExpr = xpath.compile("//flexTM/attrGroupMany[starts-with(@name,'allergenRelatedInformation')]/row");
Object obj = expr.evaluate(doc, XPathConstants.NODESET);
NodeList row = (NodeList) obj;
for(int i = 1; i < row.getLength(); i++)
{
Node attr = row.item(i);
Element e = (Element) attr;
System.out.println(row.item(i).getParentNode().getNodeName());
row.item(1).getParentNode().removeChild(e);
doc.normalize();
i-- }
<attrGroupMany name="manufacturer">
<row>
<attr name="gln">123456</attr>
<attr name="name">ABC Inc</attr>
</row>
</attrGroupMany>
<attrGroupMany name="allergenRelatedInformation">
<row>
<attr name="allergenSpecificationAgency">FDA</attr>
<attr name="allergenSpecificationName">BIG 8</attr>
<attrGroupMany name="allergen">
<row>
<attr name="allergenTypeCode">AC</attr>
<attr name="levelOfContainmentCode">FREE_FROM</attr>
</row>
</attrGroupMany>
</row>
<row>
<attr name="allergenSpecificationAgency">FDA</attr>
<attr name="allergenSpecificationName">BIG 8</attr>
<attrGroupMany name="allergen">
<row>
<attr name="allergenTypeCode">AE</attr>
<attr name="levelOfContainmentCode">FREE_FROM</attr>
</row>
</attrGroupMany>
</row>
<row>
<attr name="allergenSpecificationAgency">FDA</attr>
<attr name="allergenSpecificationName">BIG 8</attr>
<attrGroupMany name="allergen">
<row>
<attr name="allergenTypeCode">AF</attr>
<attr name="levelOfContainmentCode">FREE_FROM</attr>
</row>
</attrGroupMany>
</row>
<row>
<attr name="allergenSpecificationAgency">FDA</attr>
<attr name="allergenSpecificationName">BIG 8</attr>
<attrGroupMany name="allergen">
<row>
<attr name="allergenTypeCode">AM</attr>
<attr name="levelOfContainmentCode">FREE_FROM</attr>
</row>
</attrGroupMany>
</row>
</attrGroupMany>
【问题讨论】:
标签: java xslt dom xml-parsing xslt-2.0