2

i'm trying to add text to an empty/self-closing tag.

i would like to transform something like "<empty/>" into "<empty>some text</empty>".

this is a shortened version of my xml i'm working on:

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<attr tag="00080090" vr="PN" pos="-1" name="Referring Physician's Name" vm="0" len="0"/>
</dataset>

i would like to get this result:

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<attr tag="00080090" vr="PN" pos="-1" name="Referring Physician's Name" vm="0" len="0">this is the inserted text</attr>
</dataset>

but instead i end up with an unmodified xml. it seems my match is not working if there is no text existing for this tag. it works if there already is some text, it replaces the text in this case, which is fine for me.

my XSL(T) looks like this:

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

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

<xsl:template match="attr[@tag='00080090']/text()">
  <xsl:text>this is the inserted text</xsl:text>
</xsl:template>

</xsl:stylesheet>

i did my testing with http://xslttest.appspot.com

any hints?

4

1 回答 1

3

XSLT 应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="no"/>
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="attr[@tag='00080090']">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:text>this is the inserted text</xsl:text>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
于 2013-07-26T13:10:11.830 回答