Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

Preserve Formatting on Copied Elements (XSLT/XML)

$
0
0

Okay so Simple XML:

<people><person><name>Bob</name><age>25</age></person><person><name>Jill</name><age>23</age></person></people>

So we want to add an Element before age so we do:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" version="1.0"><xsl:output method="xml" indent="yes" encoding ="utf-8" /><xsl:template match="//person/age"><xsl:variable name="vIdx" select="count(../preceding-sibling::person)+1" /><xsl:element name="gender"><xsl:choose><xsl:when test="$vIdx=1"><xsl:text>male</xsl:text></xsl:when><xsl:when test="$vIdx=2"><xsl:text>female</xsl:text></xsl:when></xsl:choose></xsl:element><xsl:copy-of select="." /></xsl:template><xsl:template match="@* | node()" name="default"><xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy></xsl:template></xsl:stylesheet>

Now with that above XSLT you can use either call-template to call the default template, you can directly copy the same xsl:copy block from the default template, or use the 'copy-of' i currently have.  You can even try rebuilding the "age" element again manually.  You can have the copy-of (or other) before the xsl:element injection for gender or after ANY WAY YOU SLICE IT THE OUTPUT WILL BE:

<people><person><name>Bob</name><gender>male</gender><age>25</age></person><person><name>Jill</name><gender>female</gender>    <age>23</age></person></people><!-- OR IF YOU DO GENDER AFTER AGE --><people><person><name>Bob</name><age>25</age><gender>male</gender></person><person><name>Jill</name><age>23</age><gender>female</gender>    </person></people>

Either way the SECOND element does not get indented correctly event with xml:output indent set to yes.

How can I get the INDENT to be correct for this type of injection? (without hard coding the spaces from the input document.  Imagine the XLST has to figure out the indent of the current node not a hard coded 4 spaces for this XML example, and 12 spaces for a different one.  I want the XSLT to just do it regardless of the source XML, I want it to preserve the spacing and match the spacing for similar level nodes that are added/copied/updated etc.)

Thanks

Jaeden "Sifo Dyas" al'Raec Ruiner


"Never Trust a computer. Your brain is smarter than any micro-chip."
PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>