2

我想传输xml基于xsd使用的文件。xsl但不能像 xsd 上提到的那样正确传输。这是我的xml

<abc:abcTransactionTypeXml xmlns:abc="abcTransactionType.schema.xml.google.com">
<abc:id>4</abc:id>    
<abc:abcTransactionTypeCategoryId>1</abc:abcTransactionTypeCategoryId>
<abc:description>Post ofccice</abc:description>
<abc:type>POST</abc:type>

</abc:abcTransactionTypeXml>

这是 。xsd

 <xs:element name="abcTransactionType" type="abcTransactionType" />
    <xs:complexType name="abcTransactionType">
        <xs:sequence>
            few elements are here
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="abcTransactionTypeCategory" >
        <xs:sequence>
            <xs:element name="id" type="xs:short" minOccurs="1" maxOccurs="1"/>
            <xs:element name="description" type="xs:string" minOccurs="0" maxOccurs="1"/>
            <xs:element name="type" type="xs:string" minOccurs="0" maxOccurs="1"/>


        </xs:sequence>
    </xs:complexType>

这是我的 。xsl

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
    xmlns:abc="abcTransactionType.schema.xml.google.com"
    exclude-result-prefixes="abc"
    xmlns="abcTransactionType.schema.abc.xml.google.com"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    <xsl:template match="/">
        <abcTransactionType>
            <xsl:apply-templates select="abc:abcTransactionTypeXml" />
        </abcTransactionType>       
    </xsl:template>

    <xsl:template match="abc:abcTransactionTypeXml">

        <xsl:apply-templates select="abct:abcTransactionTypeCategoryId" />
         <xsl:apply-templates select="abc:description" />
        <xsl:apply-templates select="abc:type" />
    </xsl:template>

    <xsl:template match="abc:id">
        <id>
            <xsl:value-of select="." />
        </id>
    </xsl:template>


     <xsl:template match="abc:abcTransactionTypeCategoryId">
        <abcTransactionTypeCategory>
            <id>
            <xsl:value-of select="." />
            </id>
        </abcTransactionTypeCategory>
    </xsl:template>

     <xsl:template match="abc:description">
        <abcTransactionTypeCategory>
            <description>
            <xsl:value-of select="." />
            </description>
        </abcTransactionTypeCategory>
    </xsl:template>

    <xsl:template match="abc:type">
        <abcTransactionTypeCategory>
            <type>
            <xsl:value-of select="." />
            </type>
        </abcTransactionTypeCategory>
    </xsl:template>     


</xsl:stylesheet>

这是我的输出

<abcTransactionType xmlns="abcTransactionType.schema.abc.xml.google.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <id>4</id>

    <abcTransactionTypeCategory>
    <id>1</id>
    </abcTransactionTypeCategory>
    <abcTransactionTypeCategory>
    <description>POST OFFICE</description>
    </abcTransactionTypeCategory>
    <abcTransactionTypeCategory>
    <type>POST</type>
    </abcTransactionTypeCategory>
    <
    </abcTransactionType>

这是我的预期输出

<abcTransactionType xmlns="abcTransactionType.schema.abc.xml.google.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<id>4</id>
<abcTransactionTypeCategory>
<id>1</id>
<description>Post Office</description>
<type>POST</type>
</abcTransactionTypeCategory>
</abcTransactionType>

我需要id、描述、输入单个对象,即abcTransactionTypeCategory 我尝试了很多来获得预期的输出,但没有运气,任何人都可以帮助我完成这项工作。万分感谢。

4

1 回答 1

1

对于第一个解决方案,只需进行很小的更改。

将输出移动<abcTransactionTypeCategory>abc:abcTransactionTypeXml模板中。

<xsl:template match="abc:abcTransactionTypeXml">
    <xsl:apply-templates select="abc:id" />
    <abcTransactionTypeCategory>
        <xsl:apply-templates select="abc:abcTransactionTypeCategoryId" />
        <xsl:apply-templates select="abc:description" />
        <xsl:apply-templates select="abc:type" />
    </abcTransactionTypeCategory>
</xsl:template>

然后<abcTransactionTypeCategory>从其他模板中删除。

第二个改进:添加一个删除命名空间前缀的模板abc

<xsl:template match="abc:*" >
    <xsl:element name="{local-name()}">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

现在您可以删除所有仅删除命名空间前缀的模板。
因此试试这个:

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="2.0"
    xmlns:ssat="abcTransactionType.schema.xml.google.com"
    exclude-result-prefixes="abc"
    xmlns="abcTransactionType.schema.abc.xml.google.com"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:output method="xml" indent="yes"/>


<xsl:template match="/">
    <abcTransactionType>
        <xsl:apply-templates select="abc:abcTransactionTypeXml" />
    </abcTransactionType>
</xsl:template>

<xsl:template match="abc:abcTransactionTypeXml">
    <xsl:apply-templates select="abc:id" />

    <abcTransactionTypeCategory>
        <xsl:apply-templates select="abc:abcTransactionTypeCategoryId" />
        <xsl:apply-templates select="abc:description" />
        <xsl:apply-templates select="abc:type" />
    </abcTransactionTypeCategory>
</xsl:template>

<xsl:template match="abc:id">
    <id>
        <xsl:value-of select="." />
    </id>
</xsl:template>


<xsl:template match="abc:abcTransactionTypeCategoryId">
        <id>
            <xsl:value-of select="." />
        </id>
</xsl:template>

<xsl:template match="abc:*" >
    <xsl:element name="{local-name()}">
        <xsl:value-of select="."/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

这将生成以下输出:

<?xml version="1.0"?>
<abcTransactionType xmlns="abcTransactionType.schema.abc.xml.google.com" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <id>4</id>

  <abcTransactionTypeCategory>
    <id>1</id>
    <description>POST OFFICE</description>
    <type>POST</type>

  </abcTransactionTypeCategory>
</abcTransactionType>
于 2013-07-10T17:18:27.227 回答