开发者

How to select element name, not value, using XSL/XSLT?

开发者 https://www.devze.com 2023-04-08 22:19 出处:网络
We are using XSL to convert a XML file into a pipe-delimited format. <?xml version=\"1.0\" encoding=\"UTF-8\"?>

We are using XSL to convert a XML file into a pipe-delimited format.

<?xml version="1.0" encoding="UTF-8"?>
<ns:tradedata xmlns:ns="http://schemas.com/enterprise/util/extractservice/v1">
    <tradedata_item>
        <ORDER_ID>113632428</ORDER_ID>
        <CUSIP>31393FHA7</CUSIP>
        <TICKER>FHR</TICKER>
        <SEC_NAME>FHR 2527 SG</SEC_NAME>
        <ORDER_QTY>169249.6824</ORDER_QTY>
    </tradedata_item>
    <tradedata_item>
        <ORDER_ID>113632434</ORDER_ID>
        <CUSIP>31393G2C7</CUSIP>
        <TICKER>FHR</TICKER>
        <SEC_NAME>FHR 2531 ST</SEC_NAME>
        <ORDER_QTY>214673.0105</ORDER_QTY>
    </tradedata_item>
    <tradedata_item>
        <ORDER_ID>113632431</ORDER_ID>
        <CUSIP>527069AH1</CUSIP>
        <TICKER>LESL</TICKER>
        <SEC_NAME>ZZZ_LESLIE S POOLMART INC</SEC_NAME>
        <ORDER_QTY>365000.0000</ORDER_QTY>
    </tradedata_item>
</ns:tradedata>

We need the first line in the output to be the column headers, and everything else would be data, like this...

ORDER_ID|CUSIP|TICKER|SEC_NAME|ORDER_QTY
1136324289|31393FHA7|FHR|FHR 2527 SG|169249.6824
1136324304|31393G2C7|FHR|FHR 2531 ST|214673.0105

We've got the XSL working to get the data, but we can't get the header to output correctly. We just select the first tradedata_item element, then iterate the element name and separate them using | characters. Here is the full XSL...

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl"
    version="1.0" xmlns="http://schemas.com/enterprise/util/extractservice/v1"
    xmlns:o="http://schemas.com/enterprise/util/extractservice/v1" > 

    <!--  xsl:strip-space elements="*"/-->
    <xsl:output method="text" indent="no"/>

     <xsl:template match="/tradedata/tradedata_item[1]">
    <xsl:for-each select="*">
      <xsl:value-of select="local-name()"/>|
    </xsl:for-each>
    <xsl:text>&#10;</xsl:text>  
  </xsl:template>


    <xsl:template match="/">
    <xsl:for-each select="tradedata/tradedata_item">
    <xsl:value-of select="ORDER_ID"/>|<xsl:value-of select="CUSIP"/>|<xsl:value-of select="TICKER"/>|<xsl:value-of select="SEC_NAME"/>|<xsl:value-of select="ORDER_QTY"/>
    <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

The output we're seeing is just data, no header...

113632428|31393FHA7|FHR|FHR 2527 SG|169249.6824
113632430|31393G2C7|FHR|FHR 2531 ST|214673.0105
113632431|527069AH1|LESL|ZZZ_LESLIE S POOLMART INC|365000.0000
113632434|38470RAD3|GRAHAM|ZZZ_GRAHAM PACKAGING CO|595000.0000

Please disregard any name开发者_运维技巧space inconsistencies; I had to obfuscate the xml and xsl for legal reasons.


Try this :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" indent="no"/>

<xsl:template match="/">
<xsl:for-each select="tradedata/tradedata_item[1]/*">
  <xsl:value-of select="concat(name(), '|')"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Output :

ORDER_ID|CUSIP|TICKER|SEC_NAME|ORDER_QTY|

It seems pretty simple to me. Maybe your error lies elsewhere.


I tried your code.. I only changed the first template to match:

 <xsl:template match="//tradedata_item[1]">

and it worked for me, i.e. got the header names.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl ofi"
    version="1.0" xmlns="http://schemas.com/enterprise/util/extractservice/v1"
    xmlns:ofi="http://schemas.oppen.com/enterprise/util/extractservice/v1">
 <xsl:output method="text" indent="no"/>
  <xsl:template match="tradedata_item[position()='1']">
    <xsl:for-each select="self::*">
      <xsl:for-each select="child::*[position()!='5']">
        <xsl:value-of select="local-name(self::*)"/>|
      </xsl:for-each>
      <xsl:value-of select="local-name(child::*[position()='5'])"/>
      <xsl:text>
      </xsl:text>
      <xsl:value-of select="ORDER_ID"/>|<xsl:value-of select="CUSIP"/>|<xsl:value-of select="TICKER"/>|<xsl:value-of select="SEC_NAME"/>|<xsl:value-of select="ORDER_QTY"/>
      <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
  </xsl:template>
  <xsl:template match="tradedata_item[position()&gt;1]">
    <xsl:for-each select="self::*">
      <xsl:value-of select="ORDER_ID"/>|<xsl:value-of select="CUSIP"/>|<xsl:value-of select="TICKER"/>|<xsl:value-of select="SEC_NAME"/>|<xsl:value-of select="ORDER_QTY"/>
      <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号