Frage

I am looking for a solution through xslt 1.0 and x-path 1.0.

Here is a sample xml file:

<header>
<number>123</number>
<text>test string</text>
</header>

Both number and text are child elements of header node, and both are optional elements in this node. I want to check through xslt 1.0 whether text element is coming after number element or only child of header node if number element doesnt exist. Same condition goes for number element if text element doesnt exist. Basically i want to make sure the elements should come at their correct location. There can be more child elements with same sceraios. I know XSD can do this but i want to add check like this in xslt 1.0. I havent done anything yet from my side yet. Please suggest if we can do this via xslt 1.0

Thanks in advance.

War es hilfreich?

Lösung

Your question is a bit open-ended, but let's interpret as meaning that you want to validate the sequence of children against a content model

A? B? C? D? E?

Here's one way:

<xsl:template match="header">
  <xsl:apply-templates select="*" mode="v"/>
</xsl:template>

<xsl:template match="A" mode="v">
  <xsl:if test="preceding::sibling::*"><error/></xsl:if>
</xsl:template>

<xsl:template match="B" mode="v">
  <xsl:variable name="p" select="preceding-sibling::*[1]"/>
  <xsl:if test="$p and not(name($p) = 'A')"><error/></xsl:if>
</xsl:template>

<xsl:template match="C" mode="v">
  <xsl:variable name="p" select="preceding-sibling::*[1]"/>
  <xsl:if test="$p and not(name($p) = 'A' or name($p) = 'B')"><error/></xsl:if>
</xsl:template>

etc.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top