Reputation: 35
I need help in fetching the XML content between comments using XSLT.
XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<!-- start comment 1 -->
<book>
<title lang="it">Learning XML</title>
<price>39.95</price>
</book>
<!-- end comment 1 -->
</bookstore>
Output:
<book>
<title lang="it">Learning XML</title>
<price>39.95</price>
</book>
Upvotes: 0
Views: 595
Reputation: 52888
You could try something like this...
XML Input
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<!-- start comment 1 -->
<book>
<title lang="it">Learning XML</title>
<price>39.95</price>
</book>
<!-- end comment 1 -->
</bookstore>
XSLT 1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<xsl:apply-templates select="*[preceding-sibling::comment()[starts-with(normalize-space(.),'start')] and
following-sibling::comment()[starts-with(normalize-space(.),'end')]]"/>
</xsl:template>
</xsl:stylesheet>
Output
<book>
<title lang="it">Learning XML</title>
<price>39.95</price>
</book>
Upvotes: 2
Reputation: 917
Relying on comment to copy is not really good. But, I think - you have some rationale why you chose to do that. Here is my try.
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="comment()">
<xsl:if test="text()='START'">
<!-- Set Flag for copying content <xsl:variable name="dummy" value-of="myPrefix:setFlag()"/> -->
</xsl:if>
<xsl:if test="text()='END'">
<!-- Reset Flag for stop copying content -->
</xsl:if>
</xsl:template>
Unfortunately, you can't update variables in XSLT. Maybe, you can try using your own Java class instance which can have the flag stuff which is checked by your templates to decide on copy or not.
Upvotes: -1