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 回答