我正在尝试使用 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>
谁能告诉我我能做些什么来实现这一目标?