我需要洗掉 xml 中一個特定標簽的所有 emtpy 標簽。我只需要針對特定??標簽(語言)執行此操作,并非下面的所有 emtpy 標簽都是我嘗試過的示例 xml 和 xslt 以及預期的輸出
<Data xmlns="http://schemas.ctac.local/inRiver/2019/01/test">
<Item uniqueFieldTypeId="item">
<Fields>
<Field>
<Key>ItemNu</Key>
<Type>String</Type>
<Language/>
<Value>8718848195568</Value>
</Field>
<Field>
<Key>ItemNu</Key>
<Type>CVL</Type>
<Language/>
<Value>ST</Value>
</Field>
<Field>
<Key>ItemNu</Key>
<Type>CVL</Type>
<Language/>
<Value/>
</Field>
</Fields>
</Item>
</Data>
我正在嘗試下面的 xslt 但沒有給出所需的輸出
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"
indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Language/*[not(node())]"/>
</xsl:stylesheet>
所需的輸出是
<Data xmlns="http://schemas.ctac.local/inRiver/2019/01/test">
<Item uniqueFieldTypeId="item">
<Fields>
<Field>
<Key>ItemNu</Key>
<Type>String</Type>
<Value>8718848195568</Value>
</Field>
<Field>
<Key>ItemNu</Key>
<Type>CB</Type>
<Value>ST</Value>
</Field>
<Field>
<Key>ItemNu</Key>
<Type>CL</Type>
<Value/>
</Field>
</Fields>
</Item>
</Data>
uj5u.com熱心網友回復:
因為您的 XML 維護一個默認命名空間,只需為 re-style<Language>
節點分配一個前綴并使用點謂詞進行字串值檢查:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:doc="http://schemas.ctac.local/inRiver/2019/01/test">
<xsl:output omit-xml-declaration="yes"
indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="doc:Language[.='']"/>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/401038.html
上一篇:`XMLStreamReader.getEncoding()`到底做了什么?
下一篇:如何將XML決議為字典串列?