xml


How can i get arround, that a child produces something, i don't need in his parent neither?


Code is the best language
Source [source.xml]
<asset>
<name>foobar</name>
<paragraph>
I <italic>describe</italic> a function <bold>foobar</bold>.
<headline>I can't be in a paragraph!</headline>
My editor did stuff like this: <bold>Cases: <list><li>item 1</li><li>item 2</li></list></bold>
</paragraph>
</asset>
Source DTD [source.dtd]
<!ELEMENT asset (name,paragraph+)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT paragraph (#PCDATA|bold|headline|italic)*>
<!ELEMENT bold (#PCDATA|list)*>
<!ELEMENT italic (#PCDATA|list)>
<!ELEMENT headline (#PCDATA)>
<!ELEMENT list (li)+>
<!ELEMENT li (#PCDATA)>
expected output [output.xml]
<asset>
<name>foobar</name>
<p>
I <i>describe</i> a function <b>foobar</b>.
</p><title>I can't be in a paragraph!</title><p>
My editor did stuff like this: <b>Cases: </b>
</p>
<enum><item>item 1</item><item>item 2</item></enum>
</asset>
Output DTD [output.dtd]
<!ELEMENT asset (name,(title|p|enum)*)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT p (#PCDATA|b|i)*>
<!ELEMENT b (#PCDATA)*>
<!ELEMENT i (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT enum (item)+>
<!ELEMENT item (#PCDATA)>
Description in words
Simplified source.xml respectively output.xml are defined by their responsible dtd files. The changes are minor but important for my problem.
Element headline[source] is mapped to title[output] and must be a child of asset[output]. (previous/source: it was child of paragraph).
Element list[source] is mapped to enum[output] and must be a child of asset[output]. (previous/source: it was child of bold).
XSLT problems begin
I struggle to build a stylesheet with mainly rule-based templates, cause this example contains only about 4% of elements to work with. What do i expect to do:
Whenever a "breakelement" occurs in my current node, the content till the "breakelement" gets copied, then the "breakelement" gets processed (apply-templates, cause there could be a matching rule for the "breakelement") after that (if there is content) the content of current node continues. You see that in the example of p/headline -> asset/title.
Whenever i apply the same logic from above to element bold, it will produce following wrong content. enum will be outside of b, but now it is wrong, cause it must be outside of p as well:
Content:
<asset>
<name>foobar</name>
<p>
I <i>describe</i> a function <b>foobar</b>.
</p><title>I can't be in a paragraph!</title><p>
My editor did stuff like this: <b>Cases: </b><enum><item>item 1</item><item>item 2</item></enum>
</p>
</asset>
Second case will occur many times in my process from one schema to another schema, i can not handle so far.
How can i get arround, that a child produces something, i don't need in his parent neither?
TBH i can't show any stylesheet, cause this is fully theoretical stuff. Please let me you understand my thread and described problem via comments as you usally do. Highly appreciate your effort, at least for taking time for reading.
I tried to tackle that with for-each-group group-ending-with="headline | list", here is my attempt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<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="asset">
<xsl:copy>
<xsl:for-each-group
select="descendant::node()[not(ancestor::headline | ancestor::list)]"
group-ending-with="headline | list">
<xsl:apply-templates select="current-group()/ancestor::*[last() - 1]" mode="strip">
<xsl:with-param name="copy" tunnel="yes"
select="current-group()[position() ne last()]"/>
</xsl:apply-templates>
<xsl:apply-templates select="current-group()[last()]"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="node()" mode="strip">
<xsl:param name="copy" tunnel="yes"/>
<xsl:if test=". intersect $copy or . intersect $copy/ancestor::*">
<xsl:copy>
<xsl:apply-templates
select="#* | node()[. intersect $copy/ancestor-or-self::node()]" mode="strip">
<xsl:with-param name="copy" tunnel="yes" select="$copy"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="headline | list" mode="strip"/>
<xsl:template match="headline">
<title>
<xsl:apply-templates/>
</title>
</xsl:template>
<xsl:template match="list">
<enum>
<xsl:apply-templates/>
</enum>
</xsl:template>
<xsl:template match="list/li">
<item>
<xsl:apply-templates/>
</item>
</xsl:template>
</xsl:stylesheet>
It transforms
<asset>
<name>foobar</name>
<paragraph>
I <italic>describe</italic> a function <bold>foobar</bold>.
<headline>I can't be in a paragraph!</headline>
My editor did stuff like this: <bold>Cases: <list><li>item 1</li><li>item 2</li></list></bold>
</paragraph>
</asset>
into
<asset>
<name>foobar</name>
<paragraph>
I <italic>describe</italic> a function <bold>foobar</bold>.
</paragraph>
<title>I can't be in a paragraph!</title>
<paragraph>
My editor did stuff like this: <bold>Cases: </bold>
</paragraph>
<enum>
<item>item 1</item>
<item>item 2</item>
</enum>
</asset>

Related Links

Specify a multi-word enumeration value in an XML/DTD
Paypal / Flash button integration as3 problems
xslt grouping issue
Importance of indenting XML file [closed]
Xquery using FLWOR Expression Query
How to insert the text from the .txt file into a xml file [closed]
Using dbDacFx in a manifest file
Parallel iteration over XML nodes with XSLT
oracle xmlcast(xmlquery) in subselect
XML Data Type Conversion
Add one element to the parent element with xslt without copying
Writing my first Ant task: what am I missing?
Download XML file by visiting link
Strip off empty root element only
how to divide the screen with two buttons? [closed]
xslt remove highest level parent if child/child/node is empty [closed]

Categories

HOME
asp.net-web-api
paraview
appcelerator
bitbucket-api
static-site
3d-reconstruction
tail
visual-studio-emulator
square
gembox-spreadsheet
nuget-package
missing-data
joomla3.0
openstreetmap
google-chrome-app
endeca
gson
windows-ce
spring-social
speech-synthesis
bitnami
altera
capistrano3
coin3d
jersey-1.0
foreign-keys
bourbon
sar
lilypond
string-concatenation
php-mysqlidb
dynamically-generated
data.stackexchange.com
moses
genexus-sd
winexe
jaws
winpe
finagle
calculation
volatile
dst
jbutton
hibernate-validator
hard-drive
android-maps-extensions
sendbird
arduino-ide
amazon-ses
connection-pool
gdata
loading
levenshtein-distance
linqpad
smoothstate.js
file-sharing
phpdbg
dotnet-httpclient
java-bytecode-asm
sundials
check-mk
swingworker
branch-prediction
juice-ui
tinkerpop3
udpclient
turbo-prolog
np-spring
tfs-workitem
gocql
canalyzer
iced-coffeescript
cwrsync
post-commit-hook
machine-instruction
strawberry-perl
named-parameters
lnk
dot42
draw2d-js
modular
unit-of-work
mvc-editor-templates
poker
scmmanager
mantle
apache-abdera
resource-management
idn
curb
android-loadermanager
luabind
commerceserver2007
undefined-index
silverlight-oob
wcf-instancing
escrow

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App