我有这些xml数据集
<Address>
<Rowinfo>
<LocatorDesignator>Dwelling (Part Of), Null</LocatorDesignator>
<LocatorName>Flat - Buena Villa House</LocatorName>
<thoroughfare>James Passage</thoroughfare>
<AddressArea>Modakeke island</AddressArea>
</Rowinfo>
<Rowinfo>
<LocatorDesignator>Flat - Buena Villa House, 1</LocatorDesignator>
<LocatorName>Flat 3a Anderson's House</LocatorName>
<thoroughfare>Abesinia Passage</thoroughfare>
<AddressArea> Buena Villa Road</AddressArea>
</Rowinfo>
<Rowinfo>
<LocatorDesignator>Offices Unit 2a Funlife Building, 02a</LocatorDesignator>
<LocatorName>Offices Unit 2a Funlife Building <LocatorName>
<thoroughfare> Modakeke island</thoroughfare>
<AddressArea>Laguna Estate</AddressArea>
</Rowinfo>
</Address>
我想将他们变成这个
<LocatorDesignator>Dwelling(Part Of)</LocatorDesignator>
<LocatorName>Buena Villa House</LocatorName>
<thoroughfare>James Passage </thoroughfare>
<AddressArea>Modakeke island</AddressArea>
<LocatorDesignator>Flat 1</LocatorDesignator>
<LocatorName> Anderson's House</LocatorName>
<thoroughfare>Abesinia Passage</thoroughfare>
<AddressArea> </AddressArea>
<LocatorDesignator>Offices Unit 2a</LocatorDesignator>
<LocatorName> Funlife Building <LocatorName>
<thoroughfare> </thoroughfare>
<AddressArea>Laguna Estate</AddressArea>
基本上在(addressarea)元素中我想用estate和island作为后缀提取值。在通道元素中,我想删除带有estate和island作为后缀的值。房屋或建筑物的任何值都将在定位器名称中提取。
答案 0 :(得分:0)
<强>予。删除AddressArea
中未以"island"
结尾的文本节点:
<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=
"AddressArea/text()
[substring(., string-length() -5) != 'island'
and
substring(., string-length() -5) != 'Estate'
]"/>
</xsl:stylesheet>
将此转换应用于提供的XML文档(更正为格式正确):
<Address>
<Rowinfo>
<LocatorDesignator>Dwelling (Part Of), Null</LocatorDesignator>
<LocatorName>Flat - Buena Villa House</LocatorName>
<thoroughfare>James Passage</thoroughfare>
<AddressArea>Modakeke island</AddressArea>
</Rowinfo>
<Rowinfo>
<LocatorDesignator>Flat - Buena Villa House, 1</LocatorDesignator>
<LocatorName>Flat 3a Anderson's House</LocatorName>
<thoroughfare>Abesinia Passage</thoroughfare>
<AddressArea></AddressArea>
</Rowinfo>
<Rowinfo>
<LocatorDesignator>Offices Unit 2a Funlife Building, 02a</LocatorDesignator>
<LocatorName>Offices Unit 2a Funlife Building </LocatorName>
<thoroughfare> Modakeke island</thoroughfare>
<AddressArea>Laguna Estate</AddressArea>
</Rowinfo>
</Address>
<强> II。处理thoroughfare
只需将此模板添加到上面的代码中:
<xsl:template match=
"thoroughfare/text()
[substring(., string-length() -5) = 'island']"/>
现在完整的代码:
<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=
"AddressArea/text()
[substring(., string-length() -5) != 'island'
and
substring(., string-length() -5) != 'Estate'
]"/>
<xsl:template match=
"thoroughfare/text()
[substring(., string-length() -5) = 'island']"/>
</xsl:stylesheet>
,当应用于提供的XML文档(上图)时,会生成所需的正确结果:
<Address>
<Rowinfo>
<LocatorDesignator>Dwelling (Part Of), Null</LocatorDesignator>
<LocatorName>Flat - Buena Villa House</LocatorName>
<thoroughfare>James Passage</thoroughfare>
<AddressArea>Modakeke island</AddressArea>
</Rowinfo>
<Rowinfo>
<LocatorDesignator>Flat - Buena Villa House, 1</LocatorDesignator>
<LocatorName>Flat 3a Anderson's House</LocatorName>
<thoroughfare>Abesinia Passage</thoroughfare>
<AddressArea/>
</Rowinfo>
<Rowinfo>
<LocatorDesignator>Offices Unit 2a Funlife Building, 02a</LocatorDesignator>
<LocatorName>Offices Unit 2a Funlife Building </LocatorName>
<thoroughfare/>
<AddressArea>Laguna Estate</AddressArea>
</Rowinfo>
</Address>
<强> III。 XSLT 2.0解决方案
这更容易 - 只需在谓词(XSLT 1.0解决方案)中使用标准XPath 2.0函数 ends-with()
。