使用XSLT替换XML片段?

时间:2011-08-15 11:56:58

标签: xslt

我有一点XML:

<?xml version="1.0" encoding="UTF-8"?>
<photo-caption>
    <p>
        <?EM-dummyText caption?>
        <ld pattern=" "/>
        <s2>Photo </s2>
        <source>
            <?EM-dummyText photographer?>
        </source>
    </p>
</photo-caption>

我想要这个输出:

<?xml version="1.0" encoding="UTF-8"?>
<photo-caption>
      <p>
        <s2><?EM-dummyText heading?></s2>
        <?EM-dummyText caption?>
      </p>
</photo-caption>

这是我目前正在使用的XSLT:

<xsl:stylesheet version="1.0" 
    exclude-result-prefixes="subst" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:subst="http://tempuri.org">
    <xsl:strip-space elements="*"/>
    <xsl:output method="xml" encoding="utf-8" indent="yes"/>
    <subst:photo-caption>
        <p>
            <s2>
                <?EM-dummyText heading?>
            </s2>
            <?EM-dummyText caption?>
        </p>
    </subst:photo-caption>
    <xsl:variable name="subst" select="document('')/*/subst:photo-caption"/>
    <xsl:template match="photo-caption">
        <xsl:copy-of select="$subst"/>
    </xsl:template>
</xsl:stylesheet>

...产生了这个输出:

<?xml version="1.0" encoding="utf-8"?>
<subst:photo-caption xmlns:subst="http://tempuri.org" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <p>
      <s2><?EM-dummyText heading?></s2><?EM-dummyText caption?>
   </p>
</subst:photo-caption>

如何从输出中删除前缀和名称空间属性?或者有更好的方法吗?

1 个答案:

答案 0 :(得分:2)

如果您只想输出该片段,请使用

<xsl:template match="photo-caption">
<photo-caption>
      <p>
        <s2>
          <xsl:processing-instruction name="EM-dummyText">heading</xsl:processing-instruction>
        </s2>
        <xsl:processing-instruction name="EM-dummyText">caption</xsl:processing-instruction>
      </p>
</photo-caption>
</xsl:template>