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

XSLT split string by charlength and call function for each substring
TypeError: Error #2007: Parameter child must be non-null in as3 while playing VPAID Ads in adobe player
XSLT: Specific tag placement
Talend - getCode() error while extracting XML file
value-of select=“document($path)/ns0:element1/ns0:element2” returns NULL with SAXON-B XSLT 2.0 (saxon9)
XSD extend base with suppression of one attribute
ServiceStack Response - Change encoding?
Wrapping multiple identically named elements into a new element using XSLT
Xml schema uniqueness in entire subtree
dynamic xml nodes using xslt
xpst0003 xquery syntax error
Xsl anchor attribute
How to get start and end of a week of a specific date using xslt?
how to select elements when the root element has a lot of attributes
xsd select multiple values from enumeration or equivalent type
Issue with XSL loops and linefeeds

Categories

HOME
twitter-bootstrap-3
linkedin
android-studio-2.2
battery
requirements
banner
nstableheaderview
32bit-64bit
heap
supervisor
android-intent
package-managers
mvc-mini-profiler
feathersjs
android-7.0-nougat
jersey-1.0
gmm
enterprise-miner
imagemap
href
systemtime
roundup
surveymonkey
gpio
fragment-backstack
android-things
sqlpackage
eviews
testbed
jmockit
android-maps-extensions
getpixel
arduino-ide
python-pptx
openproject
wurfl
openalpr
jpad
hackintosh
kill
monkey-testing
x++
vaadin-elements
textsum
wcftestclient
mschart
facebook-pixel
vugen
gpg-signature
objectscript
rocks
grails-plugin
dotnetnuke-7
http-status-code-401
false-sharing
highland.js
jnlua
qt5.2
poppler
phonertc
finalbuilder
eoferror
dronekit-android
j-security-check
adehabitathr
cryptographichashfunction
exceldatareader
iiop
metatrader5
easyrtc
template-deduction
ss7
fpdi
menubar
openargs
gtm-oauth2
scrypt
code-duplication
neos-server
asdf
workitem
extreme-programming
inkcanvas
httponly
denied
expression-blend-4
mbeans
twitterizer
diagrams
luajava
ruby-1.8
resharper-6.0
interprocess
wcf-instancing
wordprocessingml
alsb
wysiwym
localizable.strings
online-storage

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