我有一个样式表,用于根据其他元素的值删除某些元素。但是,它不起作用......
示例输入 XML
<Model>
<Year>1999</Year>
<Operation>ABC</Operation>
<Text>Testing</Text>
<Status>Ok</Status>
</Model>
Run Code Online (Sandbox Code Playgroud)
如果操作值为“ABC”,则从 XML 中删除文本和状态节点。并给出以下输出。
<Model>
<Year>1999</Year>
<Operation>ABC</Operation>
</Model>
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的样式表,但即使操作不是“ABC”,它也会从所有 XML 中删除文本和状态节点。
<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:variable name="ID" select="//Operation"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Text | Status">
<xsl:if test ="$ID ='ABC'">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
提前致谢
当命名空间存在时,我将如何做同样的事情
<ns0:next type="Sale" xmlns:ns0="http://Test.Schemas.Inside_Sales">
Run Code Online (Sandbox Code Playgroud)
这是一个完整的 XSLT 转换——简短而简单(没有变量,没有xsl:if, xsl:choose, xsl:when, xsl:otherwise):
<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=
"*[Operation='ABC']/Text | *[Operation='ABC']/Status"/>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当此转换应用于提供的 XML 文档时:
<Model>
<Year>1999</Year>
<Operation>ABC</Operation>
<Text>Testing</Text>
<Status>Ok</Status>
</Model>
Run Code Online (Sandbox Code Playgroud)
产生了想要的、正确的结果:
<Model>
<Year>1999</Year>
<Operation>ABC</Operation>
</Model>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8449 次 |
| 最近记录: |