-2

我想用 xslt 从以下数据中删除一些不需要的文本

Address>
<Rowinfo>
<LocatorDesignator>Dwelling  (Part Of) Null</LocatorDesignator>
<LocatorName>Flat  - Buena Villa House</LocatorName>
</Rowinfo>
<Rowinfo>
<LocatorDesignator>Flat  - Buena Villa House 1</LocatorDesignator>
<LocatorName>Flat  3a  Anderson's House</LocatorName>
</Rowinfo>
<Rowinfo>
<LocatorDesignator>Offices Unit 2a Funlife Building 02a</LocatorDesignator>
<LocatorName>office Unit 2a   Funlife Building  <LocatorName>
 </Rowinfo>
 </Address>

生产这个

<LocatorDesignator>Dwelling(Part Of)</LocatorDesignator>
<LocatorName>Buena Villa House</LocatorName>

<LocatorDesignator>Flat 1</LocatorDesignator>
<LocatorName> Anderson's House</LocatorName>

<LocatorDesignator>office Unit 2a</LocatorDesignator>
<LocatorName> Funlife Building  <LocatorName>

在定位器名称中:提取

  • 布埃纳别墅

  • 安德森的房子

  • 趣生活大厦

换句话说,双引号中的部分将被删除:

(row1) "Flat  -" Buena Villa House.   
(row2) "Flat  3a"  Anderson's House .
(row3) "office Unit 2a"   Funlife Building.

在 Locatordesignator 中:删除

  • 无效的

  • 布埃纳别墅

  • 趣生活大厦

换句话说,双引号中的部分将被删除:

(row1) Dwelling  (Part Of) "Null"
(row2) Flat  " Buena Villa House" 1.   
(row3) office Unit 2a   "Funlife Building 02a". 
4

2 回答 2

1

这个完整的转变

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <my:LocatornameDeletes>
  <del>Flat  -</del>
  <del>Flat  3a</del>
  <del>office Unit 2a</del>
 </my:LocatornameDeletes>

 <my:LocatordesignatorDeletes>
  <del>Null</del>
  <del>Buena Villa House</del>
  <del>Funlife Building 02a</del>
 </my:LocatordesignatorDeletes>

 <xsl:variable name="vLocNameDels" select="document('')/*/my:LocatornameDeletes/*"/>    
 <xsl:variable name="vLocDesDels" select="document('')/*/my:LocatordesignatorDeletes/*"/>   
 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="LocatorName/text()">
  <xsl:call-template name="makeDeletes">
   <xsl:with-param name="pDeletes" select="$vLocNameDels"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template match="LocatorDesignator/text()">
  <xsl:call-template name="makeDeletes">
   <xsl:with-param name="pDeletes" select="$vLocDesDels"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="makeDeletes">
  <xsl:param name="pText" select="."/>
  <xsl:param name="pDeletes"/>

  <xsl:variable name="vDelText" select="$pDeletes[contains($pText, .)][1]"/>
  <xsl:if test="$vDelText">
   <xsl:variable name="vRough">
     <xsl:value-of select="substring-before($pText, $vDelText)"/>
     <xsl:value-of select="substring-after($pText, $vDelText)"/>
   </xsl:variable>

   <xsl:value-of select="normalize-space($vRough)"/>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

当应用于(纠正格式错误)提供的 XML 文档时

<Address>
    <Rowinfo>
        <LocatorDesignator>Dwelling  (Part Of) Null</LocatorDesignator>
        <LocatorName>Flat  - Buena Villa House</LocatorName>
    </Rowinfo>
    <Rowinfo>
        <LocatorDesignator>Flat  - Buena Villa House 1</LocatorDesignator>
        <LocatorName>Flat  3a  Anderson's House</LocatorName>
    </Rowinfo>
    <Rowinfo>
        <LocatorDesignator>Offices Unit 2a Funlife Building 02a</LocatorDesignator>
        <LocatorName>office Unit 2a   Funlife Building  </LocatorName>
    </Rowinfo>
</Address>

产生想要的正确结果

<Address>
   <Rowinfo>
      <LocatorDesignator>Dwelling (Part Of)</LocatorDesignator>
      <LocatorName>Buena Villa House</LocatorName>
   </Rowinfo>
   <Rowinfo>
      <LocatorDesignator>Flat - 1</LocatorDesignator>
      <LocatorName>Anderson's House</LocatorName>
   </Rowinfo>
   <Rowinfo>
      <LocatorDesignator>Offices Unit 2a</LocatorDesignator>
      <LocatorName>Funlife Building</LocatorName>
   </Rowinfo>
</Address>

说明

  1. LocatorName在 global-level element 中提供了 text-node children 中要删除的字符串my:LocatornameDeletes

  2. LocatorDesignator在 global-level element 中提供了 text-node children 中要删除的字符串my:LocatordesignatorDeletes

  3. 身份规则“按原样”复制选择执行它的每个节点。

  4. 有两个模板覆盖了标识模板——一个匹配LocatorName/text(),另一个匹配LocatorDesignator/text()。这些模板中的每一个都简单地调用makeDeletes模板,将包含要删除的子字符串的正确节点集作为参数传递给它。

  5. 模板执行实际的makeDeletes“删除在其$pDeletes参数中传递的子字符串。这是使用标准 XPath 函数substring-before()substring-after().

于 2012-08-26T14:36:46.203 回答
0

您可以在 XSLT 1.0 中使用这些字符串函数来实现您的目标:

  1. 包含()
  2. 子串()
  3. 子串之前()
  4. 子字符串后()

使用 XSLT 搜索函数名称以获取每个函数的规范。

如果您可以升级到 XSLT 2.0,我肯定会推荐它。在 XSLT 2.0 中,您将能够使用正则表达式来更简单地实现您的目标。

例子

有了这个输入文件...

<t>
 <row>"Flat  -" Buena Villa House.</row>  
 <row>"Flat  3a"  Anderson's House .</row>
 <row>"office Unit 2a"   Funlife Building.</row>
</t>

...当此 XSLT 1.0 样式表应用于所述文档时,将删除双引号文本片段...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
 <t>
   <xsl:apply-templates select="*/row"/>
 </t>
</xsl:template>

<xsl:template match="row">
 <row>
  <xsl:call-template name="remove-quoted-bit">
    <xsl:with-param name="raw-text" select="." />
  </xsl:call-template>  
 </row>
</xsl:template>
<xsl:template name="remove-quoted-bit">
  <xsl:param name="raw-text" />
  <xsl:choose>
    <xsl:when test="contains($raw-text,'&quot;')">
      <xsl:value-of select="concat(
        substring-before($raw-text,'&quot;'),
        substring-after( substring-after($raw-text,'&quot;'),'&quot;')
        )" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$raw-text" />
    </xsl:otherwise>  
  </xsl:choose>  
</xsl:template>  

</xsl:stylesheet>

...并产生此输出...

<t>
  <row> Buena Villa House.</row>
  <row>  Anderson's House .</row>
  <row>   Funlife Building.</row>
</t>

更新

语法编辑。

于 2012-08-26T13:39:14.633 回答