【问题标题】:Getting child from a namespaced node in XSL从 XSL 中的命名空间节点获取子节点
【发布时间】:2018-08-08 21:07:41
【问题描述】:

我需要获取 c:batch 节点的每个子节点。

<?xml version="1.0" encoding="UTF-8"?>
<c:batch xmlns:a="http://www.test.com/data/6/archive"
         xmlns:i="http://www.test.com/data/6/archive/import"
         xmlns="http://www.test.com/dos/asap"
         xmlns:n1="http://www.test.com/dos"
         xmlns:c="http://www.test.com/data/6/capture">
   <s-20 file="00000001.pdf" checked="true">
      <code>X12345</code>
      <type>data</type>
   </s-20>
   <s-20 file="00000002.pdf" checked="false">
      <code>X67890</code>
      <type>data</type>
   </s-20>
   <s-20 file="00000003.pdf" checked="true">
      <code>X87687</code>
      <type>data</type>
   </s-20>
</c:batch>

样式表:

<xsl:stylesheet version="1.0" 
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:a="http://www.test.com/data/6/archive"
         xmlns:i="http://www.test.com/data/6/archive/import"
         xmlns="http://www.test.com/dos/asap"
         xmlns:n1="http://www.test.com/dos"
         xmlns:c="http://www.test.com/data/6/capture">
         
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <xsl:element name="Documents">
      <xsl:value-of select="count(s-20)"/>
      <xsl:apply-templates />
    </xsl:element>
  </xsl:template>
  
  <xsl:template match="s-20">
    <xsl:value-of select="code"/>
  </xsl:template>
</xsl:stylesheet>

【问题讨论】:

    标签: xml xslt namespaces


    【解决方案1】:

    您的&lt;s-20&gt; 在命名空间中:

    xmlns="http://www.test.com/dos/asap"
    

    要匹配它们,请在 XSLT 中使用显式命名空间,例如添加行

    xmlns:t="http://www.test.com/dos/asap"
    

    添加到 XSLT 中的 &lt;xsl:stylesheet&gt; 根元素,并在匹配规则前加上 t:。在您的s-20 模板中也使用xsl:copy-of 而不是xsl:value-of。然后将所有s-20 元素复制到输出。

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" 
             xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
             xmlns:a="http://www.test.com/data/6/archive"
             xmlns:i="http://www.test.com/data/6/archive/import"
             xmlns:t="http://www.test.com/dos/asap"
             xmlns:n1="http://www.test.com/dos"
             xmlns:c="http://www.test.com/data/6/capture">
    
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="/c:batch">
        <xsl:element name="Documents">
          <xsl:value-of select="count(t:s-20)"/>
          <xsl:apply-templates />
        </xsl:element>
      </xsl:template>
    
      <xsl:template match="t:s-20">
        <xsl:copy-of select="."/>
      </xsl:template>
    </xsl:stylesheet>
    

    输出:

    <?xml version="1.0"?>
    <Documents>3
      <s-20 xmlns:a="http://www.test.com/data/6/archive" xmlns:i="http://www.test.com/data/6/archive/import" xmlns="http://www.test.com/dos/asap" xmlns:n1="http://www.test.com/dos" xmlns:c="http://www.test.com/data/6/capture" file="00000001.pdf" checked="true">
            <code>X12345</code>
            <type>data</type>
        </s-20>
        <s-20 xmlns:a="http://www.test.com/data/6/archive" xmlns:i="http://www.test.com/data/6/archive/import" xmlns="http://www.test.com/dos/asap" xmlns:n1="http://www.test.com/dos" xmlns:c="http://www.test.com/data/6/capture" file="00000002.pdf" checked="false">
            <code>X67890</code>
            <type>data</type>
        </s-20>
        <s-20 xmlns:a="http://www.test.com/data/6/archive" xmlns:i="http://www.test.com/data/6/archive/import" xmlns="http://www.test.com/dos/asap" xmlns:n1="http://www.test.com/dos" xmlns:c="http://www.test.com/data/6/capture" file="00000003.pdf" checked="true">
            <code>X87687</code>
            <type>data</type>
        </s-20>
    </Documents>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-26
      • 2023-03-14
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多