对于带有名称空间XML文件,我必须在XSL文件中使用该名称空间。但它不起作用。
下面是我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<data xmlns="http://www.aspzone.com/xml/serialization">
<parent>
<string xmlns="">AAA</string>
</parent>
<nb>
<string >2</string>
</nb>
<child1>aaa-1
<s1><string >1aaa-1 </string></s1>
<s1><string >2aaa-1</string></s1>
</child1>
<child1>aaa-2</child1>
<parent>
<string >BBB</string>
</parent>
<nb>
<string>1</string>
</nb>
<child2>bbb-1</child2>
</data>和名称空间为“ser”的XSL文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ser="http://www.aspzone.com/xml/serialization" >
<xsl:output indent="yes"/>
<xsl:template match="ser:data">
<xsl:copy>
<xsl:apply-templates select="ser:parent"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ser:parent[string = 'AAA']">
<xsl:copy>
<xsl:variable name="n" select="following-sibling::ser:nb[1]"/>
<xsl:copy-of select="string | following-sibling::ser:nb[1] | following-sibling::ser:child1[position() <= $n]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ser:parent[string != 'AAA']">
<xsl:copy>
<xsl:variable name="n" select="following-sibling::ser:nb[1]"/>
<xsl:copy-of select="string | following-sibling::ser:nb[1] | following-sibling::ser:child2[position() <= $n]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>当我使用前缀"ser:“时,它会生成:
<?xml version="1.0" encoding="UTF-8"?>
<data xmlns="http://www.aspzone.com/xml/serialization">
<parent>
<string xmlns="">AAA</string>
<nb>
<string>2</string>
</nb>
<child1>aaa-1
<s1><string >1aaa-1</string></s1>
<s1><string >2aaa-1</string></s1>
</child1>
<child1>aaa-2</child1>
</parent>
BBB
</data>虽然期望的是
<?xml version="1.0" encoding="UTF-8"?>
<data>
<parent>
<string>AAA</string>
<nb>
<string>2</string>
</nb>
<child1>aaa-1
<s1><string >1aaa-1 </string></s1>
<s1><string >2aaa-1</string></s1>
</child1>
<child1>aaa-2</child1>
</parent>
<parent>
<string>BBB</string>
<nb>
<string>1</string>
</nb>
<child2>bbb-1</child2>
</data>不检索childe2节点...出什么问题了?当不使用名称空间时,它工作得很好。
诚挚的问候
发布于 2018-07-17 01:56:52
未检索childe2节点...出什么问题了?当不使用名称空间时,它工作得很好。
这可能是因为你的匹配ser:parent[string != 'AAA']从来不匹配任何东西。
这是因为在第二个ser:parent中,子string位于默认名称空间http://www.aspzone.com/xml/serialization中(而在第一个ser:parent中,string位于空名称空间中)。
考虑使用xsl:key代替...
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ser="http://www.aspzone.com/xml/serialization">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="elemByParent" match="ser:data/*[not(self::ser:parent)]"
use="generate-id(preceding-sibling::ser:parent[1])"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ser:data">
<xsl:copy>
<xsl:apply-templates select="@*|ser:parent"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ser:parent">
<xsl:copy>
<xsl:apply-templates select="@*|node()|key('elemByParent',generate-id())"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ser:s1"/>
</xsl:stylesheet>小提琴:http://xsltfiddle.liberty-development.net/gWmuiJr
发布于 2018-07-17 03:17:13
xsl:copy指令生成一个与原始元素同名的元素:即,相同的本地名称和相同的命名空间。因此,如果在输入中有{http://www.aspzone.com/xml/serialization}data,并且希望在输出中有{}data (更改名称空间和名称),那么就不能使用xsl:copy;您需要使用xsl:element。
https://stackoverflow.com/questions/51366304
复制相似问题