2

我有这些 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)元素中,我想提取以房地产和岛屿为后缀的值。在通道元素中,我想删除以庄园和岛屿为后缀的值。在定位器名称中提取任何带有房屋或建筑物的值。

4

1 回答 1

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>

二、处理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>

三、XSLT 2.0 解决方案

这更容易——只需在(XSLT 1.0 解决方案的)谓词中使用标准 XPath 2.0 函数ends-with()

于 2012-08-22T13:29:36.230 回答