输出元素名称和属性通过XSLT

huangapple go评论50阅读模式
英文:

Output element names and attributes via XSLT

问题

<set>
   <attributeName>Field IDNUMBER1</attributeName>
   <attributeName>Field TITLE1</attributeName>
   <attributeName>Field LASTSUFFNAME1</attributeName>
   <attributeName>Field FIRSTMIDNAME1</attributeName>
   <attributeName>Field LASTSUFFNAME1</attributeName>
   <attributeName>Field FIRSTMIDNAME1</attributeName>
   <attributeName>Field MKEY1</attributeName>
   <attributeName>Field DATE21</attributeName>
</set>
英文:

I am working with an xml file and want to output the element names and attributes. I have it working to a point but have hit a brick wall with some nested elements. The issue I have is that my transform is not looking at any elements contained within the 'Subreport' section. It is these elements that I would like to include. Below you can find my xml data, my current transform, the output I have, and the output I would like to be creating. Any help would be very much appreciated. Many thanks in advance!

My xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<CrystalReport>
    <Details Level="2">
        <Section SectionNumber="0">
            <Field Name="IDNUMBER1">
                <Value>xxx</Value>
            </Field>
            <Field Name="TITLE1">
                <Value>aaa</Value>
            </Field>
            <Subreport Name="Subreport1">
                <ReportHeader> </ReportHeader>
                <Details Level="1">
                    <Section SectionNumber="0">
                        <Field Name="LASTSUFFNAME1">
                            <Value>bbb</Value>
                        </Field>
                        <Field Name="FIRSTMIDNAME1">
                            <Value>ccc</Value>
                        </Field>
                    </Section>
                </Details>
                <Details Level="1">
                    <Section SectionNumber="0">
                        <Field Name="LASTSUFFNAME1">
                            <Value>ddd</Value>
                        </Field>
                        <Field Name="FIRSTMIDNAME1">
                            <Value>eee</Value>
                        </Field>
                    </Section>
                </Details>
            </Subreport>
            <Field Name="MKEY1">
                <FormattedValue>111</FormattedValue>
                <Value>111</Value>
            </Field>
            <Field Name="DATE21">
                <Value>2023-07-13</Value>
            </Field>
        </Section>
    </Details>
</CrystalReport>

My xsl so far:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
  
    <xsl:template match="CrystalReport">
        <set>
            
            <xsl:apply-templates/>
            
        </set>
    </xsl:template>

    <xsl:template match="Details|Details/Section/Subreport/node()">
        <xsl:apply-templates select="Section/*"/>
        
    </xsl:template>
  
    <xsl:template match="Section/*">
        <xsl:for-each select=".">
        <attributeName>
            <xsl:value-of select="local-name()"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="@*"/>
          
        </attributeName>
        </xsl:for-each>
    </xsl:template> 
 
</xsl:stylesheet>

The output I am getting:

<set>
   <attributeName>Field IDNUMBER1</attributeName>
   <attributeName>Field TITLE1</attributeName>
   <attributeName>Subreport </attributeName>
   <attributeName>Field MKEY1</attributeName>
   <attributeName>Field DATE21</attributeName>
</set>

The output I would like:

<set>
   <attributeName>Field IDNUMBER1</attributeName>
   <attributeName>Field TITLE1</attributeName>
   <attributeName>Field LASTSUFFNAME1</attributeName>
   <attributeName>Field FIRSTMIDNAME1</attributeName>
   <attributeName>Field LASTSUFFNAME1</attributeName>
   <attributeName>Field FIRSTMIDNAME1</attributeName>
   <attributeName>Field MKEY1</attributeName>
   <attributeName>Field DATE21</attributeName>
</set>

答案1

得分: 0

以下是翻译好的内容:

尝试这个:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>
  
  <xsl:template match="CrystalReport">
    <set>
      <xsl:apply-templates/>
    </set>
  </xsl:template>
  
  <!-- 匹配所有节点模板:只需应用模板 -->
  <xsl:template match="node()">
    <xsl:apply-templates select="node()"/>
  </xsl:template>
  
  <xsl:template match="Field">
    <attributeName>
      <xsl:value-of select="local-name()"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="@*"/>
    </attributeName>
  </xsl:template> 
  
</xsl:stylesheet>

或者更简短的版本:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>
  
  <xsl:template match="CrystalReport">
    <set>
      <xsl:apply-templates select=".//Field"/>
    </set>
  </xsl:template>
  
  <xsl:template match="Field">
    <attributeName>
      <xsl:value-of select="local-name()"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="@*"/>
    </attributeName>
  </xsl:template> 
  
</xsl:stylesheet>

两者都会产生以下结果:

<set>
   <attributeName>Field IDNUMBER1</attributeName>
   <attributeName>Field TITLE1</attributeName>
   <attributeName>Field LASTSUFFNAME1</attributeName>
   <attributeName>Field FIRSTMIDNAME1</attributeName>
   <attributeName>Field LASTSUFFNAME1</attributeName>
   <attributeName>Field FIRSTMIDNAME1</attributeName>
   <attributeName>Field MKEY1</attributeName>
   <attributeName>Field DATE21</attributeName>
</set>
英文:

Try this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xsl:stylesheet version=&quot;2.0&quot;
  xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
  &lt;xsl:output omit-xml-declaration=&quot;yes&quot; indent=&quot;yes&quot;/&gt;
  &lt;xsl:strip-space elements=&quot;*&quot;/&gt;
  
  &lt;xsl:template match=&quot;CrystalReport&quot;&gt;
    &lt;set&gt;
      &lt;xsl:apply-templates/&gt;
    &lt;/set&gt;
  &lt;/xsl:template&gt;
  
  &lt;!-- Match all node template: just apply-templates --&gt;
  &lt;xsl:template match=&quot;node()&quot;&gt;
    &lt;xsl:apply-templates select=&quot;node()&quot;/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match=&quot;Field&quot;&gt;
    &lt;attributeName&gt;
      &lt;xsl:value-of select=&quot;local-name()&quot;/&gt;
      &lt;xsl:text&gt; &lt;/xsl:text&gt;
      &lt;xsl:value-of select=&quot;@*&quot;/&gt;
    &lt;/attributeName&gt;
  &lt;/xsl:template&gt; 
  
&lt;/xsl:stylesheet&gt;

Or even shorter:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xsl:stylesheet version=&quot;2.0&quot;
  xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
  &lt;xsl:output omit-xml-declaration=&quot;yes&quot; indent=&quot;yes&quot;/&gt;
  &lt;xsl:strip-space elements=&quot;*&quot;/&gt;
  
  &lt;xsl:template match=&quot;CrystalReport&quot;&gt;
    &lt;set&gt;
      &lt;xsl:apply-templates select=&quot;.//Field&quot;/&gt;
    &lt;/set&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match=&quot;Field&quot;&gt;
    &lt;attributeName&gt;
      &lt;xsl:value-of select=&quot;local-name()&quot;/&gt;
      &lt;xsl:text&gt; &lt;/xsl:text&gt;
      &lt;xsl:value-of select=&quot;@*&quot;/&gt;
    &lt;/attributeName&gt;
  &lt;/xsl:template&gt; 
  
&lt;/xsl:stylesheet&gt;

Both will result in:

&lt;set&gt;
   &lt;attributeName&gt;Field IDNUMBER1&lt;/attributeName&gt;
   &lt;attributeName&gt;Field TITLE1&lt;/attributeName&gt;
   &lt;attributeName&gt;Field LASTSUFFNAME1&lt;/attributeName&gt;
   &lt;attributeName&gt;Field FIRSTMIDNAME1&lt;/attributeName&gt;
   &lt;attributeName&gt;Field LASTSUFFNAME1&lt;/attributeName&gt;
   &lt;attributeName&gt;Field FIRSTMIDNAME1&lt;/attributeName&gt;
   &lt;attributeName&gt;Field MKEY1&lt;/attributeName&gt;
   &lt;attributeName&gt;Field DATE21&lt;/attributeName&gt;
&lt;/set&gt;

huangapple
  • 本文由 发表于 2023年7月13日 22:12:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定