0

xsltを使用してxmlを作成しようとしています。

これは、変換されるxmlです。



    <?xml version="1.0" encoding="utf-8"?>
    <document>
        <document_head>
            <title>This is the title</title>
            <version>This is the title</version>
        </document_head>
        <document_body>
            <paragraph id="TDKR">
                <text>
                    Oh no!, you're not Silvia, you're one of the kung fu creatures on the rampage... Two!
                </text>
                <properties>
                    <size>13px</size>
                    <color>#000000</color>
                </properties>
                <author>Current user</author>
            </paragraph>
            <paragraph id="AAHD">         
                <properties></properties>       
            </paragraph>
        </document_body>
    </document>


これは私が必要なものです:



    <?xml version="1.0" encoding="UTF-8"?>
    <document>
        <document_head>
            <title>This is the title</title>
            <version>This is the title</version>
        </document_head>
        <document_body>
            <paragraph>           
                <properties id="TDKR">
                    <size>13px</size>
                    <color>#000000</color>
                </properties>         
            </paragraph>
            <paragraph>           
                <properties id="AAHD">
                    <size></size>
                    <color></color>
                </properties>
            </paragraph>
        </document_body>
    </document>


これは私が持っているXSLTです:



    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
        <xsl:output method="xml" indent="yes" omit-xml-declaration="no" />

        <!--START Identity transformation-->
            <xsl:template match="@*|node()">
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
            </xsl:template>
        <!--END__ Identity transformation-->

        <!--START Copy of paragraph ID Attribute-->
            <xsl:template match="paragraph/properties" >
                <xsl:copy>
                    <xsl:apply-templates select="../@id | @* | node()"/>
                </xsl:copy>
            </xsl:template>
            <xsl:template match="paragraph">              
                <xsl:copy>
                    <xsl:apply-templates select="node()"/>
                </xsl:copy>
            </xsl:template>
        <!--END__ Copy of paragraph ID Attribute-->

        <!--START Filter of undesired elements--> 
            <xsl:template match="paragraph/text" />
            <xsl:template match="paragraph/author" />
        <!--END__ Filter of undesired elements--> 

        <!--START Creation of empty elements-->
            <xsl:variable name="empty" select="''"/> 

            <xsl:template match="*[not(node())]">
                <xsl:copy>
                    <xsl:apply-templates select="@* | node()" />
                    <xsl:value-of select="$empty"/>
                </xsl:copy>
            </xsl:template>
        <!--END__ Creation of empty elements-->
    </xsl:stylesheet>


これは、変換によって生成されるものです。



    <?xml version="1.0" encoding="UTF-8"?>
    <document>
        <document_head>
            <title>This is the title</title>
            <version>This is the title</version>
        </document_head>
        <document_body>
            <paragraph>           
                <properties id="TDKR">
                    <size>13px</size>
                    <color>#000000</color>
                </properties>         
            </paragraph>
            <paragraph>           
                <properties/>     
            </paragraph>
        </document_body>
    </document>

これを達成するために私に何ができるか誰か教えてもらえますか?

4

1 に答える 1

1

簡単なもの..。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:template match="@*|node()">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>

<xsl:template match="text|author|paragraph/@id" />

<xsl:template match="properties">
 <xsl:copy>
   <xsl:apply-templates select="@*"/>
   <xsl:attribute name="id"><xsl:value-of select="../@id" /></xsl:attribute>
   <xsl:apply-templates select="node()[not(self::size|self::color)]"/>
   <size>
     <xsl:apply-templates select="size/@*|size/node()" /> 
   </size>
   <color>
     <xsl:apply-templates select="color/@*|color/node()" /> 
   </color>
 </xsl:copy>  
</xsl:template>

</xsl:stylesheet>
于 2012-07-16T16:04:40.197 に答える