【问题标题】:XSLT Stripping Tags at All LevelsXSLT 剥离所有级别的标签
【发布时间】:2019-05-06 16:02:13
【问题描述】:

我有一些 XML 需要使用 XML 进行转换。当我创建我的 XSLT 时,数据是一种格式,但后来我改变了格式,所以我需要相应地更改我的 XSLT。

XSLT 应该创建一个原始文本标签,然后去掉句子<S> 标签中的元数据并将它们附加到变量名(即<ENAMEX type="PERSON"... 变为ENAMEX_PERSON)。之前整个 xml 是 <DOC> ... </DOC>,但现在是 <NORMDOC> <DOC> ... </DOC> ... </NORMDOC>,所以我在我的选择模式中修复了它,但现在它删除了 <TXT> 之前的所有标签,而在我的选择模式只是 DOC/ 之前它没有。如何更改我的 XSLT 使其仅在 TXT 中执行此剥离?

输入

<NORMDOC>
<DOC>
<DOCID>123</DOCID>
<FI fitype="B" xref="12345">
<FIName>BA</FIName>
<FITIN>456</FITIN>
</FI>
<OIs>
<OI xref="54321">
<OIName>BA</OIName>
</OI>
</OIs>
<Subjects>
<Subject stype="PER" xref="111111">
<SubjectFullName type="L">DISNEY/WALT</SubjectFullName>
<SubjectLastName type="L">DISNEY</SubjectLastName>
<SubjectFirstName type="L">WALT</SubjectFirstName>


<SubjectPhone type="Work">1234567890</SubjectPhone>
<SubjectPhone type="Residence">9876543210</SubjectPhone>
</Subject>
</Subjects>
<TXT>
<S sid="123-SENT-001">INTRODUCTION  this is being filed to report suspicious activity between customer<WH/>&apos;<WH/>s personal account and his animation business.</S> <S sid="123-SENT-002">The following suspect was identified: <ENAMEX type="PERSON" id="PER-123-000">WALT DISNEY</ENAMEX>.</S> <S sid="123-SENT-003">The reportable amount is <NUMEX type="MONEY" id="MON-123-001">$123,456</NUMEX>.</S> <S sid="123-SENT-004">The suspicious activity took place between <TIMEX type="DATE" id="DAT-123-002">06/01/1923</TIMEX> and <TIMEX type="DATE" id="DAT-123-003">12/15/1966</TIMEX> at studios in <LOCEX type="LOCATION" id="LOC-123-004">Los Angeles</LOCEX>, <LOCEX type="STATE" id="STA-123-005">CA</LOCEX> (<ENAMEX type="BRANCH" id="BRA-123-006">Sixth &amp; Central</ENAMEX>; <LOCEX type="LOCATION" id="LOC-123-007">Wilshire</LOCEX>-<LOCEX type="LOCATION" id="LOC-123-008">La Brea</LOCEX>; <ENAMEX type="ORGANIZATION" id="ORG-123-009">La Brea-Rosewood</ENAMEX>; Melrose-Fairfax) and theatres in <LOCEX type="LOCATION" id="LOC-123-010">Los Angeles</LOCEX>, CA.</S>
</TXT>
</DOC>
<ENTINFO ID="ACC-123-081" TYPE="ACCOUNT" NORM="222222222" REFID="ACC-123-081" ACCT-TYPE="CHK" MENTION="account: animation studio checking account 222222222" />
</NORMDOC>

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output method="xml" indent="yes" />

  <xsl:template match="/">
    <DOC>
      <xsl:apply-templates select="NORMDOC/DOC/*" />
      <xsl:apply-templates select="NORMDOC/DOC/TXT" mode="extra"/>
   </DOC>
  </xsl:template>

  <xsl:template match="*">
    <xsl:copy>
      <xsl:value-of select="current()"/>
     </xsl:copy>
  </xsl:template>

  <xsl:template match="TXT">
    <RAW_TXT>
      <xsl:value-of select="current()"/>
     </RAW_TXT>
  </xsl:template>

  <xsl:template match="TXT" mode="extra">
  <TXT>
    <xsl:for-each select="*">
      <xsl:element name="{local-name()}">
        <xsl:for-each select="*">
          <xsl:variable name="type" select="@type"/>
          <xsl:element name="{concat(name(), '_', $type)}">
          <xsl:value-of select="current()"/>
        </xsl:element>
        </xsl:for-each>
      </xsl:element>
    </xsl:for-each>
  </TXT>
  </xsl:template>
</xsl:stylesheet>

实际输出

<DOC>
   <DOCID>123</DOCID>
   <FI>
BA
456
</FI>
   <OIs>

BA

</OIs>
   <Subjects>

DISNEY/WALT
DISNEY
WALT


1234567890
9876543210

</Subjects>
   <RAW_TXT>
INTRODUCTION  this is being filed to report suspicious activity between customer's personal account and his animation business. The following suspect was identified: WALT DISNEY. The reportable amount is $123,456. The suspicious activity took place between 06/01/1923 and 12/15/1966 at studios in Los Angeles, CA (Sixth &amp; Central; Wilshire-La Brea; La Brea-Rosewood; Melrose-Fairfax) and theatres in Los Angeles, CA.
</RAW_TXT>
   <TXT>
      <S>
         <WH_/>
         <WH_/>
      </S>
      <S>
         <ENAMEX_PERSON>WALT DISNEY</ENAMEX_PERSON>
      </S>
      <S>
         <NUMEX_MONEY>$123,456</NUMEX_MONEY>
      </S>
      <S>
         <TIMEX_DATE>06/01/1923</TIMEX_DATE>
         <TIMEX_DATE>12/15/1966</TIMEX_DATE>
         <LOCEX_LOCATION>Los Angeles</LOCEX_LOCATION>
         <LOCEX_STATE>CA</LOCEX_STATE>
         <ENAMEX_BRANCH>Sixth &amp; Central</ENAMEX_BRANCH>
         <LOCEX_LOCATION>Wilshire</LOCEX_LOCATION>
         <LOCEX_LOCATION>La Brea</LOCEX_LOCATION>
         <ENAMEX_ORGANIZATION>La Brea-Rosewood</ENAMEX_ORGANIZATION>
         <LOCEX_LOCATION>Los Angeles</LOCEX_LOCATION>
      </S>
   </TXT>
</DOC>

预期输出

<DOC>
   <DOCID>123</DOCID>
   <FI>
<FINAME>BA</FINAME><FITIN>456</FITIN>
</FI>
   <OIs>
<OINAME>BA</OINAME>
</OIs>
   <Subjects>
<SubjectFullName>DISNEY/WALT</SubjectFullName>
<SubjectLastName>DISNEY</SubjectLastName>
<SubjectFirstName>WALT</SubjectFirstName>
<SubjectPhone_Work>1234567890</SubjectPhone_Work>
<SubjectPhone_Residence>9876543210</SubjectPhone_Residence>
</Subjects>
   <RAW_TXT>
INTRODUCTION  this is being filed to report suspicious activity between customer's personal account and his animation business. The following suspect was identified: WALT DISNEY. The reportable amount is $123,456. The suspicious activity took place between 06/01/1923 and 12/15/1966 at studios in Los Angeles, CA (Sixth &amp; Central; Wilshire-La Brea; La Brea-Rosewood; Melrose-Fairfax) and theatres in Los Angeles, CA.
</RAW_TXT>
   <TXT>
      <S>
         <WH_/>
         <WH_/>
      </S>
      <S>
         <ENAMEX_PERSON>WALT DISNEY</ENAMEX_PERSON>
      </S>
      <S>
         <NUMEX_MONEY>$123,456</NUMEX_MONEY>
      </S>
      <S>
         <TIMEX_DATE>06/01/1923</TIMEX_DATE>
         <TIMEX_DATE>12/15/1966</TIMEX_DATE>
         <LOCEX_LOCATION>Los Angeles</LOCEX_LOCATION>
         <LOCEX_STATE>CA</LOCEX_STATE>
         <ENAMEX_BRANCH>Sixth &amp; Central</ENAMEX_BRANCH>
         <LOCEX_LOCATION>Wilshire</LOCEX_LOCATION>
         <LOCEX_LOCATION>La Brea</LOCEX_LOCATION>
         <ENAMEX_ORGANIZATION>La Brea-Rosewood</ENAMEX_ORGANIZATION>
         <LOCEX_LOCATION>Los Angeles</LOCEX_LOCATION>
      </S>
   </TXT>
</DOC>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    AFAICT,以下样式表返回预期结果:

    XSLT 1.0

    <xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/NORMDOC">
        <xsl:apply-templates select="DOC"/>
    </xsl:template>
    
    <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="TXT">
        <RAW_TXT>
            <xsl:value-of select="."/>
        </RAW_TXT>
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="S">
        <xsl:copy>
            <xsl:apply-templates select="*" mode="extra"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="*" mode="extra">
        <xsl:element name="{name()}_{@type}">
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 这几乎是完美的,谢谢!但是,最后我得到了&lt;ENTINFO/&gt;,而&lt;WH_&gt; 则分散在各处。关于如何摆脱这些的任何建议?
    • @carousallie 我已经编辑了我的答案,所以排除了&lt;ENTINFO/&gt;。我看到的唯一&lt;WH_&gt; 节点也是出现在您的预期输出中的两个节点。
    【解决方案2】:

    覆盖身份规则是解决问题的最佳方法。这个样式表:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:template match="node()|@*" name="identity">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="NORMDOC">
        <xsl:apply-templates/>
      </xsl:template>
    
      <xsl:template match="TXT">
        <RAW_TXT>
          <xsl:value-of select="."/>
        </RAW_TXT>
        <xsl:call-template name="identity"/>
      </xsl:template>
    
      <xsl:template match="TXT/S/text()|ENTINFO"/>
    </xsl:stylesheet>
    

    输出:

    <DOC>
       <DOCID>123</DOCID>
       <FI fitype="B" xref="12345">
          <FIName>BA</FIName>
          <FITIN>456</FITIN>
       </FI>
       <OIs>
          <OI xref="54321">
             <OIName>BA</OIName>
          </OI>
       </OIs>
       <Subjects>
          <Subject stype="PER" xref="111111">
             <SubjectFullName type="L">DISNEY/WALT</SubjectFullName>
             <SubjectLastName type="L">DISNEY</SubjectLastName>
             <SubjectFirstName type="L">WALT</SubjectFirstName>
             <SubjectPhone type="Work">1234567890</SubjectPhone>
             <SubjectPhone type="Residence">9876543210</SubjectPhone>
          </Subject>
       </Subjects>
       <RAW_TXT>INTRODUCTION  this is being filed to report suspicious activity between customer's personal account and his animation business.The following suspect was identified: WALT DISNEY.The reportable amount is $123,456.The suspicious activity took place between 06/01/1923 and 12/15/1966 at studios in Los Angeles, CA (Sixth &amp; Central; Wilshire-La Brea; La Brea-Rosewood; Melrose-Fairfax) and theatres in Los Angeles, CA.</RAW_TXT>
       <TXT>
          <S sid="123-SENT-001">
             <WH/>
             <WH/>
          </S>
          <S sid="123-SENT-002">
             <ENAMEX type="PERSON" id="PER-123-000">WALT DISNEY</ENAMEX>
          </S>
          <S sid="123-SENT-003">
             <NUMEX type="MONEY" id="MON-123-001">$123,456</NUMEX>
          </S>
          <S sid="123-SENT-004">
             <TIMEX type="DATE" id="DAT-123-002">06/01/1923</TIMEX>
             <TIMEX type="DATE" id="DAT-123-003">12/15/1966</TIMEX>
             <LOCEX type="LOCATION" id="LOC-123-004">Los Angeles</LOCEX>
             <LOCEX type="STATE" id="STA-123-005">CA</LOCEX>
             <ENAMEX type="BRANCH" id="BRA-123-006">Sixth &amp; Central</ENAMEX>
             <LOCEX type="LOCATION" id="LOC-123-007">Wilshire</LOCEX>
             <LOCEX type="LOCATION" id="LOC-123-008">La Brea</LOCEX>
             <ENAMEX type="ORGANIZATION" id="ORG-123-009">La Brea-Rosewood</ENAMEX>
             <LOCEX type="LOCATION" id="LOC-123-010">Los Angeles</LOCEX>
          </S>
       </TXT>
    </DOC>
    

    注意:NORMDOC 元素使用“绕过规则”;使用 empty rule 剥离 S' 文本节点子节点和 ENTINFO 元素 和后代;使用命名模板能够覆盖 TXT 元素的标识规则,但不会失去重用它的机会。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 1970-01-01
      • 2023-03-21
      相关资源
      最近更新 更多