某些命名空间中的XSLT过滤节点(XSLT filter nodes in certain namespace)

编程入门 行业动态 更新时间:2024-10-28 20:18:02
某些命名空间中的XSLT过滤节点(XSLT filter nodes in certain namespace)

我(非常)是XSLT的新手,并希望有人可以告诉我如何在XML文档中使用XSLT过滤掉某些节点,具体取决于定义节点的命名空间。我有以下简化示例:

Input: <root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="xxx:yyy:datamodel:SW:ABCD:01 AB_01.xsd" xmlns="xxx:yyy:datamodel:SW:ABCD:01"> <Document xmlns="ABCD:ZYX:01"> <TypeCode>SEC</TypeCode> </Document> <Document xmlns="ABCD:NOP:01"> <TypeCode>DEC</TypeCode> </Document> </root>

输出:

SEC

所以我只想解析Namespace ABCD中的Document节点:ZYX:01。

到目前为止这是我的xslt:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sec="xxx:yyy:datamodel:ABCD:ZYX:01" version="1.0"> <xsl:template match="sec:TypeCode"> <xsl:for-each select="//*[local-name() = 'TypeCode']"> <xsl:value-of select="." /> </xsl:for-each> </xsl:template> </xsl:stylesheet>

提前致谢!

I am (very) new to XSLT and was hoping somebody could show me how to filter out certain nodes with XSLT in an XML document depending on the namespace that node is defined in. I have the following simplified example:

Input: <root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="xxx:yyy:datamodel:SW:ABCD:01 AB_01.xsd" xmlns="xxx:yyy:datamodel:SW:ABCD:01"> <Document xmlns="ABCD:ZYX:01"> <TypeCode>SEC</TypeCode> </Document> <Document xmlns="ABCD:NOP:01"> <TypeCode>DEC</TypeCode> </Document> </root>

Output:

SEC

So I want only to parse Document nodes in Namespace ABCD:ZYX:01.

This is my xslt so far:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sec="xxx:yyy:datamodel:ABCD:ZYX:01" version="1.0"> <xsl:template match="sec:TypeCode"> <xsl:for-each select="//*[local-name() = 'TypeCode']"> <xsl:value-of select="." /> </xsl:for-each> </xsl:template> </xsl:stylesheet>

Thanks in advance!

最满意答案

在您的XSLT中,您已经定义了名称空间“xxx:yyy:datamodel:ABCD:ZYX:01”,但是在您的XML中,您想要的TypeCode位于默认名称空间“ABCD:ZYX:01”中,因此您的模板匹配在XSLT中实际上不会匹配它。

你可能想做这样的事......

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:sec="ABCD:ZYX:01"> <xsl:output method="text" /> <xsl:template match="/"> <xsl:for-each select="//sec:TypeCode"> <xsl:value-of select="." /> </xsl:for-each> </xsl:template> </xsl:stylesheet>

In your XSLT you have defined a namespace of "xxx:yyy:datamodel:ABCD:ZYX:01", but in your XML, the TypeCode you want is in the default namespace of "ABCD:ZYX:01", so your template match in the XSLT will not actually match it.

You probably want to do something like this...

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:sec="ABCD:ZYX:01"> <xsl:output method="text" /> <xsl:template match="/"> <xsl:for-each select="//sec:TypeCode"> <xsl:value-of select="." /> </xsl:for-each> </xsl:template> </xsl:stylesheet>

更多推荐

xmlns,ABCD,XSLT,电脑培训,计算机培训,IT培训"/> <meta name="description&q

本文发布于:2023-08-03 01:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1383145.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   空间   XSLT   namespace   filter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!