模板匹配打印匹配标签中的所有内容 [英] Template match prints everything from matching tag

查看:37
本文介绍了模板匹配打印匹配标签中的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么它会在我打印时打印标签中的所有内容?我只想进入那个节点,所以我不必总是输入路径?这是示例 XML:

Why does it print everything from tag when I do ? I just want to get into that node so I don't have to type always the path? Here is example XML:

           <?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="blablabla" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <CstmrCdtTrfInitn>
      <GrpHdr>
         <MsgId>35006</MsgId>
         <CreDtTm>2017-04-13T08:30:09</CreDtTm>
         <NbOfTxs>3</NbOfTxs>
         <CtrlSum>22000.00</CtrlSum>
         <InitgPty>
            <Nm>XXXXX</Nm>
            <Id>
               <OrgId>
                  <Othr>
                     <Id>0000010681</Id>
                  </Othr>
               </OrgId>
            </Id>
         </InitgPty>
      </GrpHdr>
      <PmtInf>
         <PmtInfId>35006_26011</PmtInfId>
         <PmtMtd>TRF</PmtMtd>
         <NbOfTxs>3</NbOfTxs>
         <CtrlSum>22000.00</CtrlSum>
         <PmtTpInf />
         <ReqdExctnDt>2017-04-13</ReqdExctnDt>
         <Dbtr>
            <Nm>WWWWWWW</Nm>
            <PstlAdr>
               <StrtNm>AAAAAA</StrtNm>
               <PstCd>BBBBBB</PstCd>
               <TwnNm>CCCCCC</TwnNm>
               <Ctry>PL</Ctry>
            </PstlAdr>
            <Id>
               <OrgId>
                  <Othr>
                     <Id>0000010681</Id>
                  </Othr>
               </OrgId>
            </Id>
         </Dbtr>
      </PmtInf>
   </CstmrCdtTrfInitn>
</Document>

这是我想收到的:

1.  XXXXX
2.  AAAAAA
3.  BBBBBB
4.  CCCCCC

我得到:

350062017-04-13T08:30:09322000.00XXXXX0000010681
  1.
  WWWWWWW
  2.
  AAAAAA
  3.
  BBBBBB
  4.
  CCCCCC

使用此 XLST:

    <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:doc="blablabla" version="1.0">
   <xsl:output method="text" encoding="utf-8" />
   <xsl:strip-space elements="*" />
   <xsl:template match="doc:PmtInf">
      1.
      <xsl:value-of select="doc:Dbtr/doc:Nm" />
      2.
      <xsl:value-of select="doc:Dbtr/doc:PstlAdr/doc:StrtNm" />
      3.
      <xsl:value-of select="doc:Dbtr/doc:PstlAdr/doc:PstCd" />
      4.
      <xsl:value-of select="doc:Dbtr/doc:PstlAdr/doc:TwnNm" />
   </xsl:template>
</xsl:stylesheet>

推荐答案

这是因为 内置模板规则,当处理器在您的 XSLT 中找不到匹配的模板时使用这些规则

This is because of built-in template rules which are used when the processor cannot find a matching template in your XSLT

XML 的处理开始于 XSLT 寻找与文档节点匹配的模板(由 / 表示),并且因为您的 XSLT 中没有与内置模板匹配的模板适用

Processing of the XML starts by XSLT looking for a template that matches the document node (represented by a /) and because you don't have a template matching in your XSLT the built-in template applies

<xsl:template match="*|/">
  <xsl:apply-templates/>
</xsl:template>

这将简单地传递节点,并寻找与子节点匹配的模板.

This will simply pass over the node, and look for templates matching child nodes.

当它到达 CstmrCdtTrfInitn 时,您也没有匹配的模板,因此内置模板仍然适用于选择其子项.您确实有一个匹配 pmtInf 但不匹配 GrpHdr 的模板.对于 GrpHdr 元素,最终内置模板将到达文本节点,由这些匹配

When it gets to CstmrCdtTrfInitn, you also don't have a matching template, so the built-in template still applies to select its children. You do have a template matching pmtInf but not GrpHdr. For the GrpHdr element, eventually the built-in template will reach text nodes, which gets matched by these

<xsl:template match="text()|@*">
  <xsl:value-of select="."/>
</xsl:template>

换句话说,内置模板会输出它找到的任何文本节点,这就是您获得额外文本的原因.

In other words, the built-in templates outputs any text nodes it finds, which is why you get the extra text.

您可以做的是添加一个与 GrpHdr 匹配的模板,并告诉 XSLT 不再继续

What you could do, is add a template that matches GrpHdr and tells XSLT to go no further

<xsl:template match="doc:GrpHdr" />

或者你可以有一个匹配 doc:CstmrCdtTrfInitn 的模板,然后只选择你想要的子节点.

Or you could have a template that matches doc:CstmrCdtTrfInitn that then selects only the child node you want.

<xsl:template match="doc:CstmrCdtTrfInitn">
  <xsl:apply-templates select="doc:PmtInf" />
</xsl:template>

如果您根本不想依赖内置模板,或者如果您的 XML 中有其他不想要的元素正在发挥作用,您也可以尝试添加此模板,以匹配文档节点,然后直接跳转到PmtInf节点.

If you did not want to rely on built-in templates at all, or if you have other elements in your XML that are coming into play where you don't want them, you could also try adding this template instead, to match the document node, and then jump straigt to the PmtInf node.

 <xsl:template match="/">
    <xsl:apply-templates select="doc:Document/doc:CstmrCdtTrfInitn/doc:PmtInf" />
 </xsl:template>

举个例子,这应该给你你需要的结果

As an example, this should give you the result you need

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:doc="blablabla" version="1.0">
   <xsl:output method="text" encoding="utf-8" />
   <xsl:strip-space elements="*" />

   <xsl:template match="doc:GrpHdr" />

   <xsl:template match="doc:PmtInf">
      1. <xsl:value-of select="doc:Dbtr/doc:Nm" />
      2. <xsl:value-of select="doc:Dbtr/doc:PstlAdr/doc:StrtNm" />
      3. <xsl:value-of select="doc:Dbtr/doc:PstlAdr/doc:PstCd" />
      4. <xsl:value-of select="doc:Dbtr/doc:PstlAdr/doc:TwnNm" />
   </xsl:template>
</xsl:stylesheet>

这篇关于模板匹配打印匹配标签中的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆