1

Here my XML doc :

<?xml version="1.0" ?> 
<ArrayOfPrimitiveParameter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">   
    <PrimitiveParameter>
        <Name>field</Name>
        <Value xsi:type="xsd:string">TNA</Value>
    </PrimitiveParameter>
    <PrimitiveParameter>
        <Name>date</Name>
        <Value xsi:type="xsd:dateTime">2011-12-14T00:00:00</Value>
    </PrimitiveParameter>
</ArrayOfPrimitiveParameter>

And I want to transform its structure in :

TNA 2011-12-14T00:00:00

for the moment, I wrote this :

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/TR/REC-html40"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                >
  <xsl:output method="xml" encoding="utf-8"/>
  <xsl:template match="/">
    <Parameters>
      <xsl:apply-templates/>
    </Parameters>
  </xsl:template>


  <xsl:template match="ArrayOfPrimitiveParameters">

      <!-- String -->
      <xsl:if test="PrimitiveParameter/@xsi:type = 'xsd:string'">
        <StringParameter>
          <xsl:attribute name="name">
            <xsl:value-of select='PrimitiveParameter/name' />">
          </xsl:attribute>
          <xsl:value-of select='PrimitiveParameter/value'/>
        </StringParameter>
      </xsl:if>

      <!-- DateTime -->
      <xsl:if test="PrimitiveParameter/@xsi:type = 'xsd:dateTime'">
        <DateParameter>
          <xsl:attribute name="name">
            <xsl:value-of select='PrimitiveParameter/name' />">
          </xsl:attribute>
          <xsl:value-of select='PrimitiveParameter/value'/>
        </DateParameter>
      </xsl:if>

    </xsl:template>
</xsl:stylesheet>

but the output of my XML is that :

<?xml version="1.0" encoding="utf-8"?><Parameters xmlns="http://www.w3.org/TR/REC-html40" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">   

        field
        TNA


        date
        2011-12-14T00:00:00

</Parameters>

Thank you for your help,

Florian

4

1 回答 1

1

尝试使用以下 XSL。

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/REC-html40"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            >
<xsl:output method="xml" encoding="utf-8"/>
<xsl:template match="/">
  <Parameters>
    <xsl:apply-templates/>
  </Parameters>
</xsl:template>

<xsl:template match="PrimitiveParameter">

    <!-- String -->
    <xsl:if test="Value/@xsi:type='xsd:string'">
      <xsl:element name="StringParameter">
        <xsl:attribute name="name">
          <xsl:value-of select="Name" />
        </xsl:attribute>
        <xsl:value-of select="Value" />
      </xsl:element>
    </xsl:if>

    <!-- DateTime -->
    <xsl:if test="Value/@xsi:type='xsd:dateTime'">
      <xsl:element name="DateParameter">
        <xsl:attribute name="name">
          <xsl:value-of select="Name" />
        </xsl:attribute>
        <xsl:value-of select="Value" />
      </xsl:element>
    </xsl:if>

  </xsl:template>
</xsl:stylesheet>
于 2013-10-08T08:47:49.277 回答