简体   繁体   English

xslt跳过行集中的架构

[英]xslt skip schema in rowset

I have the following questions or problems with my xsl below. 我的xsl出现以下问题或疑问。 I've included my current output, the xml and the xsl. 我已经包含了当前的输出,xml和xsl。 I'm having issues trying to remove the 2nd line containing the double quotes. 我在尝试删除包含双引号的第二行时遇到问题。 I'm assuming it is coming from the schema node. 我假设它来自架构节点。

1) the closest version my xsl puts an invalid line between the headings and the data as show in the sample below. 1)我的xsl最接近的版本在标题和数据之间放置无效行,如下面的示例所示。
2) I tried inserting this template "" before the 2nd template to remove the blank line caused by the schema node but it had no effect 2)我尝试在第二个模板之前插入此模板“”以删除由架构节点引起的空行,但它没有任何效果
3) I tried changing the 2nd template to look like this 1]/child:: "> but it messes up the formatting of my data. I don't quite understand why because >0 works fine. 3)我尝试将第二个模板更改为如下所示:1] / child :: “>,但它弄乱了我的数据格式,我不太明白为什么会这样,因为> 0可以正常工作。
4 I also tried changeing the 2nd template with variations of match="Row/child:: but this also messes up the format of the data. Any help is greatly appreciated. 4我还尝试过使用match =“ Row / child ::的变体来更改第二个模板,但这也会弄乱数据的格式,非常感谢您的帮助。

Current Results: 当前结果:

"Column0", "Column1", "Column2", "Column3"
""
"0",    "2010",    "336227.46",    "1.43075514893617"
"0",    "2011",    "782819.36",    "3.40356243478261"
"0",    "2012",    "783044.89",    "3.33210591489362"

Source XML: 源XML:

<rowset xmlns="urn:schemas-microsoft-com:xml-analysis:rowset" >
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:saw-sql="urn:saw-sql" targetNamespace="urn:schemas-microsoft-com:xml-analysis:rowset">
        <xsd:complexType name="Row">
            <xsd:sequence>
                <xsd:element name="Column0" type="xsd:int" minOccurs="0" maxOccurs="1" saw-sql:type="integer" saw-sql:displayFormula="s_0" />
                <xsd:element name="Column1" type="xsd:string" minOccurs="0" maxOccurs="1" saw-sql:type="varchar" saw-sql:displayFormula="s_1" />
                <xsd:element name="Column2" type="xsd:double" minOccurs="0" maxOccurs="1" saw-sql:type="double" saw-sql:displayFormula="s_2" />
                <xsd:element name="Column3" type="xsd:double" minOccurs="0" maxOccurs="1" saw-sql:type="double" saw-sql:displayFormula="s_3" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:schema>
    <Row>
        <Column0>0</Column0>
        <Column1>2010</Column1>
        <Column2>336227.46</Column2>
        <Column3>1.43075514893617</Column3>
    </Row>
    <Row>
        <Column0>0</Column0>
        <Column1>2011</Column1>
        <Column2>782819.36</Column2>
        <Column3>3.40356243478261</Column3>
    </Row>
    <Row>
        <Column0>0</Column0>
        <Column1>2012</Column1>
        <Column2>783044.89</Column2>
        <Column3>3.33210591489362</Column3>
    </Row>
</rowset>

Code Version - Closest 代码版本-最近

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
    xmlns:err="http://www.w3.org/2005/xqt-errors"
    exclude-result-prefixes="xs xdt err fn">

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

<xsl:strip-space elements="*" />

<xsl:template match="/*">
<xsl:for-each select="*[2]/*">
<xsl:text>"</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>"</xsl:text>
<xsl:if test="position() != last()">, </xsl:if>
<xsl:if test="position() = last()">
<xsl:text>&#xD;&#xA;</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:template>


<xsl:template match="/*/child::*">
<xsl:for-each select="child::*">
<xsl:if test="position() != last()">"<xsl:value-of select="normalize-space(.)"/>",    </xsl:if>
<xsl:if test="position()  = last()">"<xsl:value-of select="normalize-space(.)"/>"<xsl:text>&#xD;&#xA;</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

The extra line is caused by your applying templates to all children of the root element. 多余的行是由于您将模板应用于根元素的所有子元素而引起的。 This includes the xsd:schema node. 这包括xsd:schema节点。

You can prevent this by changing: 您可以通过以下方法防止这种情况:

<xsl:apply-templates/>

in line #23 to: 在第23行中:

<xsl:apply-templates select="*[position() gt 1]"/>

PS I would suggest you use explicit names to address the source XML nodes, instead of cryptic abbreviations like *[2]/* . PS:我建议您使用显式名称来寻址源XML节点,而不要使用*[2]/*类的缩写。 That will make your code more readable and - as a result - easier to manage. 这将使您的代码更具可读性,因此,更易于管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM