我(非常)是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
发布评论