Using XSD to validate node count - xsd

I don't think this is possible but I thought I'd throw it out there. Given this XML:
<people count="3">
<person>Bill</person>
<person>Joe</person>
<person>Susan</person>
</people>
Is it possible in an XSD to force the #count attribute value to be the correct count of defined elements (in this case, the person element)? The above example would obviously be correct and the below example would not validate:
<people count="5">
<person>Bill</person>
<person>Joe</person>
<person>Susan</person>
</people>

I'm pretty sure XSD can't do that. However if you want to guarantee that your count attribute is the real count of elements below, running a XSLT stylesheet on the document can ensure that is true by setting the value:
<xsl:template match="people">
<xsl:attribute name="count">
<xsl:value-of select="count(person)"/>
</xsl:attibute>
<xsl:apply-templates/>
</xsl:template>
<!-- insert your identity template here -->

Parsers implementing XSD Specification 1.1 should provide an assert function that can be used to validate the XML content against number of child nodes

Related

How to do XML-Split and Keep Parent Element?

I know Spring Integration is rather old but I have a piece of code that needs small modification to move on. I need to split the incoming XML message and preserve the parent node. Here is my XML:
<Order>
<Item>A</Item>
<Item>B</Item>
</Order>
And I want to split into 2 XMLs and preserve the XSD validation.
<Order>
<Item>A</Item>
</Order>
and
<Order>
<Item>B</Item>
</Order>
Did this:
<beans xmlns:int-xml="http://www.springframework.org/schema/integration/xml">
...
<int-xml:xpath-splitter id="splitter">
<int-xml:xpath-expression expression="/Order/Item"/>
</int-xml:xpath-splitter>
...
It split into
<Item>A</Item>
and
<Item>B</Item>
but I need the parent to be there. Anyway to make it works? Thanks in Advance.
What you need is an <int-xml:xslt-transformer> after your <int-xml:xpath-splitter>.
See Reference Manual for more info: https://docs.spring.io/spring-integration/docs/5.2.0.RELEASE/reference/html/xml.html#xml-xslt-payload-transformers
You need to learn what is XSLT and so on.
I know Spring Integration is rather old
??
It is actively maintained and enhanced; there was a completely new release this month.
You either need a custom splitter, or you can add a transformer after the splitter to add the outer node.

How to unescape a CDATA section using JAXB in a dynamic way

I'm trying to make a section of my SOAP request look like this:
<![CDATA[<?xml version="1.0" encoding="UTF-8"?><placeholder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="WirelessEdgeInfo_v1_0.xsd"/>]]>
However, it looks like this after it has been escaped by underlying JAXB:
<![CDATA[<?xml version="1.0"
encoding="UTF-8"?> <placeholder
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="WirelessEdgeInfo_v1_0.xsd"/>]]&gt
I have looked at numerous examples, such as using MOXy and also implementing a CharacterEscapeHandler adapter or XmlAdapter HOWEVER the problem is the same for ALL these implementations. That is, it always comes down to this:
marshaller.setProperty(CharacterEscapeHandler.class.getName(),
new XmlCharacterHandler());
Setting a property in the marshaller.
However, in my case, I am using annotations generated through WSImport to generate my Web Services code dynamically when I execute the client SOAP call.
i.e. I do not have access to the marshaller object.
SO for me, how can I unescape the CDATA section of a String variable I need to pass in if I cannot set any property on the marshaller type?
Can I somehow "hack" or trick JAXB to tell it not to escape CDATA values? Or can I use annotations or somehow dynamically indicate unescaping?
I tried the following Adapter via annotation but of course this did not work:
#XmlJavaTypeAdapter(value=CDATAAdapter.class) //this is not working right now
protected String variableXmlContent;
variableXmlContent still ended up escaped. I also tried unescaping variableXmlContent and setting it as an input value, but it still came out the same way, unescaped.
An XML element value containing <, >, & is escaped by the marshaller using <, >, &.
Thus, if the XML element should contain
<![CDATA[ ... ]]>
the result in the XML file will be
<![CDATA[ ... ]]>
I suspect that the text to be passed as an XML element is
<?xml version="1.0" ... v1_0.xsd"/>
Now if I had to write this by hand into an XML file, I'd write it into a "CDATA section", but a (JAXB) marshaller will simply treat those three characters as I described before, and that's it.
<?xml version="1.0" ... v1_0.xsd"/>
It is pointless to try and make any XML marshaller use the CDATA mechanism, and there is no such thing as a "CDATA value" requiring special treatment - so there is no such special treatment.

XSL : Attribute name from value of random element or other attribute [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to send the value of element to the attribute, which overwrite that attribute name. Value of the attribute remain the same.
Example:
<engine hybrid="yes">
<A>2000</A>
<B>diesel</B>
<C>160</C>
</engine>
What i need to achieve for example:
<engine diesel="yes">
<A>2000</A>
<B>diesel</B>
<C>160</C>
</engine>
Thanks in advance!
You should base your transformation on the identity template, which copies everything from the input to output unchanged except where this behaviour is overridden by more specific templates
<xsl:template match="#*|node()">
<xsl:copy><xsl:apply-templates select="#*|node()" /></xsl:copy>
</xsl:template>
With this in place you can then just add a specific template for the engine element
<xsl:template match="engine">
<engine>
<!-- create an attribute whose name is taken from the first B child -->
<xsl:attribute name="{B}">yes</xsl:attribute>
<!-- and continue processing child nodes (but not other attributes) -->
<xsl:apply-templates />
</engine>
</xsl:template>
The key thing to note here is that the name attribute of xsl:attribute is treated as an "attribute value template" - you can enclose parts of the value in braces {} and these parts will be evaluated as XPath expressions rather than being taken as literal values.
Finally, of course, you should note that this will only work if the content of B is a valid XML attribute name (so no whitespace, no characters like = or ", and it must not start with a digit).

xsl:fo how to include dynamically generated svg

I want to generate a svg chart and insert it into pdf using apache fop. So far I have tried using
<fo:instream-foreign-object xmlns:svg="http://www.w3.org/2000/svg">
<xsl:value-of select="svgData"/>
</fo:instream-foreign-object>
in xsl file and generated svg is held as a string in the "svgData". This approach is not working as it is not parsing the string svg data and just appending it.
How do I achieve this ? I need to create the svg in java (baitk?) and add it to the pdf.
I spent some time too having headache on this problem. Finally I could make it work by using a <xsl:copy-of> instead of <xsl:value-of> + using prefixed namespace.
<fo:block>
<fo:instream-foreign-object content-type="content-type:xml/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<xsl:copy-of select="/path/to/svg:svg"/>
</fo:instream-foreign-object>
</fo:block>
I spent many hours solving the problem. Without success. My recommendation is not to pursue this approach any further.
My solution to get dynamic SVG into a PDF is the following.
Save the SVG on your hard disk and remember the filename/path in the object.
Then use the filename to fill the external-graphic src attribute.
After creating the PDF the svg-files have to be deleted.
Done.
XSL:
<xsl:template match="qandapair">
<fo:block text-align="center">
<fo:external-graphic src="{chart}"/>
</fo:block>
</xsl:template>
XML:
<qandapair>
<chart>gaoejTLVfcBUrCgjvmsWgjBwQzHYmYYJ.svg</chart>
...
</qandapair>

How to Get and Output Field Choices in a Custom SharePoint List DispForm Page?

I'm working on a custom Display Form for a SharePoint list. Is there a way to get the field choices for a field that has multiple choices? That is, I can print out the chosen value with something like:
<xsl:value-of select="#Migration_x0020_Status" />
But, I want to print out all the choices, including the one that was chosen. The closest I've come is to use the FormField element from the EditForm page like the following:
<SharePoint:FormField runat="server" id="ff4{$Pos}" ControlMode="Edit" FieldName="Migration_x0020_Status" />
However, that prints out SELECT and OPTION elements. I want to get the underlying choices and print out my own stuff (eg: with UL and LI elements). How can I do this?
i would suggest to use object model to create a custom webpart for the same. if thats not the CHOICE I would like to suggest a slight change in your architecture to include lookup column and then in your xslt datasource add the master list data source as well and then work on xslt to show it the way you want

Resources