1

次のXSLTを使用してXMLをXMLに変換しています。必要な要素のソースXMLを検証する必要があります。必要なノードの兄弟ノードの値が欠落している場合は、新しいノードを作成します。これがXSLTです

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <Data Schema="XML A">
            <xsl:apply-templates/>
        </Data>
    </xsl:template>
    <xsl:template match="Attribute[not(Type=following::Type)]">
        <Attributes type="{Type}">
            <xsl:apply-templates 
                 select="../Attribute[Type=current()/Type]" mode="out"/>
        </Attributes>
    </xsl:template>
    <xsl:template match="Attribute" mode="out">
        <Attr id="{id}" name="{Name}" value="{Value}"/>
    </xsl:template>
    <xsl:template match="Attribute"/>
</xsl:stylesheet>

これがXMLです

<?xml version="1.0" encoding="windows-1252"?>
<XML>
    <Attributes>
        <Attribute>
            <id>331</id>
            <Name>Enviornment</Name>
            <Type>common</Type>
            <Value>Development</Value>
        </Attribute>
        <Attribute>
            <id>79</id>
            <Name>Retail</Name>
            <Type>common</Type>
            <Value></Value>
        </Attribute>
        <Attribute>
            <id>402</id>
            <Name>Gender</Name>
            <Type>category</Type>
            <Value>Men</Value>
        </Attribute>
    </Attributes>
</XML>

また、必要な要素が欠落している場合は、次のXMLを作成する必要があります。複数の必須要素があります。

<?xml version="1.0" encoding="utf-8"?>
<Data Schema="XML A">
  <Attributes type="common">
    <Attr id="331" name="Enviornment" value="Development" />
    <Attr id="79" name="Retail" value="" />
  </Attributes>
  <Attributes type="category">
    <Attr id="402" name="Gender" value="Men" />
  </Attributes>
  <errorCodes>
    <errorCode>"value for Retail is missing."</errorCode>
  </errorCodes>
</Data>

そして、それが次のXSLTを使用して実行できる場合、それは大きなプラスになります。前もって感謝します。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="type" match="Attribute" use="Type"/>
    <xsl:template match="/">
        <Data Schema="XML A">
            <xsl:apply-templates select="XML/Attributes/Attribute">
                 <xsl:sort select="Type" order="descending"/>
            </xsl:apply-templates>
        </Data>
    </xsl:template>
    <xsl:template 
            match="Attribute[generate-id()=generate-id(key('type', Type)[1])]">
        <Attributes type="{Type}">
            <xsl:apply-templates 
                    select="../Attribute[Type=current()/Type]" mode="out"/>
        </Attributes>
    </xsl:template>
    <xsl:template match="Attribute" mode="out">
         <Attr id="{id}" name="{Name}" value="{Value}"/>
    </xsl:template>
    <xsl:template match="Attribute"/>
</xsl:stylesheet>
4

1 に答える 1

6

次のスタイルシート:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="type" match="Attribute" use="Type"/>
    <xsl:template match="/">
        <Data Schema="XML A">
            <xsl:apply-templates select="XML/Attributes/Attribute">
                 <xsl:sort select="Type" order="descending"/>
            </xsl:apply-templates>
            <errorCodes>
                <xsl:apply-templates select="XML/Attributes/Attribute" 
                                     mode="errors"/>
            </errorCodes>
        </Data>
    </xsl:template>
    <xsl:template 
            match="Attribute[generate-id()=generate-id(key('type', Type)[1])]">
        <Attributes type="{Type}">
            <xsl:apply-templates 
                    select="../Attribute[Type=current()/Type]" mode="out"/>
        </Attributes>
    </xsl:template>
    <xsl:template match="Attribute" mode="out">
         <Attr id="{id}" name="{Name}" value="{Value}"/>
    </xsl:template>
    <xsl:template match="Attribute"/>
    <xsl:template match="Attribute" mode="errors"/>
    <xsl:template match="Attribute[Value='']" mode="errors">
        <errorCode>"value for <xsl:value-of select="Name"/> is missing."</errorCode>
    </xsl:template>
</xsl:stylesheet>

目的の出力を生成します。

<Data Schema="XML A">
    <Attributes type="common">
        <Attr id="331" name="Enviornment" value="Development" />
        <Attr id="79" name="Retail" value="" />
    </Attributes>
    <Attributes type="category">
        <Attr id="402" name="Gender" value="Men" />
    </Attributes>
    <errorCodes>
        <errorCode>"value for Retail is missing."</errorCode>
    </errorCodes>
</Data>
于 2011-03-22T17:01:13.833 に答える