Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用xslt操作XMLTextNode_Xml_Xslt_Xpath - Fatal编程技术网

用xslt操作XMLTextNode

用xslt操作XMLTextNode,xml,xslt,xpath,Xml,Xslt,Xpath,我想用xslt从以下数据中删除一些不需要的文本 Address> <Rowinfo> <LocatorDesignator>Dwelling (Part Of) Null</LocatorDesignator> <LocatorName>Flat - Buena Villa House</LocatorName> </Rowinfo> <Rowinfo> <LocatorDesignator>

我想用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>
在定位器指示器中:删除

  • 空的

  • 布埃纳别墅酒店

  • Funlife大厦

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

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

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

  • 包含()
  • 子字符串()
  • 子字符串-before()
  • 子字符串-after()
  • 用谷歌搜索函数名和XSLT来获得每个函数的规范

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

    例子 使用此输入文档

    <t>
     <row>"Flat  -" Buena Villa House.</row>  
     <row>"Flat  3a"  Anderson's House .</row>
     <row>"office Unit 2a"   Funlife Building.</row>
    </t>
    
    
    “公寓——”布埃纳别墅。
    “3a公寓”安德森的房子。
    “办公单元2a”Funlife大厦。
    
    …当此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>
    
    
    布埃纳别墅。
    安德森家。
    Funlife大厦。
    
    更新
    语法已编辑。

    此完整转换:

    <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>
    
    <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>
    
    
    平坦的-
    公寓3a
    办公室单元2a
    无效的
    布埃纳别墅酒店
    Funlife大厦02a
    
    应用于(已纠正格式错误的)提供的XML文档时

    <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>
    
    <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>
    


    这意味着你甚至不想找出工作代码?人工智能是一种选择吗?子字符串操作似乎无法满足您的需要…lee,您必须解释必须将什么更改为什么--这一点都不明显。在一个文本节点中,您正在删除“Flat-”并离开“Buena Villa House”。但是,在另一个文本节点中,您将删除“-Buena Villa House”,并留下“Flat”和“1”。这太令人困惑了!请编辑问题,并描述转换必须实现的确切要求。@Dimitre am在xmlns:my=“my:my”>中有语法错误是什么?错误:F[Saxon6.5.5]元素“my:LocatornameDeletes”的前缀“my”未绑定。@lee,您显然有复制/粘贴错误--请复制整个thransformation并粘贴到您的窗口中
    xmlns:something=“some URI”
    是一个名称空间声明。@lee,我添加了要求的解释。@lee,我不明白--“跳转一行”是什么意思?对不起,我的意思是不删除一行中的任何值,而是移动到下一行进行删除。例如,从第一行删除一个值后,移动到第二行,然后不删除到第三行,然后删除Sean,可以正确地说样式表(或转换)应用于XML文档,而不是相反。Sean B,我把所有内容都混在一起了,请您根据数据(元素)编辑它。感谢you@Lee,我不明白你想要什么,因为你的原始文件没有任何双引号,但你问题的修改部分是用“双引号中的部分”来框定的。这是一个矛盾。无论如何,你应该对Dimitre的全面回答感到满意。我的回答只是为了说明问题和方向。